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);
247 /* configure segment to stop relative, does not update stop
248 * since we did not set it before. */
249 gst_segment_do_seek (&segment, 1.0,
252 GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
253 fail_unless (segment.start == 100);
254 fail_unless (segment.stop == -1);
255 fail_unless (update == FALSE);
258 /* do some clipping on the open range */
259 /* completely outside */
260 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
261 fail_unless (res == FALSE);
263 /* touching lower bound */
264 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
265 fail_unless (res == FALSE);
267 /* partially inside */
268 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
269 fail_unless (res == TRUE);
270 fail_unless (cstart == 100);
271 fail_unless (cstop == 150);
273 /* inside, touching lower bound */
274 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
275 100, 150, &cstart, &cstop);
276 fail_unless (res == TRUE);
277 fail_unless (cstart == 100);
278 fail_unless (cstop == 150);
280 /* completely inside */
281 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
282 150, 200, &cstart, &cstop);
283 fail_unless (res == TRUE);
284 fail_unless (cstart == 150);
285 fail_unless (cstop == 200);
287 /* partially inside, clip to size */
288 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
289 150, 300, &cstart, &cstop);
290 fail_unless (res == TRUE);
291 fail_unless (cstart == 150);
292 fail_unless (cstop == 200);
295 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
296 fail_unless (res == FALSE);
299 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
300 fail_unless (res == TRUE);
301 fail_unless (cstart == 100);
302 fail_unless (cstop == -1);
304 /* start on lower bound */
305 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
306 fail_unless (res == TRUE);
307 fail_unless (cstart == 100);
308 fail_unless (cstop == -1);
311 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
312 fail_unless (res == TRUE);
313 fail_unless (cstart == 150);
314 fail_unless (cstop == -1);
316 /* add 100 to start, set stop to 300, stop clips to 200 */
317 gst_segment_do_seek (&segment, 1.0,
320 GST_SEEK_TYPE_SET, 100 + 100, GST_SEEK_TYPE_SET, 300, &update);
321 fail_unless (segment.start == 200);
322 fail_unless (segment.stop == 200);
324 /* add 100 to start (to 300), set stop to 200, this clips start
326 gst_segment_do_seek (&segment, 1.0,
329 GST_SEEK_TYPE_SET, 200 + 100, GST_SEEK_TYPE_SET, 200, &update);
330 fail_unless (segment.start == 200);
331 fail_unless (segment.stop == 200);
332 fail_unless (update == FALSE);
334 /* seek relative to end */
335 gst_segment_do_seek (&segment, 1.0,
338 GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
339 fail_unless (segment.start == 100);
340 fail_unless (segment.stop == 180);
341 fail_unless (update == TRUE);
343 /* completely outside */
344 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
345 fail_unless (res == FALSE);
347 /* touching lower bound */
348 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
349 fail_unless (res == FALSE);
351 /* partially inside */
352 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
353 fail_unless (res == TRUE);
354 fail_unless (cstart == 100);
355 fail_unless (cstop == 150);
357 /* inside, touching lower bound */
358 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
359 100, 150, &cstart, &cstop);
360 fail_unless (res == TRUE);
361 fail_unless (cstart == 100);
362 fail_unless (cstop == 150);
364 /* completely inside */
365 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
366 150, 170, &cstart, &cstop);
367 fail_unless (res == TRUE);
368 fail_unless (cstart == 150);
369 fail_unless (cstop == 170);
371 /* partially inside */
372 res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
373 150, 250, &cstart, &cstop);
374 fail_unless (res == TRUE);
375 fail_unless (cstart == 150);
376 fail_unless (cstop == 180);
379 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
380 fail_unless (res == FALSE);
383 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
384 fail_unless (res == TRUE);
385 fail_unless (cstart == 100);
386 fail_unless (cstop == 180);
388 /* start on lower bound */
389 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
390 fail_unless (res == TRUE);
391 fail_unless (cstart == 100);
392 fail_unless (cstop == 180);
395 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
396 fail_unless (res == TRUE);
397 fail_unless (cstart == 150);
398 fail_unless (cstop == 180);
400 /* start outside on boundary */
401 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
402 fail_unless (res == FALSE);
404 /* start completely outside */
405 res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
406 fail_unless (res == FALSE);
411 GST_START_TEST (segment_seek_reverse)
416 gst_segment_init (&segment, GST_FORMAT_BYTES);
417 segment.duration = 200;
419 /* configure segment to stop 100 */
420 gst_segment_do_seek (&segment, -1.0,
423 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 100, &update);
424 fail_unless (segment.start == 0);
425 fail_unless (segment.stop == 100);
426 fail_unless (segment.time == 0);
427 fail_unless (segment.position == 100);
428 fail_unless (update == TRUE);
431 gst_segment_do_seek (&segment, -1.0,
434 GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_SET, 100 - 20, &update);
435 fail_unless (segment.start == 10);
436 fail_unless (segment.stop == 80);
437 fail_unless (segment.time == 10);
438 fail_unless (segment.position == 80);
439 fail_unless (update == TRUE);
441 gst_segment_do_seek (&segment, -1.0,
444 GST_SEEK_TYPE_SET, 20, GST_SEEK_TYPE_NONE, 0, &update);
445 fail_unless (segment.start == 20);
446 fail_unless (segment.stop == 80);
447 fail_unless (segment.time == 20);
448 fail_unless (segment.position == 80);
449 fail_unless (update == FALSE);
454 /* mess with the segment structure in the bytes format */
455 GST_START_TEST (segment_seek_rate)
460 gst_segment_init (&segment, GST_FORMAT_BYTES);
462 /* configure segment to rate 2.0 */
463 gst_segment_do_seek (&segment, 2.0,
466 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1, &update);
467 fail_unless (segment.format == GST_FORMAT_BYTES);
468 fail_unless (segment.start == 0);
469 fail_unless (segment.stop == -1);
470 fail_unless (segment.rate == 2.0);
471 fail_unless (update == FALSE);
474 /* 0 is the same in all formats and should not fail */
475 gst_segment_do_seek (&segment, 2.0,
476 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
477 GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1, &update);
478 fail_unless (segment.format == GST_FORMAT_BYTES);
480 /* set to -1 means start from 0 */
481 gst_segment_do_seek (&segment, 2.0,
482 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
483 GST_SEEK_TYPE_SET, -1, GST_SEEK_TYPE_NONE, -1, &update);
484 fail_unless (segment.format == GST_FORMAT_BYTES);
485 fail_unless (segment.start == 0);
487 gst_segment_do_seek (&segment, 2.0,
488 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
489 GST_SEEK_TYPE_CUR, 0, GST_SEEK_TYPE_NONE, -1, &update);
491 gst_segment_do_seek (&segment, 2.0,
492 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
493 GST_SEEK_TYPE_END, 0, GST_SEEK_TYPE_NONE, -1, &update);
495 /* -1 for end is fine too in all formats */
496 gst_segment_do_seek (&segment, 2.0,
497 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
498 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
500 /* 0 as relative end is fine too */
501 gst_segment_do_seek (&segment, 2.0,
502 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
503 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
505 gst_segment_do_seek (&segment, 2.0,
506 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
507 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
510 /* set a real stop position, this must happen in bytes */
511 gst_segment_do_seek (&segment, 3.0,
514 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, 100, &update);
515 fail_unless (segment.format == GST_FORMAT_BYTES);
516 fail_unless (segment.start == 0);
517 fail_unless (segment.stop == 100);
518 fail_unless (segment.rate == 3.0);
519 /* no seek should happen, we just updated the stop position in forward
521 fail_unless (update == FALSE);
524 /* 0 as relative end is fine too */
525 gst_segment_do_seek (&segment, 2.0,
526 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
527 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
528 fail_unless (segment.stop == 100);
530 gst_segment_do_seek (&segment, 2.0,
531 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
532 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
533 fail_unless (segment.stop == 100);
535 /* -1 for end is fine too in all formats */
536 gst_segment_do_seek (&segment, 2.0,
537 GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
538 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
539 fail_unless (segment.stop == -1);
542 /* set some duration, stop -1 END seeks will now work with the
543 * duration, if the formats match */
544 segment.duration = 200;
545 fail_unless (segment.duration == 200);
547 /* seek to end with 0 should set the stop to the duration */
548 gst_segment_do_seek (&segment, 2.0,
549 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
550 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
551 fail_unless (segment.stop == 200);
552 fail_unless (segment.duration == 200);
554 /* subtract 100 from the end */
555 gst_segment_do_seek (&segment, 2.0,
556 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
557 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, -100, &update);
558 fail_unless (segment.stop == 100);
559 fail_unless (segment.duration == 200);
561 /* add 100 to the duration, this should be clamped to the duration */
562 gst_segment_do_seek (&segment, 2.0,
563 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
564 GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 100, &update);
565 fail_unless (segment.stop == 200);
566 fail_unless (segment.duration == 200);
569 /* add 300 to the start, this should be clamped to the duration */
570 gst_segment_do_seek (&segment, 2.0,
571 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
572 GST_SEEK_TYPE_CUR, 300, GST_SEEK_TYPE_END, 0, &update);
573 fail_unless (segment.start == 200);
574 fail_unless (segment.stop == 200);
575 fail_unless (segment.duration == 200);
577 /* subtract 300 from the start, this should be clamped to 0 */
578 gst_segment_do_seek (&segment, 2.0,
579 GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
580 GST_SEEK_TYPE_CUR, -300, GST_SEEK_TYPE_END, 0, &update);
581 GST_DEBUG ("%" G_GINT64_FORMAT, segment.start);
582 fail_unless (segment.start == 0);
583 fail_unless (segment.stop == 200);
584 fail_unless (segment.duration == 200);
591 /* mess with the segment structure in the bytes format */
592 GST_START_TEST (segment_newsegment_open)
596 gst_segment_init (&segment, GST_FORMAT_BYTES);
598 /* time should also work for starting from 0 */
599 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0, -1,
602 fail_unless (segment.rate == 1.0);
603 fail_unless (segment.format == GST_FORMAT_BYTES);
604 fail_unless (segment.flags == 0);
605 fail_unless (segment.start == 0);
606 fail_unless (segment.stop == -1);
607 fail_unless (segment.time == 0);
608 fail_unless (segment.base == 0);
609 fail_unless (segment.position == 0);
610 fail_unless (segment.duration == -1);
612 /* we set stop but in the wrong format, stop stays open. */
613 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0,
616 fail_unless (segment.start == 0);
617 fail_unless (segment.stop == -1);
618 fail_unless (segment.time == 0);
619 fail_unless (segment.base == 0);
620 fail_unless (segment.position == 0);
622 /* update, nothing changes */
623 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 0, -1,
626 fail_unless (segment.start == 0);
627 fail_unless (segment.stop == -1);
628 fail_unless (segment.time == 0);
629 fail_unless (segment.base == 0);
630 fail_unless (segment.position == 0);
633 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
634 GST_FORMAT_BYTES, 100, -1, 100);
636 fail_unless (segment.start == 100);
637 fail_unless (segment.stop == -1);
638 fail_unless (segment.time == 100);
639 fail_unless (segment.base == 100);
640 fail_unless (segment.position == 100);
642 /* last_stop 0, base does not change */
643 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
646 fail_unless (segment.start == 0);
647 fail_unless (segment.stop == -1);
648 fail_unless (segment.time == 0);
649 fail_unless (segment.base == 100);
651 gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
653 fail_unless (segment.position == 200);
655 /* last_stop 200, base changes */
656 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
659 fail_unless (segment.start == 0);
660 fail_unless (segment.stop == -1);
661 fail_unless (segment.time == 0);
662 fail_unless (segment.base == 300);
663 fail_unless (segment.position == 0);
669 /* mess with the segment structure in the bytes format */
670 GST_START_TEST (segment_newsegment_closed)
674 gst_segment_init (&segment, GST_FORMAT_BYTES);
676 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
677 GST_FORMAT_BYTES, 0, 200, 0);
679 fail_unless (segment.rate == 1.0);
680 fail_unless (segment.format == GST_FORMAT_BYTES);
681 fail_unless (segment.flags == 0);
682 fail_unless (segment.start == 0);
683 fail_unless (segment.stop == 200);
684 fail_unless (segment.time == 0);
685 fail_unless (segment.base == 0);
686 fail_unless (segment.position == 0);
687 fail_unless (segment.duration == -1);
689 /* assume we advanced to position 40 */
690 gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 40);
691 fail_unless (segment.position == 40);
693 /* do an update to the start, last_stop is unchanged because it's bigger */
694 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 20,
697 fail_unless (segment.start == 20);
698 fail_unless (segment.stop == 200);
699 fail_unless (segment.time == 20);
700 fail_unless (segment.base == 20);
701 fail_unless (segment.position == 40);
703 /* do an update past our last_stop, it should be updated now */
704 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 50,
707 fail_unless (segment.start == 50);
708 fail_unless (segment.stop == 300);
709 fail_unless (segment.time == 50);
710 fail_unless (segment.base == 50);
711 fail_unless (segment.position == 50);
713 /* and a new accumulated one */
714 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
715 GST_FORMAT_BYTES, 100, 400, 300);
717 fail_unless (segment.start == 100);
718 fail_unless (segment.stop == 400);
719 fail_unless (segment.time == 300);
720 fail_unless (segment.base == 300);
722 /* and a new updated one */
723 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
724 GST_FORMAT_BYTES, 100, 500, 300);
726 fail_unless (segment.start == 100);
727 fail_unless (segment.stop == 500);
728 fail_unless (segment.time == 300);
729 fail_unless (segment.base == 300);
731 /* and a new partially updated one */
732 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
733 GST_FORMAT_BYTES, 200, 500, 400);
735 fail_unless (segment.start == 200);
736 fail_unless (segment.stop == 500);
737 fail_unless (segment.time == 400);
738 fail_unless (segment.base == 400);
743 /* mess with the segment structure in the time format */
744 GST_START_TEST (segment_newsegment_streamtime)
749 gst_segment_init (&segment, GST_FORMAT_TIME);
751 /***************************
753 ***************************/
754 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
755 GST_FORMAT_TIME, 0, 200, 0);
757 fail_unless (segment.rate == 1.0);
758 fail_unless (segment.applied_rate == 1.0);
759 fail_unless (segment.format == GST_FORMAT_TIME);
760 fail_unless (segment.flags == 0);
761 fail_unless (segment.start == 0);
762 fail_unless (segment.stop == 200);
763 fail_unless (segment.time == 0);
764 fail_unless (segment.base == 0);
765 fail_unless (segment.position == 0);
766 fail_unless (segment.duration == -1);
768 /* invalid time gives invalid result */
769 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
770 fail_unless (result == -1);
772 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
773 fail_unless (result == 0);
775 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
776 fail_unless (result == 100);
778 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
779 fail_unless (result == 200);
781 /* outside of the segment */
782 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
783 fail_unless (result == -1);
785 /*********************
786 * time shifted by 500
787 *********************/
788 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
789 GST_FORMAT_TIME, 0, 200, 500);
791 fail_unless (segment.base == 200);
793 /* invalid time gives invalid result */
794 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
795 fail_unless (result == -1);
797 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
798 fail_unless (result == 500);
800 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
801 fail_unless (result == 600);
803 /* outside of the segment */
804 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
805 fail_unless (result == -1);
807 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
808 fail_unless (result == -1);
810 /*********************
812 *********************/
813 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
814 GST_FORMAT_TIME, 500, 700, 0);
816 fail_unless (segment.base == 400);
818 /* invalid time gives invalid result */
819 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
820 fail_unless (result == -1);
822 /* before segment is invalid */
823 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
824 fail_unless (result == -1);
826 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
827 fail_unless (result == 0);
829 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
830 fail_unless (result == 100);
832 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
833 fail_unless (result == 200);
835 /* outside of the segment */
836 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
837 fail_unless (result == -1);
839 /*************************************
840 * time offset by 500, shifted by 200
841 *************************************/
842 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
843 GST_FORMAT_TIME, 500, 700, 200);
845 fail_unless (segment.base == 600);
847 /* invalid time gives invalid result */
848 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
849 fail_unless (result == -1);
851 /* before segment is invalid */
852 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
853 fail_unless (result == -1);
855 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
856 fail_unless (result == 200);
858 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
859 fail_unless (result == 300);
861 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
862 fail_unless (result == 400);
864 /* outside of the segment */
865 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
866 fail_unless (result == -1);
871 /* mess with the segment structure in the time format */
872 GST_START_TEST (segment_newsegment_streamtime_rate)
877 gst_segment_init (&segment, GST_FORMAT_TIME);
879 /***************************
880 * Normal segment rate 2.0
881 ***************************/
882 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
883 GST_FORMAT_TIME, 0, 200, 0);
885 fail_unless (segment.rate == 2.0);
886 fail_unless (segment.applied_rate == 1.0);
887 fail_unless (segment.format == GST_FORMAT_TIME);
888 fail_unless (segment.flags == 0);
889 fail_unless (segment.start == 0);
890 fail_unless (segment.stop == 200);
891 fail_unless (segment.time == 0);
892 fail_unless (segment.base == 0);
893 fail_unless (segment.position == 0);
894 fail_unless (segment.duration == -1);
896 /* invalid time gives invalid result */
897 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
898 fail_unless (result == -1);
900 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
901 fail_unless (result == 0);
903 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
904 fail_unless (result == 100);
906 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
907 fail_unless (result == 150);
909 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
910 fail_unless (result == 200);
912 /* outside of the segment */
913 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
914 fail_unless (result == -1);
916 /***************************************
917 * Normal segment rate 2.0, offset
918 ***************************************/
919 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
920 GST_FORMAT_TIME, 100, 300, 0);
922 fail_unless (segment.base == 100);
924 /* invalid time gives invalid result */
925 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
926 fail_unless (result == -1);
928 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
929 fail_unless (result == 0);
931 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
932 fail_unless (result == 100);
934 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
935 fail_unless (result == 150);
937 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
938 fail_unless (result == 200);
940 /* outside of the segment */
941 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
942 fail_unless (result == -1);
944 /***************************************
945 * Normal segment rate -1.0, offset
946 ***************************************/
948 /* buffers will arrive from 300 to 100 in a sink, stream time
949 * calculation is unaffected by the rate */
950 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
951 GST_FORMAT_TIME, 100, 300, 0);
953 fail_unless (segment.base == 200);
955 /* invalid time gives invalid result */
956 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
957 fail_unless (result == -1);
959 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
960 fail_unless (result == 0);
962 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
963 fail_unless (result == 100);
965 /***********************************************
966 * Normal segment rate -1.0, offset, time = 200
967 ***********************************************/
968 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
969 GST_FORMAT_TIME, 100, 300, 200);
971 /* invalid time gives invalid result */
972 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
973 fail_unless (result == -1);
975 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
976 fail_unless (result == 200);
978 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
979 fail_unless (result == 300);
981 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
982 fail_unless (result == 400);
984 /* outside of the segment */
985 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
986 fail_unless (result == -1);
991 /* mess with the segment structure in the time format */
992 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
997 gst_segment_init (&segment, GST_FORMAT_TIME);
999 /***********************************************************
1000 * Normal segment rate 1.0, applied rate -1.0
1001 * This means the timestamps represents a stream going backwards
1002 * starting from @time to 0.
1003 ************************************************************/
1004 gst_segment_set_newsegment (&segment, FALSE, 1.0, -1.0,
1005 GST_FORMAT_TIME, 0, 200, 200);
1007 fail_unless (segment.rate == 1.0);
1008 fail_unless (segment.applied_rate == -1.0);
1009 fail_unless (segment.format == GST_FORMAT_TIME);
1010 fail_unless (segment.flags == 0);
1011 fail_unless (segment.start == 0);
1012 fail_unless (segment.stop == 200);
1013 fail_unless (segment.time == 200);
1014 fail_unless (segment.base == 0);
1015 fail_unless (segment.position == 0);
1016 fail_unless (segment.duration == -1);
1018 /* invalid time gives invalid result */
1019 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1020 fail_unless (result == -1);
1022 /* we count backwards from 200 */
1023 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1024 fail_unless (result == 200);
1026 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1027 fail_unless (result == 100);
1029 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1030 fail_unless (result == 50);
1032 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1033 fail_unless (result == 0);
1035 /* outside of the segment */
1036 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1037 fail_unless (result == -1);
1039 /***********************************************************
1040 * Normal segment rate 1.0, applied rate 2.0
1041 * This means the timestamps represents a stream at twice the
1043 ************************************************************/
1044 gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1045 GST_FORMAT_TIME, 0, 200, 0);
1047 fail_unless (segment.rate == 1.0);
1048 fail_unless (segment.applied_rate == 2.0);
1049 fail_unless (segment.format == GST_FORMAT_TIME);
1050 fail_unless (segment.flags == 0);
1051 fail_unless (segment.start == 0);
1052 fail_unless (segment.stop == 200);
1053 fail_unless (segment.time == 0);
1054 fail_unless (segment.base == 200);
1055 fail_unless (segment.position == 0);
1056 fail_unless (segment.duration == -1);
1058 /* invalid time gives invalid result */
1059 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1060 fail_unless (result == -1);
1062 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1063 fail_unless (result == 0);
1065 /* the stream prepresents a stream going twice as fast, the position
1066 * in the segment is therefore scaled by the applied rate */
1067 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1068 fail_unless (result == 200);
1070 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1071 fail_unless (result == 300);
1073 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1074 fail_unless (result == 400);
1076 /* outside of the segment */
1077 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1078 fail_unless (result == -1);
1080 /***********************************************************
1081 * Normal segment rate 1.0, applied rate -2.0
1082 * This means the timestamps represents a stream at twice the
1084 ************************************************************/
1085 gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1086 GST_FORMAT_TIME, 0, 200, 400);
1088 fail_unless (segment.rate == 1.0);
1089 fail_unless (segment.applied_rate == -2.0);
1090 fail_unless (segment.format == GST_FORMAT_TIME);
1091 fail_unless (segment.flags == 0);
1092 fail_unless (segment.start == 0);
1093 fail_unless (segment.stop == 200);
1094 fail_unless (segment.time == 400);
1095 /* previous segment lasted 200, rate of 2.0 was already applied */
1096 fail_unless (segment.base == 400);
1097 fail_unless (segment.position == 0);
1098 fail_unless (segment.duration == -1);
1100 /* invalid time gives invalid result */
1101 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1102 fail_unless (result == -1);
1104 /* we count backwards from 400 */
1105 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1106 fail_unless (result == 400);
1108 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1109 fail_unless (result == 200);
1111 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1112 fail_unless (result == 100);
1114 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1115 fail_unless (result == 0);
1117 /* outside of the segment */
1118 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1119 fail_unless (result == -1);
1121 /***********************************************************
1122 * Normal segment rate 1.0, applied rate -2.0
1123 * This means the timestamps represents a stream at twice the
1124 * reverse rate, start time cannot compensate the complete
1125 * duration of the segment so we stop at 0
1126 ************************************************************/
1127 gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1128 GST_FORMAT_TIME, 0, 200, 200);
1130 fail_unless (segment.rate == 1.0);
1131 fail_unless (segment.applied_rate == -2.0);
1132 fail_unless (segment.format == GST_FORMAT_TIME);
1133 fail_unless (segment.flags == 0);
1134 fail_unless (segment.start == 0);
1135 fail_unless (segment.stop == 200);
1136 fail_unless (segment.time == 200);
1137 fail_unless (segment.base == 600);
1138 fail_unless (segment.position == 0);
1139 fail_unless (segment.duration == -1);
1141 /* invalid time gives invalid result */
1142 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1143 fail_unless (result == -1);
1145 /* we count backwards from 200 */
1146 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1147 fail_unless (result == 200);
1149 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1150 fail_unless (result == 0);
1153 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1154 fail_unless (result == 0);
1156 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1157 fail_unless (result == 0);
1159 /* outside of the segment */
1160 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1161 fail_unless (result == -1);
1166 /* mess with the segment structure in the time format */
1167 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
1172 gst_segment_init (&segment, GST_FORMAT_TIME);
1174 /***********************************************************
1175 * Segment rate 2.0, applied rate 2.0
1176 * this means we have a double speed stream that we should
1177 * speed up by a factor of 2.0 some more. the resulting
1178 * stream will be played at four times the speed.
1179 ************************************************************/
1180 gst_segment_set_newsegment (&segment, FALSE, 2.0, 2.0,
1181 GST_FORMAT_TIME, 0, 200, 0);
1183 fail_unless (segment.rate == 2.0);
1184 fail_unless (segment.applied_rate == 2.0);
1185 fail_unless (segment.format == GST_FORMAT_TIME);
1186 fail_unless (segment.flags == 0);
1187 fail_unless (segment.start == 0);
1188 fail_unless (segment.stop == 200);
1189 fail_unless (segment.time == 0);
1190 fail_unless (segment.base == 0);
1191 fail_unless (segment.position == 0);
1192 fail_unless (segment.duration == -1);
1194 /* invalid time gives invalid result */
1195 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1196 fail_unless (result == -1);
1198 /* only applied rate affects our calculation of the stream time */
1199 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1200 fail_unless (result == 0);
1202 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1203 fail_unless (result == 200);
1205 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1206 fail_unless (result == 300);
1208 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1209 fail_unless (result == 400);
1211 /* outside of the segment */
1212 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1213 fail_unless (result == -1);
1215 /***********************************************************
1216 * Segment rate 2.0, applied rate -1.0
1217 * this means we have a reverse stream that we should
1218 * speed up by a factor of 2.0
1219 ************************************************************/
1220 gst_segment_set_newsegment (&segment, FALSE, 2.0, -1.0,
1221 GST_FORMAT_TIME, 0, 200, 200);
1223 fail_unless (segment.rate == 2.0);
1224 fail_unless (segment.applied_rate == -1.0);
1225 fail_unless (segment.format == GST_FORMAT_TIME);
1226 fail_unless (segment.flags == 0);
1227 fail_unless (segment.start == 0);
1228 fail_unless (segment.stop == 200);
1229 fail_unless (segment.time == 200);
1230 /* previous segment lasted 100 */
1231 fail_unless (segment.base == 100);
1232 fail_unless (segment.position == 0);
1233 fail_unless (segment.duration == -1);
1235 /* invalid time gives invalid result */
1236 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1237 fail_unless (result == -1);
1239 /* only applied rate affects our calculation of the stream time */
1240 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1241 fail_unless (result == 200);
1243 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1244 fail_unless (result == 100);
1246 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1247 fail_unless (result == 50);
1249 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1250 fail_unless (result == 0);
1252 /* outside of the segment */
1253 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1254 fail_unless (result == -1);
1256 /***********************************************************
1257 * Segment rate -1.0, applied rate -1.0
1258 * this means we have a reverse stream that we should
1259 * reverse to get the normal stream again.
1260 ************************************************************/
1261 gst_segment_set_newsegment (&segment, FALSE, -1.0, -1.0,
1262 GST_FORMAT_TIME, 0, 200, 200);
1264 fail_unless (segment.rate == -1.0);
1265 fail_unless (segment.applied_rate == -1.0);
1266 fail_unless (segment.format == GST_FORMAT_TIME);
1267 fail_unless (segment.flags == 0);
1268 fail_unless (segment.start == 0);
1269 fail_unless (segment.stop == 200);
1270 fail_unless (segment.time == 200);
1271 /* accumulated 100 of previous segment to make 200 */
1272 fail_unless (segment.base == 200);
1273 fail_unless (segment.position == 200);
1274 fail_unless (segment.duration == -1);
1276 /* invalid time gives invalid result */
1277 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1278 fail_unless (result == -1);
1280 /* only applied rate affects our calculation of the stream time */
1281 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1282 fail_unless (result == 200);
1284 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1285 fail_unless (result == 100);
1287 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1288 fail_unless (result == 50);
1290 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1291 fail_unless (result == 0);
1293 /* outside of the segment */
1294 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1295 fail_unless (result == -1);
1297 /***********************************************************
1298 * Segment rate -1.0, applied rate -1.0
1299 * this means we have a reverse stream that we should
1300 * reverse to get the normal stream again.
1301 ************************************************************/
1302 gst_segment_set_newsegment (&segment, FALSE, -1.0, 2.0,
1303 GST_FORMAT_TIME, 0, 200, 0);
1305 fail_unless (segment.rate == -1.0);
1306 fail_unless (segment.applied_rate == 2.0);
1307 fail_unless (segment.format == GST_FORMAT_TIME);
1308 fail_unless (segment.flags == 0);
1309 fail_unless (segment.start == 0);
1310 fail_unless (segment.stop == 200);
1311 fail_unless (segment.time == 0);
1312 fail_unless (segment.base == 400);
1313 fail_unless (segment.position == 200);
1314 fail_unless (segment.duration == -1);
1316 /* invalid time gives invalid result */
1317 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1318 fail_unless (result == -1);
1320 /* only applied rate affects our calculation of the stream time */
1321 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1322 fail_unless (result == 0);
1324 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1325 fail_unless (result == 200);
1327 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1328 fail_unless (result == 300);
1330 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1331 fail_unless (result == 400);
1333 /* outside of the segment */
1334 result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1335 fail_unless (result == -1);
1340 /* mess with the segment structure in the time format */
1341 GST_START_TEST (segment_newsegment_runningtime)
1346 gst_segment_init (&segment, GST_FORMAT_TIME);
1348 /***************************
1350 ***************************/
1351 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1352 GST_FORMAT_TIME, 0, 200, 0);
1354 fail_unless (segment.rate == 1.0);
1355 fail_unless (segment.applied_rate == 1.0);
1356 fail_unless (segment.format == GST_FORMAT_TIME);
1357 fail_unless (segment.flags == 0);
1358 fail_unless (segment.start == 0);
1359 fail_unless (segment.stop == 200);
1360 fail_unless (segment.time == 0);
1361 fail_unless (segment.base == 0);
1362 fail_unless (segment.position == 0);
1363 fail_unless (segment.duration == -1);
1365 /* invalid time gives invalid result */
1366 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1367 fail_unless (result == -1);
1369 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1370 fail_unless (result == 0);
1371 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1372 fail_unless (result == 0);
1374 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1375 fail_unless (result == 100);
1376 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1377 fail_unless (result == 100);
1379 /* at edge is exactly the segment duration */
1380 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1381 fail_unless (result == 200);
1382 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1383 fail_unless (result == 200);
1385 /* outside of the segment */
1386 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1387 fail_unless (result == -1);
1388 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 300);
1389 fail_unless (result == -1);
1391 /***********************************************************
1392 * time shifted by 500, check if accumulation worked.
1393 * Rate convert to twice the speed which means scaling down
1394 * all positions by 2.0 in this segment.
1395 * Then time argument is not used at all here.
1396 ***********************************************************/
1397 gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
1398 GST_FORMAT_TIME, 0, 200, 500);
1400 /* normal speed gives elapsed of 200 */
1401 fail_unless (segment.base == 200);
1403 /* invalid time gives invalid result */
1404 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1405 fail_unless (result == -1);
1407 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1408 fail_unless (result == 200);
1409 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1410 fail_unless (result == 0);
1412 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1413 fail_unless (result == 250);
1414 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1415 fail_unless (result == 100);
1417 /* outside of the segment */
1418 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1419 fail_unless (result == -1);
1420 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 310);
1421 fail_unless (result == -1);
1423 /********************************************
1424 * time offset by 500
1425 * applied rate is not used for running time
1426 ********************************************/
1427 gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1428 GST_FORMAT_TIME, 500, 700, 0);
1430 /* previous segment played at double speed gives elapsed time of
1431 * 100 added to previous accum of 200 gives 300. */
1432 fail_unless (segment.base == 300);
1434 /* invalid time gives invalid result */
1435 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1436 fail_unless (result == -1);
1438 /* before segment is invalid */
1439 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1440 fail_unless (result == -1);
1441 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 200);
1442 fail_unless (result == -1);
1444 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1445 fail_unless (result == 300);
1446 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1447 fail_unless (result == 500);
1449 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1450 fail_unless (result == 400);
1451 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1452 fail_unless (result == 600);
1454 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1455 fail_unless (result == 500);
1456 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1457 fail_unless (result == 700);
1459 /* outside of the segment */
1460 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1461 fail_unless (result == -1);
1462 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1463 fail_unless (result == -1);
1465 /**********************************************************
1466 * time offset by 500, shifted by 200
1467 * Negative rate makes the running time go backwards
1468 * relative to the segment stop position. again time
1470 **********************************************************/
1471 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1472 GST_FORMAT_TIME, 500, 700, 200);
1474 fail_unless (segment.base == 500);
1476 /* invalid time gives invalid result */
1477 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1478 fail_unless (result == -1);
1480 /* before segment is invalid */
1481 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1482 fail_unless (result == -1);
1483 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 400);
1484 fail_unless (result == -1);
1486 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1487 fail_unless (result == 700);
1488 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1489 fail_unless (result == 500);
1491 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1492 fail_unless (result == 600);
1493 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1494 fail_unless (result == 600);
1496 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1497 fail_unless (result == 500);
1498 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1499 fail_unless (result == 700);
1501 /* outside of the segment */
1502 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1503 fail_unless (result == -1);
1504 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1505 fail_unless (result == -1);
1507 /**********************************************************
1508 * time offset by 500, shifted by 200
1509 * Negative rate makes the running time go backwards at
1510 * twice speed relative to the segment stop position. again
1512 **********************************************************/
1513 gst_segment_set_newsegment (&segment, FALSE, -2.0, -2.0,
1514 GST_FORMAT_TIME, 500, 700, 200);
1516 fail_unless (segment.base == 700);
1518 /* invalid time gives invalid result */
1519 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1520 fail_unless (result == -1);
1522 /* before segment is invalid */
1523 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1524 fail_unless (result == -1);
1525 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1526 fail_unless (result == -1);
1528 /* total scaled segment time is 100, accum is 700, so we get 800 */
1529 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1530 fail_unless (result == 800);
1531 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1532 fail_unless (result == 500);
1534 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1535 fail_unless (result == 750);
1536 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1537 fail_unless (result == 600);
1539 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1540 fail_unless (result == 700);
1541 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1542 fail_unless (result == 700);
1544 /* outside of the segment */
1545 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1546 fail_unless (result == -1);
1547 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 900);
1548 fail_unless (result == -1);
1550 /* see if negative rate closed segment correctly */
1551 gst_segment_set_newsegment (&segment, FALSE, -2.0, -1.0,
1552 GST_FORMAT_TIME, 500, 700, 200);
1554 /* previous segment lasted 100, and was at 700 so we should get 800 */
1555 fail_unless (segment.base == 800);
1556 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1557 fail_unless (result == 700);
1562 /* mess with the segment structure in the time format */
1563 GST_START_TEST (segment_newsegment_accum)
1568 gst_segment_init (&segment, GST_FORMAT_TIME);
1570 /***************************
1571 * Normal reverse segment
1572 ***************************/
1573 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1574 GST_FORMAT_TIME, 0, 200, 0);
1576 fail_unless (segment.rate == -1.0);
1577 fail_unless (segment.applied_rate == 1.0);
1578 fail_unless (segment.format == GST_FORMAT_TIME);
1579 fail_unless (segment.flags == 0);
1580 fail_unless (segment.start == 0);
1581 fail_unless (segment.stop == 200);
1582 fail_unless (segment.time == 0);
1583 fail_unless (segment.base == 0);
1584 fail_unless (segment.position == 200);
1585 fail_unless (segment.duration == -1);
1587 /* invalid time gives invalid result */
1588 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1589 fail_unless (result == -1);
1591 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1592 fail_unless (result == 0);
1593 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1594 fail_unless (result == 200);
1596 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1597 fail_unless (result == 50);
1598 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1599 fail_unless (result == 150);
1601 /* update segment, this accumulates 50 from the previous segment. */
1602 gst_segment_set_newsegment (&segment, TRUE, -2.0, 1.0,
1603 GST_FORMAT_TIME, 0, 150, 0);
1605 fail_unless (segment.rate == -2.0);
1606 fail_unless (segment.applied_rate == 1.0);
1607 fail_unless (segment.format == GST_FORMAT_TIME);
1608 fail_unless (segment.flags == 0);
1609 fail_unless (segment.start == 0);
1610 fail_unless (segment.stop == 150);
1611 fail_unless (segment.time == 0);
1612 fail_unless (segment.base == 50);
1613 fail_unless (segment.position == 150);
1614 fail_unless (segment.duration == -1);
1616 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1617 fail_unless (result == 50);
1618 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1619 fail_unless (result == 150);
1621 /* 50 accumulated + 50 / 2 */
1622 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1623 fail_unless (result == 75);
1624 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1625 fail_unless (result == 100);
1627 /* update segment, this does not accumulate anything. */
1628 gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
1629 GST_FORMAT_TIME, 100, 200, 100);
1631 fail_unless (segment.rate == 1.0);
1632 fail_unless (segment.applied_rate == 1.0);
1633 fail_unless (segment.format == GST_FORMAT_TIME);
1634 fail_unless (segment.flags == 0);
1635 fail_unless (segment.start == 100);
1636 fail_unless (segment.stop == 200);
1637 fail_unless (segment.time == 100);
1638 fail_unless (segment.base == 50);
1639 fail_unless (segment.position == 150);
1640 fail_unless (segment.duration == -1);
1642 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1643 fail_unless (result == 50);
1644 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1645 fail_unless (result == 100);
1647 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1648 fail_unless (result == 100);
1649 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1650 fail_unless (result == 150);
1655 /* mess with the segment structure in the time format */
1656 GST_START_TEST (segment_newsegment_accum2)
1661 gst_segment_init (&segment, GST_FORMAT_TIME);
1663 /***************************
1664 * Normal reverse segment
1665 ***************************/
1666 gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1667 GST_FORMAT_TIME, 0, 200, 0);
1669 fail_unless (segment.rate == -1.0);
1670 fail_unless (segment.applied_rate == 1.0);
1671 fail_unless (segment.format == GST_FORMAT_TIME);
1672 fail_unless (segment.flags == 0);
1673 fail_unless (segment.start == 0);
1674 fail_unless (segment.stop == 200);
1675 fail_unless (segment.time == 0);
1676 fail_unless (segment.base == 0);
1677 fail_unless (segment.position == 200);
1678 fail_unless (segment.duration == -1);
1680 /* invalid time gives invalid result */
1681 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1682 fail_unless (result == -1);
1683 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1684 fail_unless (result == -1);
1686 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1687 fail_unless (result == 0);
1688 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1689 fail_unless (result == 200);
1691 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1692 fail_unless (result == 50);
1693 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1694 fail_unless (result == 150);
1696 /* close segment, this accumulates nothing. */
1697 gst_segment_set_newsegment (&segment, TRUE, -1.0, 1.0,
1698 GST_FORMAT_TIME, 150, 200, 0);
1700 fail_unless (segment.rate == -1.0);
1701 fail_unless (segment.applied_rate == 1.0);
1702 fail_unless (segment.format == GST_FORMAT_TIME);
1703 fail_unless (segment.flags == 0);
1704 fail_unless (segment.start == 150);
1705 fail_unless (segment.stop == 200);
1706 fail_unless (segment.time == 0);
1707 fail_unless (segment.base == 0);
1708 fail_unless (segment.position == 200);
1709 fail_unless (segment.duration == -1);
1711 /* new segment, this accumulates 50. */
1712 gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1713 GST_FORMAT_TIME, 150, 300, 150);
1715 fail_unless (segment.rate == 1.0);
1716 fail_unless (segment.applied_rate == 1.0);
1717 fail_unless (segment.format == GST_FORMAT_TIME);
1718 fail_unless (segment.flags == 0);
1719 fail_unless (segment.start == 150);
1720 fail_unless (segment.stop == 300);
1721 fail_unless (segment.time == 150);
1722 fail_unless (segment.base == 50);
1723 fail_unless (segment.position == 150);
1724 fail_unless (segment.duration == -1);
1726 /* invalid time gives invalid result */
1727 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1728 fail_unless (result == -1);
1730 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1731 fail_unless (result == 50);
1732 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1733 fail_unless (result == 150);
1735 result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1736 fail_unless (result == 100);
1737 result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1738 fail_unless (result == 200);
1744 GST_START_TEST (segment_copy)
1747 GstSegment segment = { 0.0, };
1749 /* this is a boxed type copy function, we support copying NULL */
1750 fail_unless (gst_segment_copy (NULL) == NULL);
1752 gst_segment_init (&segment, GST_FORMAT_TIME);
1754 segment.rate = -1.0;
1755 segment.applied_rate = 1.0;
1760 copy = gst_segment_copy (&segment);
1761 fail_unless (copy != NULL);
1762 /* we inited the struct on the stack to zeroes, so direct comparison should
1763 * be ok here despite the padding field and regardless of implementation */
1764 fail_unless (memcmp (copy, &segment, sizeof (GstSegment)) == 0);
1765 gst_segment_free (copy);
1771 gst_segment_suite (void)
1773 Suite *s = suite_create ("GstSegment");
1774 TCase *tc_chain = tcase_create ("segments");
1776 tcase_set_timeout (tc_chain, 20);
1778 suite_add_tcase (s, tc_chain);
1779 tcase_add_test (tc_chain, segment_seek_nosize);
1780 tcase_add_test (tc_chain, segment_seek_size);
1781 tcase_add_test (tc_chain, segment_seek_reverse);
1782 tcase_add_test (tc_chain, segment_seek_rate);
1784 tcase_add_test (tc_chain, segment_newsegment_open);
1785 tcase_add_test (tc_chain, segment_newsegment_closed);
1786 tcase_add_test (tc_chain, segment_newsegment_streamtime);
1787 tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1788 tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1789 tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1790 tcase_add_test (tc_chain, segment_newsegment_runningtime);
1791 tcase_add_test (tc_chain, segment_newsegment_accum);
1792 tcase_add_test (tc_chain, segment_newsegment_accum2);
1794 tcase_add_test (tc_chain, segment_copy);
1799 GST_CHECK_MAIN (gst_segment);