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)
30 guint64 cstart, cstop;
33 gst_segment_init (&segment, GST_FORMAT_BYTES);
35 /* configure segment to start 100 */
36 gst_segment_do_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);
45 /* configure segment to stop relative, should not do anything since
47 gst_segment_do_seek (&segment, 1.0,
50 GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
51 fail_unless (segment.start == 100);
52 fail_unless (segment.stop == -1);
53 fail_unless (update == FALSE);
56 /* do some clipping on the open range */
57 /* completely outside */
58 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
59 fail_unless (res == FALSE);
61 /* touching lower bound, still outside of the segment */
62 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
63 fail_unless (res == FALSE);
65 /* partially inside */
66 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
67 fail_unless (res == TRUE);
68 fail_unless (cstart == 100);
69 fail_unless (cstop == 150);
71 /* inside, touching lower bound */
72 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
73 100, 150, &cstart, &cstop);
74 fail_unless (res == TRUE);
75 fail_unless (cstart == 100);
76 fail_unless (cstop == 150);
78 /* special case, 0 duration and outside segment */
79 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 90, 90, &cstart, &cstop);
80 fail_unless (res == FALSE);
82 /* special case, 0 duration and touching lower bound, i.e. inside segment */
83 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
84 100, 100, &cstart, &cstop);
85 fail_unless (res == TRUE);
86 fail_unless (cstart == 100);
87 fail_unless (cstop == 100);
89 /* special case, 0 duration and inside the segment */
90 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
91 120, 120, &cstart, &cstop);
92 fail_unless (res == TRUE);
93 fail_unless (cstart == 120);
94 fail_unless (cstop == 120);
96 /* completely inside */
97 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
98 150, 200, &cstart, &cstop);
99 fail_unless (res == TRUE);
100 fail_unless (cstart == 150);
101 fail_unless (cstop == 200);
104 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
105 fail_unless (res == FALSE);
107 /* start outside, we don't know the stop */
108 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
109 fail_unless (res == TRUE);
110 fail_unless (cstart == 100);
111 fail_unless (cstop == -1);
113 /* start on lower bound */
114 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
115 fail_unless (res == TRUE);
116 fail_unless (cstart == 100);
117 fail_unless (cstop == -1);
120 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
121 fail_unless (res == TRUE);
122 fail_unless (cstart == 150);
123 fail_unless (cstop == -1);
125 /* add 100 to start, set stop to 300 */
126 gst_segment_do_seek (&segment, 1.0,
129 GST_SEEK_TYPE_SET, 100 + 100, GST_SEEK_TYPE_SET, 300, &update);
130 fail_unless (segment.start == 200);
131 fail_unless (segment.stop == 300);
132 fail_unless (update == TRUE);
135 /* add 100 to start (to 300), set stop to 200, this is not allowed.
136 * nothing should be updated in the segment. A g_warning is
138 ASSERT_CRITICAL (gst_segment_do_seek (&segment, 1.0,
141 GST_SEEK_TYPE_SET, 200 + 100, GST_SEEK_TYPE_SET, 200, &update));
142 fail_unless (segment.start == 200);
143 fail_unless (segment.stop == 300);
144 /* update didn't change */
145 fail_unless (update == FALSE);
148 /* seek relative to end, should not do anything since size is
150 gst_segment_do_seek (&segment, 1.0,
153 GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100, &update);
154 fail_unless (segment.start == 200);
155 fail_unless (segment.stop == 300);
156 fail_unless (update == FALSE);
158 /* completely outside */
159 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
160 fail_unless (res == FALSE);
162 /* touching lower bound */
163 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
164 fail_unless (res == FALSE);
166 /* partially inside */
167 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
168 fail_unless (res == TRUE);
169 fail_unless (cstart == 200);
170 fail_unless (cstop == 250);
172 /* inside, touching lower bound */
173 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
174 200, 250, &cstart, &cstop);
175 fail_unless (res == TRUE);
176 fail_unless (cstart == 200);
177 fail_unless (cstop == 250);
179 /* completely inside */
180 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
181 250, 290, &cstart, &cstop);
182 fail_unless (res == TRUE);
183 fail_unless (cstart == 250);
184 fail_unless (cstop == 290);
186 /* partially inside */
187 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
188 250, 350, &cstart, &cstop);
189 fail_unless (res == TRUE);
190 fail_unless (cstart == 250);
191 fail_unless (cstop == 300);
194 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
195 fail_unless (res == FALSE);
198 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
199 fail_unless (res == TRUE);
200 fail_unless (cstart == 200);
201 fail_unless (cstop == 300);
203 /* start on lower bound */
204 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
205 fail_unless (res == TRUE);
206 fail_unless (cstart == 200);
207 fail_unless (cstop == 300);
210 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
211 fail_unless (res == TRUE);
212 fail_unless (cstart == 250);
213 fail_unless (cstop == 300);
215 /* start outside on boundary */
216 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
217 fail_unless (res == FALSE);
219 /* start completely outside */
220 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
221 fail_unless (res == FALSE);
226 /* mess with the segment structure in the bytes format */
227 GST_START_TEST (segment_seek_size)
231 guint64 cstart, cstop;
234 gst_segment_init (&segment, GST_FORMAT_BYTES);
235 segment.duration = 200;
237 /* configure segment to start 100 */
238 gst_segment_do_seek (&segment, 1.0,
241 GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
242 fail_unless (segment.start == 100);
243 fail_unless (segment.stop == -1);
244 fail_unless (update == TRUE);
246 /* do some clipping on the open range */
247 /* completely outside */
248 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
249 fail_unless (res == FALSE);
251 /* touching lower bound */
252 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
253 fail_unless (res == FALSE);
255 /* partially inside */
256 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
257 fail_unless (res == TRUE);
258 fail_unless (cstart == 100);
259 fail_unless (cstop == 150);
261 /* inside, touching lower bound */
262 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
263 100, 150, &cstart, &cstop);
264 fail_unless (res == TRUE);
265 fail_unless (cstart == 100);
266 fail_unless (cstop == 150);
268 /* completely inside */
269 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
270 150, 200, &cstart, &cstop);
271 fail_unless (res == TRUE);
272 fail_unless (cstart == 150);
273 fail_unless (cstop == 200);
276 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
277 fail_unless (res == FALSE);
280 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
281 fail_unless (res == TRUE);
282 fail_unless (cstart == 100);
283 fail_unless (cstop == -1);
285 /* start on lower bound */
286 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
287 fail_unless (res == TRUE);
288 fail_unless (cstart == 100);
289 fail_unless (cstop == -1);
292 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
293 fail_unless (res == TRUE);
294 fail_unless (cstart == 150);
295 fail_unless (cstop == -1);
297 /* add 100 to start, set stop to 300, stop clips to 200 */
298 gst_segment_do_seek (&segment, 1.0,
301 GST_SEEK_TYPE_SET, 100 + 100, GST_SEEK_TYPE_SET, 300, &update);
302 fail_unless (segment.start == 200);
303 fail_unless (segment.stop == 200);
305 /* add 100 to start (to 300), set stop to 200, this clips start
307 gst_segment_do_seek (&segment, 1.0,
310 GST_SEEK_TYPE_SET, 200 + 100, GST_SEEK_TYPE_SET, 200, &update);
311 fail_unless (segment.start == 200);
312 fail_unless (segment.stop == 200);
313 fail_unless (update == FALSE);
315 /* seek relative to end */
316 gst_segment_do_seek (&segment, 1.0,
319 GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
320 fail_unless (segment.start == 100);
321 fail_unless (segment.stop == 180);
322 fail_unless (update == TRUE);
324 /* completely outside */
325 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
326 fail_unless (res == FALSE);
328 /* touching lower bound */
329 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
330 fail_unless (res == FALSE);
332 /* partially inside */
333 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
334 fail_unless (res == TRUE);
335 fail_unless (cstart == 100);
336 fail_unless (cstop == 150);
338 /* inside, touching lower bound */
339 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
340 100, 150, &cstart, &cstop);
341 fail_unless (res == TRUE);
342 fail_unless (cstart == 100);
343 fail_unless (cstop == 150);
345 /* completely inside */
346 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
347 150, 170, &cstart, &cstop);
348 fail_unless (res == TRUE);
349 fail_unless (cstart == 150);
350 fail_unless (cstop == 170);
352 /* partially inside */
353 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
354 150, 250, &cstart, &cstop);
355 fail_unless (res == TRUE);
356 fail_unless (cstart == 150);
357 fail_unless (cstop == 180);
360 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
361 fail_unless (res == FALSE);
364 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
365 fail_unless (res == TRUE);
366 fail_unless (cstart == 100);
367 fail_unless (cstop == 180);
369 /* start on lower bound */
370 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
371 fail_unless (res == TRUE);
372 fail_unless (cstart == 100);
373 fail_unless (cstop == 180);
376 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
377 fail_unless (res == TRUE);
378 fail_unless (cstart == 150);
379 fail_unless (cstop == 180);
381 /* start outside on boundary */
382 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
383 fail_unless (res == FALSE);
385 /* start completely outside */
386 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
387 fail_unless (res == FALSE);
392 GST_START_TEST (segment_seek_reverse)
397 gst_segment_init (&segment, GST_FORMAT_BYTES);
398 segment.duration = 200;
400 /* configure segment to stop 100 */
401 gst_segment_do_seek (&segment, -1.0,
404 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 100, &update);
405 fail_unless (segment.start == 0);
406 fail_unless (segment.stop == 100);
407 fail_unless (segment.time == 0);
408 fail_unless (segment.position == 100);
409 fail_unless (update == TRUE);
412 gst_segment_do_seek (&segment, -1.0,
415 GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_SET, 100 - 20, &update);
416 fail_unless (segment.start == 10);
417 fail_unless (segment.stop == 80);
418 fail_unless (segment.time == 10);
419 fail_unless (segment.position == 80);
420 fail_unless (update == TRUE);
422 gst_segment_do_seek (&segment, -1.0,
425 GST_SEEK_TYPE_SET, 20, GST_SEEK_TYPE_NONE, 0, &update);
426 fail_unless (segment.start == 20);
427 fail_unless (segment.stop == 80);
428 fail_unless (segment.time == 20);
429 fail_unless (segment.position == 80);
430 fail_unless (update == FALSE);
435 /* mess with the segment structure in the bytes format */
436 GST_START_TEST (segment_seek_rate)
441 gst_segment_init (&segment, GST_FORMAT_BYTES);
443 /* configure segment to rate 2.0 */
444 gst_segment_do_seek (&segment, 2.0,
447 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1, &update);
448 fail_unless (segment.format == GST_FORMAT_BYTES);
449 fail_unless (segment.start == 0);
450 fail_unless (segment.stop == -1);
451 fail_unless (segment.rate == 2.0);
452 fail_unless (update == FALSE);
455 /* 0 is the same in all formats and should not fail */
456 gst_segment_do_seek (&segment, 2.0,
457 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
458 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1, &update);
459 fail_unless (segment.format == GST_FORMAT_BYTES);
461 /* set to -1 means start from 0 */
462 gst_segment_do_seek (&segment, 2.0,
463 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
464 GST_SEEK_TYPE_SET, -1, GST_SEEK_TYPE_NONE, -1, &update);
465 fail_unless (segment.format == GST_FORMAT_BYTES);
466 fail_unless (segment.start == 0);
468 gst_segment_do_seek (&segment, 2.0,
469 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
470 GST_SEEK_TYPE_CUR, 0, GST_SEEK_TYPE_NONE, -1, &update);
472 gst_segment_do_seek (&segment, 2.0,
473 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
474 GST_SEEK_TYPE_END, 0, GST_SEEK_TYPE_NONE, -1, &update);
476 /* -1 for end is fine too in all formats */
477 gst_segment_do_seek (&segment, 2.0,
478 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
479 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
481 /* 0 as relative end is fine too */
482 gst_segment_do_seek (&segment, 2.0,
483 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
484 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
486 gst_segment_do_seek (&segment, 2.0,
487 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
488 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
491 /* set a real stop position, this must happen in bytes */
492 gst_segment_do_seek (&segment, 3.0,
495 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, 100, &update);
496 fail_unless (segment.format == GST_FORMAT_BYTES);
497 fail_unless (segment.start == 0);
498 fail_unless (segment.stop == 100);
499 fail_unless (segment.rate == 3.0);
500 /* no seek should happen, we just updated the stop position in forward
502 fail_unless (update == FALSE);
505 /* 0 as relative end is fine too */
506 gst_segment_do_seek (&segment, 2.0,
507 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
508 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
509 fail_unless (segment.stop == 100);
511 gst_segment_do_seek (&segment, 2.0,
512 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
513 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
514 fail_unless (segment.stop == 100);
516 /* -1 for end is fine too in all formats */
517 gst_segment_do_seek (&segment, 2.0,
518 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
519 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
520 fail_unless (segment.stop == -1);
523 /* set some duration, stop -1 END seeks will now work with the
524 * duration, if the formats match */
525 segment.duration = 200;
526 fail_unless (segment.duration == 200);
528 /* seek to end with 0 should set the stop to the duration */
529 gst_segment_do_seek (&segment, 2.0,
530 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
531 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
532 fail_unless (segment.stop == 200);
533 fail_unless (segment.duration == 200);
535 /* subtract 100 from the end */
536 gst_segment_do_seek (&segment, 2.0,
537 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
538 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, -100, &update);
539 fail_unless (segment.stop == 100);
540 fail_unless (segment.duration == 200);
542 /* add 100 to the duration, this should be clamped to the duration */
543 gst_segment_do_seek (&segment, 2.0,
544 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
545 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 100, &update);
546 fail_unless (segment.stop == 200);
547 fail_unless (segment.duration == 200);
550 /* add 300 to the start, this should be clamped to the duration */
551 gst_segment_do_seek (&segment, 2.0,
552 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
553 GST_SEEK_TYPE_CUR, 300, GST_SEEK_TYPE_END, 0, &update);
554 fail_unless (segment.start == 200);
555 fail_unless (segment.stop == 200);
556 fail_unless (segment.duration == 200);
558 /* subtract 300 from the start, this should be clamped to 0 */
559 gst_segment_do_seek (&segment, 2.0,
560 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
561 GST_SEEK_TYPE_CUR, -300, GST_SEEK_TYPE_END, 0, &update);
562 GST_DEBUG ("%" G_GINT64_FORMAT, segment.start);
563 fail_unless (segment.start == 0);
564 fail_unless (segment.stop == 200);
565 fail_unless (segment.duration == 200);
572 /* mess with the segment structure in the bytes format */
573 GST_START_TEST (segment_newsegment_open)
577 gst_segment_init (&segment, GST_FORMAT_BYTES);
579 /* time should also work for starting from 0 */
580 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0, -1,
583 fail_unless (segment.rate == 1.0);
584 fail_unless (segment.format == GST_FORMAT_BYTES);
585 fail_unless (segment.flags == 0);
586 fail_unless (segment.start == 0);
587 fail_unless (segment.stop == -1);
588 fail_unless (segment.time == 0);
589 fail_unless (segment.base == 0);
590 fail_unless (segment.position == 0);
591 fail_unless (segment.duration == -1);
593 /* we set stop but in the wrong format, stop stays open. */
594 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0,
597 fail_unless (segment.start == 0);
598 fail_unless (segment.stop == -1);
599 fail_unless (segment.time == 0);
600 fail_unless (segment.base == 0);
601 fail_unless (segment.position == 0);
603 /* update, nothing changes */
604 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 0, -1,
607 fail_unless (segment.start == 0);
608 fail_unless (segment.stop == -1);
609 fail_unless (segment.time == 0);
610 fail_unless (segment.base == 0);
611 fail_unless (segment.position == 0);
614 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
615 GST_FORMAT_BYTES, 100, -1, 100);
617 fail_unless (segment.start == 100);
618 fail_unless (segment.stop == -1);
619 fail_unless (segment.time == 100);
620 fail_unless (segment.base == 100);
621 fail_unless (segment.position == 100);
623 /* last_stop 0, base does not change */
624 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
627 fail_unless (segment.start == 0);
628 fail_unless (segment.stop == -1);
629 fail_unless (segment.time == 0);
630 fail_unless (segment.base == 100);
632 gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
634 fail_unless (segment.position == 200);
636 /* last_stop 200, base changes */
637 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
640 fail_unless (segment.start == 0);
641 fail_unless (segment.stop == -1);
642 fail_unless (segment.time == 0);
643 fail_unless (segment.base == 300);
644 fail_unless (segment.position == 0);
650 /* mess with the segment structure in the bytes format */
651 GST_START_TEST (segment_newsegment_closed)
655 gst_segment_init (&segment, GST_FORMAT_BYTES);
657 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
658 GST_FORMAT_BYTES, 0, 200, 0);
660 fail_unless (segment.rate == 1.0);
661 fail_unless (segment.format == GST_FORMAT_BYTES);
662 fail_unless (segment.flags == 0);
663 fail_unless (segment.start == 0);
664 fail_unless (segment.stop == 200);
665 fail_unless (segment.time == 0);
666 fail_unless (segment.base == 0);
667 fail_unless (segment.position == 0);
668 fail_unless (segment.duration == -1);
670 /* assume we advanced to position 40 */
671 gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 40);
672 fail_unless (segment.position == 40);
674 /* do an update to the start, last_stop is unchanged because it's bigger */
675 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 20,
678 fail_unless (segment.start == 20);
679 fail_unless (segment.stop == 200);
680 fail_unless (segment.time == 20);
681 fail_unless (segment.base == 20);
682 fail_unless (segment.position == 40);
684 /* do an update past our last_stop, it should be updated now */
685 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 50,
688 fail_unless (segment.start == 50);
689 fail_unless (segment.stop == 300);
690 fail_unless (segment.time == 50);
691 fail_unless (segment.base == 50);
692 fail_unless (segment.position == 50);
694 /* and a new accumulated one */
695 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
696 GST_FORMAT_BYTES, 100, 400, 300);
698 fail_unless (segment.start == 100);
699 fail_unless (segment.stop == 400);
700 fail_unless (segment.time == 300);
701 fail_unless (segment.base == 300);
703 /* and a new updated one */
704 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
705 GST_FORMAT_BYTES, 100, 500, 300);
707 fail_unless (segment.start == 100);
708 fail_unless (segment.stop == 500);
709 fail_unless (segment.time == 300);
710 fail_unless (segment.base == 300);
712 /* and a new partially updated one */
713 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
714 GST_FORMAT_BYTES, 200, 500, 400);
716 fail_unless (segment.start == 200);
717 fail_unless (segment.stop == 500);
718 fail_unless (segment.time == 400);
719 fail_unless (segment.base == 400);
724 /* mess with the segment structure in the time format */
725 GST_START_TEST (segment_newsegment_streamtime)
730 gst_segment_init (&segment, GST_FORMAT_TIME);
732 /***************************
734 ***************************/
735 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
736 GST_FORMAT_TIME, 0, 200, 0);
738 fail_unless (segment.rate == 1.0);
739 fail_unless (segment.applied_rate == 1.0);
740 fail_unless (segment.format == GST_FORMAT_TIME);
741 fail_unless (segment.flags == 0);
742 fail_unless (segment.start == 0);
743 fail_unless (segment.stop == 200);
744 fail_unless (segment.time == 0);
745 fail_unless (segment.base == 0);
746 fail_unless (segment.position == 0);
747 fail_unless (segment.duration == -1);
749 /* invalid time gives invalid result */
750 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
751 fail_unless (result == -1);
753 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
754 fail_unless (result == 0);
756 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
757 fail_unless (result == 100);
759 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
760 fail_unless (result == 200);
762 /* outside of the segment */
763 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
764 fail_unless (result == -1);
766 /*********************
767 * time shifted by 500
768 *********************/
769 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
770 GST_FORMAT_TIME, 0, 200, 500);
772 fail_unless (segment.base == 200);
774 /* invalid time gives invalid result */
775 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
776 fail_unless (result == -1);
778 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
779 fail_unless (result == 500);
781 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
782 fail_unless (result == 600);
784 /* outside of the segment */
785 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
786 fail_unless (result == -1);
788 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
789 fail_unless (result == -1);
791 /*********************
793 *********************/
794 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
795 GST_FORMAT_TIME, 500, 700, 0);
797 fail_unless (segment.base == 400);
799 /* invalid time gives invalid result */
800 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
801 fail_unless (result == -1);
803 /* before segment is invalid */
804 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
805 fail_unless (result == -1);
807 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
808 fail_unless (result == 0);
810 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
811 fail_unless (result == 100);
813 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
814 fail_unless (result == 200);
816 /* outside of the segment */
817 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
818 fail_unless (result == -1);
820 /*************************************
821 * time offset by 500, shifted by 200
822 *************************************/
823 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
824 GST_FORMAT_TIME, 500, 700, 200);
826 fail_unless (segment.base == 600);
828 /* invalid time gives invalid result */
829 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
830 fail_unless (result == -1);
832 /* before segment is invalid */
833 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
834 fail_unless (result == -1);
836 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
837 fail_unless (result == 200);
839 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
840 fail_unless (result == 300);
842 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
843 fail_unless (result == 400);
845 /* outside of the segment */
846 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
847 fail_unless (result == -1);
852 /* mess with the segment structure in the time format */
853 GST_START_TEST (segment_newsegment_streamtime_rate)
858 gst_segment_init (&segment, GST_FORMAT_TIME);
860 /***************************
861 * Normal segment rate 2.0
862 ***************************/
863 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
864 GST_FORMAT_TIME, 0, 200, 0);
866 fail_unless (segment.rate == 2.0);
867 fail_unless (segment.applied_rate == 1.0);
868 fail_unless (segment.format == GST_FORMAT_TIME);
869 fail_unless (segment.flags == 0);
870 fail_unless (segment.start == 0);
871 fail_unless (segment.stop == 200);
872 fail_unless (segment.time == 0);
873 fail_unless (segment.base == 0);
874 fail_unless (segment.position == 0);
875 fail_unless (segment.duration == -1);
877 /* invalid time gives invalid result */
878 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
879 fail_unless (result == -1);
881 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
882 fail_unless (result == 0);
884 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
885 fail_unless (result == 100);
887 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
888 fail_unless (result == 150);
890 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
891 fail_unless (result == 200);
893 /* outside of the segment */
894 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
895 fail_unless (result == -1);
897 /***************************************
898 * Normal segment rate 2.0, offset
899 ***************************************/
900 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
901 GST_FORMAT_TIME, 100, 300, 0);
903 fail_unless (segment.base == 100);
905 /* invalid time gives invalid result */
906 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
907 fail_unless (result == -1);
909 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
910 fail_unless (result == 0);
912 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
913 fail_unless (result == 100);
915 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
916 fail_unless (result == 150);
918 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
919 fail_unless (result == 200);
921 /* outside of the segment */
922 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
923 fail_unless (result == -1);
925 /***************************************
926 * Normal segment rate -1.0, offset
927 ***************************************/
929 /* buffers will arrive from 300 to 100 in a sink, stream time
930 * calculation is unaffected by the rate */
931 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
932 GST_FORMAT_TIME, 100, 300, 0);
934 fail_unless (segment.base == 200);
936 /* invalid time gives invalid result */
937 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
938 fail_unless (result == -1);
940 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
941 fail_unless (result == 0);
943 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
944 fail_unless (result == 100);
946 /***********************************************
947 * Normal segment rate -1.0, offset, time = 200
948 ***********************************************/
949 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
950 GST_FORMAT_TIME, 100, 300, 200);
952 /* invalid time gives invalid result */
953 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
954 fail_unless (result == -1);
956 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
957 fail_unless (result == 200);
959 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
960 fail_unless (result == 300);
962 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
963 fail_unless (result == 400);
965 /* outside of the segment */
966 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
967 fail_unless (result == -1);
972 /* mess with the segment structure in the time format */
973 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
978 gst_segment_init (&segment, GST_FORMAT_TIME);
980 /***********************************************************
981 * Normal segment rate 1.0, applied rate -1.0
982 * This means the timestamps represents a stream going backwards
983 * starting from @time to 0.
984 ************************************************************/
985 gst_segment_set_newsegment (&segment, FALSE, 1.0, -1.0,
986 GST_FORMAT_TIME, 0, 200, 200);
988 fail_unless (segment.rate == 1.0);
989 fail_unless (segment.applied_rate == -1.0);
990 fail_unless (segment.format == GST_FORMAT_TIME);
991 fail_unless (segment.flags == 0);
992 fail_unless (segment.start == 0);
993 fail_unless (segment.stop == 200);
994 fail_unless (segment.time == 200);
995 fail_unless (segment.base == 0);
996 fail_unless (segment.position == 0);
997 fail_unless (segment.duration == -1);
999 /* invalid time gives invalid result */
1000 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1001 fail_unless (result == -1);
1003 /* we count backwards from 200 */
1004 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1005 fail_unless (result == 200);
1007 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1008 fail_unless (result == 100);
1010 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1011 fail_unless (result == 50);
1013 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1014 fail_unless (result == 0);
1016 /* outside of the segment */
1017 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1018 fail_unless (result == -1);
1020 /***********************************************************
1021 * Normal segment rate 1.0, applied rate 2.0
1022 * This means the timestamps represents a stream at twice the
1024 ************************************************************/
1025 gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1026 GST_FORMAT_TIME, 0, 200, 0);
1028 fail_unless (segment.rate == 1.0);
1029 fail_unless (segment.applied_rate == 2.0);
1030 fail_unless (segment.format == GST_FORMAT_TIME);
1031 fail_unless (segment.flags == 0);
1032 fail_unless (segment.start == 0);
1033 fail_unless (segment.stop == 200);
1034 fail_unless (segment.time == 0);
1035 fail_unless (segment.base == 200);
1036 fail_unless (segment.position == 0);
1037 fail_unless (segment.duration == -1);
1039 /* invalid time gives invalid result */
1040 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1041 fail_unless (result == -1);
1043 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1044 fail_unless (result == 0);
1046 /* the stream prepresents a stream going twice as fast, the position
1047 * in the segment is therefore scaled by the applied rate */
1048 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1049 fail_unless (result == 200);
1051 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1052 fail_unless (result == 300);
1054 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1055 fail_unless (result == 400);
1057 /* outside of the segment */
1058 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1059 fail_unless (result == -1);
1061 /***********************************************************
1062 * Normal segment rate 1.0, applied rate -2.0
1063 * This means the timestamps represents a stream at twice the
1065 ************************************************************/
1066 gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1067 GST_FORMAT_TIME, 0, 200, 400);
1069 fail_unless (segment.rate == 1.0);
1070 fail_unless (segment.applied_rate == -2.0);
1071 fail_unless (segment.format == GST_FORMAT_TIME);
1072 fail_unless (segment.flags == 0);
1073 fail_unless (segment.start == 0);
1074 fail_unless (segment.stop == 200);
1075 fail_unless (segment.time == 400);
1076 /* previous segment lasted 200, rate of 2.0 was already applied */
1077 fail_unless (segment.base == 400);
1078 fail_unless (segment.position == 0);
1079 fail_unless (segment.duration == -1);
1081 /* invalid time gives invalid result */
1082 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1083 fail_unless (result == -1);
1085 /* we count backwards from 400 */
1086 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1087 fail_unless (result == 400);
1089 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1090 fail_unless (result == 200);
1092 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1093 fail_unless (result == 100);
1095 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1096 fail_unless (result == 0);
1098 /* outside of the segment */
1099 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1100 fail_unless (result == -1);
1102 /***********************************************************
1103 * Normal segment rate 1.0, applied rate -2.0
1104 * This means the timestamps represents a stream at twice the
1105 * reverse rate, start time cannot compensate the complete
1106 * duration of the segment so we stop at 0
1107 ************************************************************/
1108 gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1109 GST_FORMAT_TIME, 0, 200, 200);
1111 fail_unless (segment.rate == 1.0);
1112 fail_unless (segment.applied_rate == -2.0);
1113 fail_unless (segment.format == GST_FORMAT_TIME);
1114 fail_unless (segment.flags == 0);
1115 fail_unless (segment.start == 0);
1116 fail_unless (segment.stop == 200);
1117 fail_unless (segment.time == 200);
1118 fail_unless (segment.base == 600);
1119 fail_unless (segment.position == 0);
1120 fail_unless (segment.duration == -1);
1122 /* invalid time gives invalid result */
1123 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1124 fail_unless (result == -1);
1126 /* we count backwards from 200 */
1127 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1128 fail_unless (result == 200);
1130 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1131 fail_unless (result == 0);
1134 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1135 fail_unless (result == 0);
1137 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1138 fail_unless (result == 0);
1140 /* outside of the segment */
1141 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1142 fail_unless (result == -1);
1147 /* mess with the segment structure in the time format */
1148 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
1153 gst_segment_init (&segment, GST_FORMAT_TIME);
1155 /***********************************************************
1156 * Segment rate 2.0, applied rate 2.0
1157 * this means we have a double speed stream that we should
1158 * speed up by a factor of 2.0 some more. the resulting
1159 * stream will be played at four times the speed.
1160 ************************************************************/
1161 gst_segment_set_newsegment (&segment, FALSE, 2.0, 2.0,
1162 GST_FORMAT_TIME, 0, 200, 0);
1164 fail_unless (segment.rate == 2.0);
1165 fail_unless (segment.applied_rate == 2.0);
1166 fail_unless (segment.format == GST_FORMAT_TIME);
1167 fail_unless (segment.flags == 0);
1168 fail_unless (segment.start == 0);
1169 fail_unless (segment.stop == 200);
1170 fail_unless (segment.time == 0);
1171 fail_unless (segment.base == 0);
1172 fail_unless (segment.position == 0);
1173 fail_unless (segment.duration == -1);
1175 /* invalid time gives invalid result */
1176 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1177 fail_unless (result == -1);
1179 /* only applied rate affects our calculation of the stream time */
1180 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1181 fail_unless (result == 0);
1183 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1184 fail_unless (result == 200);
1186 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1187 fail_unless (result == 300);
1189 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1190 fail_unless (result == 400);
1192 /* outside of the segment */
1193 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1194 fail_unless (result == -1);
1196 /***********************************************************
1197 * Segment rate 2.0, applied rate -1.0
1198 * this means we have a reverse stream that we should
1199 * speed up by a factor of 2.0
1200 ************************************************************/
1201 gst_segment_set_newsegment (&segment, FALSE, 2.0, -1.0,
1202 GST_FORMAT_TIME, 0, 200, 200);
1204 fail_unless (segment.rate == 2.0);
1205 fail_unless (segment.applied_rate == -1.0);
1206 fail_unless (segment.format == GST_FORMAT_TIME);
1207 fail_unless (segment.flags == 0);
1208 fail_unless (segment.start == 0);
1209 fail_unless (segment.stop == 200);
1210 fail_unless (segment.time == 200);
1211 /* previous segment lasted 100 */
1212 fail_unless (segment.base == 100);
1213 fail_unless (segment.position == 0);
1214 fail_unless (segment.duration == -1);
1216 /* invalid time gives invalid result */
1217 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1218 fail_unless (result == -1);
1220 /* only applied rate affects our calculation of the stream time */
1221 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1222 fail_unless (result == 200);
1224 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1225 fail_unless (result == 100);
1227 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1228 fail_unless (result == 50);
1230 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1231 fail_unless (result == 0);
1233 /* outside of the segment */
1234 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1235 fail_unless (result == -1);
1237 /***********************************************************
1238 * Segment rate -1.0, applied rate -1.0
1239 * this means we have a reverse stream that we should
1240 * reverse to get the normal stream again.
1241 ************************************************************/
1242 gst_segment_set_newsegment (&segment, FALSE, -1.0, -1.0,
1243 GST_FORMAT_TIME, 0, 200, 200);
1245 fail_unless (segment.rate == -1.0);
1246 fail_unless (segment.applied_rate == -1.0);
1247 fail_unless (segment.format == GST_FORMAT_TIME);
1248 fail_unless (segment.flags == 0);
1249 fail_unless (segment.start == 0);
1250 fail_unless (segment.stop == 200);
1251 fail_unless (segment.time == 200);
1252 /* accumulated 100 of previous segment to make 200 */
1253 fail_unless (segment.base == 200);
1254 fail_unless (segment.position == 200);
1255 fail_unless (segment.duration == -1);
1257 /* invalid time gives invalid result */
1258 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1259 fail_unless (result == -1);
1261 /* only applied rate affects our calculation of the stream time */
1262 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1263 fail_unless (result == 200);
1265 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1266 fail_unless (result == 100);
1268 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1269 fail_unless (result == 50);
1271 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1272 fail_unless (result == 0);
1274 /* outside of the segment */
1275 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1276 fail_unless (result == -1);
1278 /***********************************************************
1279 * Segment rate -1.0, applied rate -1.0
1280 * this means we have a reverse stream that we should
1281 * reverse to get the normal stream again.
1282 ************************************************************/
1283 gst_segment_set_newsegment (&segment, FALSE, -1.0, 2.0,
1284 GST_FORMAT_TIME, 0, 200, 0);
1286 fail_unless (segment.rate == -1.0);
1287 fail_unless (segment.applied_rate == 2.0);
1288 fail_unless (segment.format == GST_FORMAT_TIME);
1289 fail_unless (segment.flags == 0);
1290 fail_unless (segment.start == 0);
1291 fail_unless (segment.stop == 200);
1292 fail_unless (segment.time == 0);
1293 fail_unless (segment.base == 400);
1294 fail_unless (segment.position == 200);
1295 fail_unless (segment.duration == -1);
1297 /* invalid time gives invalid result */
1298 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1299 fail_unless (result == -1);
1301 /* only applied rate affects our calculation of the stream time */
1302 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1303 fail_unless (result == 0);
1305 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1306 fail_unless (result == 200);
1308 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1309 fail_unless (result == 300);
1311 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1312 fail_unless (result == 400);
1314 /* outside of the segment */
1315 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1316 fail_unless (result == -1);
1321 /* mess with the segment structure in the time format */
1322 GST_START_TEST (segment_newsegment_runningtime)
1327 gst_segment_init (&segment, GST_FORMAT_TIME);
1329 /***************************
1331 ***************************/
1332 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1333 GST_FORMAT_TIME, 0, 200, 0);
1335 fail_unless (segment.rate == 1.0);
1336 fail_unless (segment.applied_rate == 1.0);
1337 fail_unless (segment.format == GST_FORMAT_TIME);
1338 fail_unless (segment.flags == 0);
1339 fail_unless (segment.start == 0);
1340 fail_unless (segment.stop == 200);
1341 fail_unless (segment.time == 0);
1342 fail_unless (segment.base == 0);
1343 fail_unless (segment.position == 0);
1344 fail_unless (segment.duration == -1);
1346 /* invalid time gives invalid result */
1347 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1348 fail_unless (result == -1);
1350 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1351 fail_unless (result == 0);
1352 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1353 fail_unless (result == 0);
1355 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1356 fail_unless (result == 100);
1357 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1358 fail_unless (result == 100);
1360 /* at edge is exactly the segment duration */
1361 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1362 fail_unless (result == 200);
1363 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1364 fail_unless (result == 200);
1366 /* outside of the segment */
1367 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1368 fail_unless (result == -1);
1369 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 300);
1370 fail_unless (result == -1);
1372 /***********************************************************
1373 * time shifted by 500, check if accumulation worked.
1374 * Rate convert to twice the speed which means scaling down
1375 * all positions by 2.0 in this segment.
1376 * Then time argument is not used at all here.
1377 ***********************************************************/
1378 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
1379 GST_FORMAT_TIME, 0, 200, 500);
1381 /* normal speed gives elapsed of 200 */
1382 fail_unless (segment.base == 200);
1384 /* invalid time gives invalid result */
1385 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1386 fail_unless (result == -1);
1388 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1389 fail_unless (result == 200);
1390 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1391 fail_unless (result == 0);
1393 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1394 fail_unless (result == 250);
1395 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1396 fail_unless (result == 100);
1398 /* outside of the segment */
1399 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1400 fail_unless (result == -1);
1401 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 310);
1402 fail_unless (result == -1);
1404 /********************************************
1405 * time offset by 500
1406 * applied rate is not used for running time
1407 ********************************************/
1408 gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1409 GST_FORMAT_TIME, 500, 700, 0);
1411 /* previous segment played at double speed gives elapsed time of
1412 * 100 added to previous accum of 200 gives 300. */
1413 fail_unless (segment.base == 300);
1415 /* invalid time gives invalid result */
1416 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1417 fail_unless (result == -1);
1419 /* before segment is invalid */
1420 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1421 fail_unless (result == -1);
1422 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 200);
1423 fail_unless (result == -1);
1425 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1426 fail_unless (result == 300);
1427 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1428 fail_unless (result == 500);
1430 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1431 fail_unless (result == 400);
1432 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1433 fail_unless (result == 600);
1435 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1436 fail_unless (result == 500);
1437 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1438 fail_unless (result == 700);
1440 /* outside of the segment */
1441 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1442 fail_unless (result == -1);
1443 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1444 fail_unless (result == -1);
1446 /**********************************************************
1447 * time offset by 500, shifted by 200
1448 * Negative rate makes the running time go backwards
1449 * relative to the segment stop position. again time
1451 **********************************************************/
1452 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1453 GST_FORMAT_TIME, 500, 700, 200);
1455 fail_unless (segment.base == 500);
1457 /* invalid time gives invalid result */
1458 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1459 fail_unless (result == -1);
1461 /* before segment is invalid */
1462 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1463 fail_unless (result == -1);
1464 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 400);
1465 fail_unless (result == -1);
1467 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1468 fail_unless (result == 700);
1469 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1470 fail_unless (result == 500);
1472 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1473 fail_unless (result == 600);
1474 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1475 fail_unless (result == 600);
1477 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1478 fail_unless (result == 500);
1479 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1480 fail_unless (result == 700);
1482 /* outside of the segment */
1483 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1484 fail_unless (result == -1);
1485 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1486 fail_unless (result == -1);
1488 /**********************************************************
1489 * time offset by 500, shifted by 200
1490 * Negative rate makes the running time go backwards at
1491 * twice speed relative to the segment stop position. again
1493 **********************************************************/
1494 gst_segment_set_newsegment (&segment, FALSE, -2.0, -2.0,
1495 GST_FORMAT_TIME, 500, 700, 200);
1497 fail_unless (segment.base == 700);
1499 /* invalid time gives invalid result */
1500 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1501 fail_unless (result == -1);
1503 /* before segment is invalid */
1504 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1505 fail_unless (result == -1);
1506 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1507 fail_unless (result == -1);
1509 /* total scaled segment time is 100, accum is 700, so we get 800 */
1510 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1511 fail_unless (result == 800);
1512 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1513 fail_unless (result == 500);
1515 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1516 fail_unless (result == 750);
1517 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1518 fail_unless (result == 600);
1520 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1521 fail_unless (result == 700);
1522 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1523 fail_unless (result == 700);
1525 /* outside of the segment */
1526 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1527 fail_unless (result == -1);
1528 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 900);
1529 fail_unless (result == -1);
1531 /* see if negative rate closed segment correctly */
1532 gst_segment_set_newsegment (&segment, FALSE, -2.0, -1.0,
1533 GST_FORMAT_TIME, 500, 700, 200);
1535 /* previous segment lasted 100, and was at 700 so we should get 800 */
1536 fail_unless (segment.base == 800);
1537 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1538 fail_unless (result == 700);
1543 /* mess with the segment structure in the time format */
1544 GST_START_TEST (segment_newsegment_accum)
1549 gst_segment_init (&segment, GST_FORMAT_TIME);
1551 /***************************
1552 * Normal reverse segment
1553 ***************************/
1554 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1555 GST_FORMAT_TIME, 0, 200, 0);
1557 fail_unless (segment.rate == -1.0);
1558 fail_unless (segment.applied_rate == 1.0);
1559 fail_unless (segment.format == GST_FORMAT_TIME);
1560 fail_unless (segment.flags == 0);
1561 fail_unless (segment.start == 0);
1562 fail_unless (segment.stop == 200);
1563 fail_unless (segment.time == 0);
1564 fail_unless (segment.base == 0);
1565 fail_unless (segment.position == 200);
1566 fail_unless (segment.duration == -1);
1568 /* invalid time gives invalid result */
1569 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1570 fail_unless (result == -1);
1572 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1573 fail_unless (result == 0);
1574 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1575 fail_unless (result == 200);
1577 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1578 fail_unless (result == 50);
1579 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1580 fail_unless (result == 150);
1582 /* update segment, this accumulates 50 from the previous segment. */
1583 gst_segment_set_newsegment (&segment, TRUE, -2.0, 1.0,
1584 GST_FORMAT_TIME, 0, 150, 0);
1586 fail_unless (segment.rate == -2.0);
1587 fail_unless (segment.applied_rate == 1.0);
1588 fail_unless (segment.format == GST_FORMAT_TIME);
1589 fail_unless (segment.flags == 0);
1590 fail_unless (segment.start == 0);
1591 fail_unless (segment.stop == 150);
1592 fail_unless (segment.time == 0);
1593 fail_unless (segment.base == 50);
1594 fail_unless (segment.position == 150);
1595 fail_unless (segment.duration == -1);
1597 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1598 fail_unless (result == 50);
1599 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1600 fail_unless (result == 150);
1602 /* 50 accumulated + 50 / 2 */
1603 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1604 fail_unless (result == 75);
1605 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1606 fail_unless (result == 100);
1608 /* update segment, this does not accumulate anything. */
1609 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
1610 GST_FORMAT_TIME, 100, 200, 100);
1612 fail_unless (segment.rate == 1.0);
1613 fail_unless (segment.applied_rate == 1.0);
1614 fail_unless (segment.format == GST_FORMAT_TIME);
1615 fail_unless (segment.flags == 0);
1616 fail_unless (segment.start == 100);
1617 fail_unless (segment.stop == 200);
1618 fail_unless (segment.time == 100);
1619 fail_unless (segment.base == 50);
1620 fail_unless (segment.position == 150);
1621 fail_unless (segment.duration == -1);
1623 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1624 fail_unless (result == 50);
1625 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1626 fail_unless (result == 100);
1628 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1629 fail_unless (result == 100);
1630 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1631 fail_unless (result == 150);
1636 /* mess with the segment structure in the time format */
1637 GST_START_TEST (segment_newsegment_accum2)
1642 gst_segment_init (&segment, GST_FORMAT_TIME);
1644 /***************************
1645 * Normal reverse segment
1646 ***************************/
1647 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1648 GST_FORMAT_TIME, 0, 200, 0);
1650 fail_unless (segment.rate == -1.0);
1651 fail_unless (segment.applied_rate == 1.0);
1652 fail_unless (segment.format == GST_FORMAT_TIME);
1653 fail_unless (segment.flags == 0);
1654 fail_unless (segment.start == 0);
1655 fail_unless (segment.stop == 200);
1656 fail_unless (segment.time == 0);
1657 fail_unless (segment.base == 0);
1658 fail_unless (segment.position == 200);
1659 fail_unless (segment.duration == -1);
1661 /* invalid time gives invalid result */
1662 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1663 fail_unless (result == -1);
1664 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1665 fail_unless (result == -1);
1667 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1668 fail_unless (result == 0);
1669 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1670 fail_unless (result == 200);
1672 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1673 fail_unless (result == 50);
1674 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1675 fail_unless (result == 150);
1677 /* close segment, this accumulates nothing. */
1678 gst_segment_set_newsegment (&segment, TRUE, -1.0, 1.0,
1679 GST_FORMAT_TIME, 150, 200, 0);
1681 fail_unless (segment.rate == -1.0);
1682 fail_unless (segment.applied_rate == 1.0);
1683 fail_unless (segment.format == GST_FORMAT_TIME);
1684 fail_unless (segment.flags == 0);
1685 fail_unless (segment.start == 150);
1686 fail_unless (segment.stop == 200);
1687 fail_unless (segment.time == 0);
1688 fail_unless (segment.base == 0);
1689 fail_unless (segment.position == 200);
1690 fail_unless (segment.duration == -1);
1692 /* new segment, this accumulates 50. */
1693 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1694 GST_FORMAT_TIME, 150, 300, 150);
1696 fail_unless (segment.rate == 1.0);
1697 fail_unless (segment.applied_rate == 1.0);
1698 fail_unless (segment.format == GST_FORMAT_TIME);
1699 fail_unless (segment.flags == 0);
1700 fail_unless (segment.start == 150);
1701 fail_unless (segment.stop == 300);
1702 fail_unless (segment.time == 150);
1703 fail_unless (segment.base == 50);
1704 fail_unless (segment.position == 150);
1705 fail_unless (segment.duration == -1);
1707 /* invalid time gives invalid result */
1708 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1709 fail_unless (result == -1);
1711 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1712 fail_unless (result == 50);
1713 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1714 fail_unless (result == 150);
1716 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1717 fail_unless (result == 100);
1718 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1719 fail_unless (result == 200);
1725 GST_START_TEST (segment_copy)
1728 GstSegment segment = { 0.0, };
1730 /* this is a boxed type copy function, we support copying NULL */
1731 fail_unless (gst_segment_copy (NULL) == NULL);
1733 gst_segment_init (&segment, GST_FORMAT_TIME);
1735 segment.rate = -1.0;
1736 segment.applied_rate = 1.0;
1741 copy = gst_segment_copy (&segment);
1742 fail_unless (copy != NULL);
1743 /* we inited the struct on the stack to zeroes, so direct comparison should
1744 * be ok here despite the padding field and regardless of implementation */
1745 fail_unless (memcmp (copy, &segment, sizeof (GstSegment)) == 0);
1746 gst_segment_free (copy);
1752 gst_segment_suite (void)
1754 Suite *s = suite_create ("GstSegment");
1755 TCase *tc_chain = tcase_create ("segments");
1757 tcase_set_timeout (tc_chain, 20);
1759 suite_add_tcase (s, tc_chain);
1760 tcase_add_test (tc_chain, segment_seek_nosize);
1761 tcase_add_test (tc_chain, segment_seek_size);
1762 tcase_add_test (tc_chain, segment_seek_reverse);
1763 tcase_add_test (tc_chain, segment_seek_rate);
1765 tcase_add_test (tc_chain, segment_newsegment_open);
1766 tcase_add_test (tc_chain, segment_newsegment_closed);
1767 tcase_add_test (tc_chain, segment_newsegment_streamtime);
1768 tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1769 tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1770 tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1771 tcase_add_test (tc_chain, segment_newsegment_runningtime);
1772 tcase_add_test (tc_chain, segment_newsegment_accum);
1773 tcase_add_test (tc_chain, segment_newsegment_accum2);
1775 tcase_add_test (tc_chain, segment_copy);
1780 GST_CHECK_MAIN (gst_segment);