gst/gstsegment.c: Fine tune the cases where the segment start/stop values are really...
[platform/upstream/gstreamer.git] / tests / check / gst / gstsegment.c
1 /* GStreamer
2  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3  *
4  * gstsegment.c: Unit test for segments
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 /* mess with the segment structure in the bytes format */
25 GST_START_TEST (segment_seek_nosize)
26 {
27   GstSegment segment;
28   gboolean res;
29   gint64 cstart, cstop;
30   gboolean update;
31
32   gst_segment_init (&segment, GST_FORMAT_BYTES);
33
34   /* configure segment to start 100 */
35   gst_segment_set_seek (&segment, 1.0,
36       GST_FORMAT_BYTES,
37       GST_SEEK_FLAG_NONE,
38       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
39   fail_unless (segment.start == 100);
40   fail_unless (segment.stop == -1);
41   fail_unless (update == TRUE);
42
43   /* configure segment to stop relative, should not do anything since 
44    * size is unknown. */
45   gst_segment_set_seek (&segment, 1.0,
46       GST_FORMAT_BYTES,
47       GST_SEEK_FLAG_NONE,
48       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
49   fail_unless (segment.start == 100);
50   fail_unless (segment.stop == -1);
51   fail_unless (update == FALSE);
52
53   /* do some clipping on the open range */
54   /* completely outside */
55   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
56   fail_unless (res == FALSE);
57
58   /* touching lower bound, still outside of the segment */
59   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
60   fail_unless (res == FALSE);
61
62   /* partially inside */
63   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
64   fail_unless (res == TRUE);
65   fail_unless (cstart == 100);
66   fail_unless (cstop == 150);
67
68   /* inside, touching lower bound */
69   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
70       100, 150, &cstart, &cstop);
71   fail_unless (res == TRUE);
72   fail_unless (cstart == 100);
73   fail_unless (cstop == 150);
74
75   /* special case, 0 duration */
76   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
77       100, 100, &cstart, &cstop);
78   fail_unless (res == TRUE);
79   fail_unless (cstart == 100);
80   fail_unless (cstop == 100);
81
82   /* completely inside */
83   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
84       150, 200, &cstart, &cstop);
85   fail_unless (res == TRUE);
86   fail_unless (cstart == 150);
87   fail_unless (cstop == 200);
88
89   /* invalid start */
90   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
91   fail_unless (res == FALSE);
92
93   /* start outside, we don't know the stop */
94   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
95   fail_unless (res == TRUE);
96   fail_unless (cstart == 100);
97   fail_unless (cstop == -1);
98
99   /* start on lower bound */
100   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
101   fail_unless (res == TRUE);
102   fail_unless (cstart == 100);
103   fail_unless (cstop == -1);
104
105   /* start inside */
106   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
107   fail_unless (res == TRUE);
108   fail_unless (cstart == 150);
109   fail_unless (cstop == -1);
110
111   /* add 100 to start, set stop to 300 */
112   gst_segment_set_seek (&segment, 1.0,
113       GST_FORMAT_BYTES,
114       GST_SEEK_FLAG_NONE,
115       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
116   fail_unless (segment.start == 200);
117   fail_unless (segment.stop == 300);
118   fail_unless (update == TRUE);
119
120   update = FALSE;
121   /* add 100 to start (to 300), set stop to 200, this is not allowed. 
122    * nothing should be updated in the segment. A g_warning is
123    * emited. */
124   ASSERT_CRITICAL (gst_segment_set_seek (&segment, 1.0,
125           GST_FORMAT_BYTES,
126           GST_SEEK_FLAG_NONE,
127           GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update));
128   fail_unless (segment.start == 200);
129   fail_unless (segment.stop == 300);
130   /* update didn't change */
131   fail_unless (update == FALSE);
132
133   update = TRUE;
134   /* seek relative to end, should not do anything since size is
135    * unknown. */
136   gst_segment_set_seek (&segment, 1.0,
137       GST_FORMAT_BYTES,
138       GST_SEEK_FLAG_NONE,
139       GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100, &update);
140   fail_unless (segment.start == 200);
141   fail_unless (segment.stop == 300);
142   fail_unless (update == FALSE);
143
144   /* completely outside */
145   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
146   fail_unless (res == FALSE);
147
148   /* touching lower bound */
149   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
150   fail_unless (res == FALSE);
151
152   /* partially inside */
153   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
154   fail_unless (res == TRUE);
155   fail_unless (cstart == 200);
156   fail_unless (cstop == 250);
157
158   /* inside, touching lower bound */
159   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
160       200, 250, &cstart, &cstop);
161   fail_unless (res == TRUE);
162   fail_unless (cstart == 200);
163   fail_unless (cstop == 250);
164
165   /* completely inside */
166   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
167       250, 290, &cstart, &cstop);
168   fail_unless (res == TRUE);
169   fail_unless (cstart == 250);
170   fail_unless (cstop == 290);
171
172   /* partially inside */
173   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
174       250, 350, &cstart, &cstop);
175   fail_unless (res == TRUE);
176   fail_unless (cstart == 250);
177   fail_unless (cstop == 300);
178
179   /* invalid start */
180   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
181   fail_unless (res == FALSE);
182
183   /* start outside */
184   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
185   fail_unless (res == TRUE);
186   fail_unless (cstart == 200);
187   fail_unless (cstop == 300);
188
189   /* start on lower bound */
190   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
191   fail_unless (res == TRUE);
192   fail_unless (cstart == 200);
193   fail_unless (cstop == 300);
194
195   /* start inside */
196   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
197   fail_unless (res == TRUE);
198   fail_unless (cstart == 250);
199   fail_unless (cstop == 300);
200
201   /* start outside on boundary */
202   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
203   fail_unless (res == FALSE);
204
205   /* start completely outside */
206   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
207   fail_unless (res == FALSE);
208 }
209
210 GST_END_TEST;
211
212 /* mess with the segment structure in the bytes format */
213 GST_START_TEST (segment_seek_size)
214 {
215   GstSegment segment;
216   gboolean res;
217   gint64 cstart, cstop;
218   gboolean update;
219
220   gst_segment_init (&segment, GST_FORMAT_BYTES);
221   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
222
223   /* configure segment to start 100 */
224   gst_segment_set_seek (&segment, 1.0,
225       GST_FORMAT_BYTES,
226       GST_SEEK_FLAG_NONE,
227       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
228   fail_unless (segment.start == 100);
229   fail_unless (segment.stop == -1);
230   fail_unless (update == TRUE);
231
232   /* configure segment to stop relative, does not update stop
233    * since we did not set it before. */
234   gst_segment_set_seek (&segment, 1.0,
235       GST_FORMAT_BYTES,
236       GST_SEEK_FLAG_NONE,
237       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
238   fail_unless (segment.start == 100);
239   fail_unless (segment.stop == -1);
240   fail_unless (update == FALSE);
241
242   /* do some clipping on the open range */
243   /* completely outside */
244   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
245   fail_unless (res == FALSE);
246
247   /* touching lower bound */
248   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
249   fail_unless (res == FALSE);
250
251   /* partially inside */
252   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
253   fail_unless (res == TRUE);
254   fail_unless (cstart == 100);
255   fail_unless (cstop == 150);
256
257   /* inside, touching lower bound */
258   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
259       100, 150, &cstart, &cstop);
260   fail_unless (res == TRUE);
261   fail_unless (cstart == 100);
262   fail_unless (cstop == 150);
263
264   /* completely inside */
265   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
266       150, 200, &cstart, &cstop);
267   fail_unless (res == TRUE);
268   fail_unless (cstart == 150);
269   fail_unless (cstop == 200);
270
271   /* partially inside, clip to size */
272   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
273       150, 300, &cstart, &cstop);
274   fail_unless (res == TRUE);
275   fail_unless (cstart == 150);
276   fail_unless (cstop == 200);
277
278   /* invalid start */
279   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
280   fail_unless (res == FALSE);
281
282   /* start outside */
283   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
284   fail_unless (res == TRUE);
285   fail_unless (cstart == 100);
286   fail_unless (cstop == -1);
287
288   /* start on lower bound */
289   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
290   fail_unless (res == TRUE);
291   fail_unless (cstart == 100);
292   fail_unless (cstop == -1);
293
294   /* start inside */
295   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
296   fail_unless (res == TRUE);
297   fail_unless (cstart == 150);
298   fail_unless (cstop == -1);
299
300   /* add 100 to start, set stop to 300, stop clips to 200 */
301   gst_segment_set_seek (&segment, 1.0,
302       GST_FORMAT_BYTES,
303       GST_SEEK_FLAG_NONE,
304       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
305   fail_unless (segment.start == 200);
306   fail_unless (segment.stop == 200);
307
308   /* add 100 to start (to 300), set stop to 200, this clips start
309    * to duration */
310   gst_segment_set_seek (&segment, 1.0,
311       GST_FORMAT_BYTES,
312       GST_SEEK_FLAG_NONE,
313       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update);
314   fail_unless (segment.start == 200);
315   fail_unless (segment.stop == 200);
316   fail_unless (update == TRUE);
317
318   /* seek relative to end */
319   gst_segment_set_seek (&segment, 1.0,
320       GST_FORMAT_BYTES,
321       GST_SEEK_FLAG_NONE,
322       GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
323   fail_unless (segment.start == 100);
324   fail_unless (segment.stop == 180);
325   fail_unless (update == TRUE);
326
327   /* completely outside */
328   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
329   fail_unless (res == FALSE);
330
331   /* touching lower bound */
332   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
333   fail_unless (res == FALSE);
334
335   /* partially inside */
336   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
337   fail_unless (res == TRUE);
338   fail_unless (cstart == 100);
339   fail_unless (cstop == 150);
340
341   /* inside, touching lower bound */
342   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
343       100, 150, &cstart, &cstop);
344   fail_unless (res == TRUE);
345   fail_unless (cstart == 100);
346   fail_unless (cstop == 150);
347
348   /* completely inside */
349   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
350       150, 170, &cstart, &cstop);
351   fail_unless (res == TRUE);
352   fail_unless (cstart == 150);
353   fail_unless (cstop == 170);
354
355   /* partially inside */
356   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
357       150, 250, &cstart, &cstop);
358   fail_unless (res == TRUE);
359   fail_unless (cstart == 150);
360   fail_unless (cstop == 180);
361
362   /* invalid start */
363   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
364   fail_unless (res == FALSE);
365
366   /* start outside */
367   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
368   fail_unless (res == TRUE);
369   fail_unless (cstart == 100);
370   fail_unless (cstop == 180);
371
372   /* start on lower bound */
373   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
374   fail_unless (res == TRUE);
375   fail_unless (cstart == 100);
376   fail_unless (cstop == 180);
377
378   /* start inside */
379   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
380   fail_unless (res == TRUE);
381   fail_unless (cstart == 150);
382   fail_unless (cstop == 180);
383
384   /* start outside on boundary */
385   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
386   fail_unless (res == FALSE);
387
388   /* start completely outside */
389   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
390   fail_unless (res == FALSE);
391 }
392
393 GST_END_TEST;
394
395 GST_START_TEST (segment_seek_reverse)
396 {
397   GstSegment segment;
398   gboolean update;
399
400   gst_segment_init (&segment, GST_FORMAT_BYTES);
401   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
402
403   /* configure segment to stop 100 */
404   gst_segment_set_seek (&segment, -1.0,
405       GST_FORMAT_BYTES,
406       GST_SEEK_FLAG_NONE,
407       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 100, &update);
408   fail_unless (segment.start == 0);
409   fail_unless (segment.stop == 100);
410   fail_unless (segment.time == 0);
411   fail_unless (segment.last_stop == 100);
412   fail_unless (update == TRUE);
413
414   /* update */
415   gst_segment_set_seek (&segment, -1.0,
416       GST_FORMAT_BYTES,
417       GST_SEEK_FLAG_NONE,
418       GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_CUR, -20, &update);
419   fail_unless (segment.start == 10);
420   fail_unless (segment.stop == 80);
421   fail_unless (segment.time == 10);
422   fail_unless (segment.last_stop == 80);
423   fail_unless (update == TRUE);
424
425   gst_segment_set_seek (&segment, -1.0,
426       GST_FORMAT_BYTES,
427       GST_SEEK_FLAG_NONE,
428       GST_SEEK_TYPE_SET, 20, GST_SEEK_TYPE_NONE, 0, &update);
429   fail_unless (segment.start == 20);
430   fail_unless (segment.stop == 80);
431   fail_unless (segment.time == 20);
432   fail_unless (segment.last_stop == 80);
433   fail_unless (update == TRUE);
434 }
435
436 GST_END_TEST;
437
438 /* mess with the segment structure in the bytes format */
439 GST_START_TEST (segment_newsegment_open)
440 {
441   GstSegment segment;
442
443   gst_segment_init (&segment, GST_FORMAT_BYTES);
444
445   /* time should also work for starting from 0 */
446   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_TIME, 0, -1, 0);
447
448   fail_unless (segment.rate == 1.0);
449   fail_unless (segment.format == GST_FORMAT_BYTES);
450   fail_unless (segment.flags == 0);
451   fail_unless (segment.start == 0);
452   fail_unless (segment.stop == -1);
453   fail_unless (segment.time == 0);
454   fail_unless (segment.accum == 0);
455   fail_unless (segment.last_stop == 0);
456   fail_unless (segment.duration == -1);
457
458   /* we set stop but in the wrong format, stop stays open. */
459   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_TIME, 0, 200, 0);
460
461   fail_unless (segment.start == 0);
462   fail_unless (segment.stop == -1);
463   fail_unless (segment.time == 0);
464   fail_unless (segment.accum == 0);
465
466   /* update, nothing changes */
467   gst_segment_set_newsegment (&segment, TRUE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
468
469   fail_unless (segment.start == 0);
470   fail_unless (segment.stop == -1);
471   fail_unless (segment.time == 0);
472   fail_unless (segment.accum == 0);
473
474   /* update */
475   gst_segment_set_newsegment (&segment, TRUE, 1.0,
476       GST_FORMAT_BYTES, 100, -1, 100);
477
478   fail_unless (segment.start == 100);
479   fail_unless (segment.stop == -1);
480   fail_unless (segment.time == 100);
481   fail_unless (segment.accum == 100);
482
483   /* last_stop 0, accum does not change */
484   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
485
486   fail_unless (segment.start == 0);
487   fail_unless (segment.stop == -1);
488   fail_unless (segment.time == 0);
489   fail_unless (segment.accum == 100);
490
491   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
492
493   /* last_stop 200, accum changes */
494   gst_segment_set_newsegment (&segment, FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0);
495
496   fail_unless (segment.start == 0);
497   fail_unless (segment.stop == -1);
498   fail_unless (segment.time == 0);
499   fail_unless (segment.accum == 300);
500
501 }
502
503 GST_END_TEST;
504
505
506 /* mess with the segment structure in the bytes format */
507 GST_START_TEST (segment_newsegment_closed)
508 {
509   GstSegment segment;
510
511   gst_segment_init (&segment, GST_FORMAT_BYTES);
512
513   gst_segment_set_newsegment (&segment, FALSE, 1.0,
514       GST_FORMAT_BYTES, 0, 200, 0);
515
516   fail_unless (segment.rate == 1.0);
517   fail_unless (segment.format == GST_FORMAT_BYTES);
518   fail_unless (segment.flags == 0);
519   fail_unless (segment.start == 0);
520   fail_unless (segment.stop == 200);
521   fail_unless (segment.time == 0);
522   fail_unless (segment.accum == 0);
523   fail_unless (segment.last_stop == 0);
524   fail_unless (segment.duration == -1);
525
526   /* do an update */
527   gst_segment_set_newsegment (&segment, TRUE, 1.0, GST_FORMAT_BYTES, 0, 300, 0);
528
529   fail_unless (segment.start == 0);
530   fail_unless (segment.stop == 300);
531   fail_unless (segment.time == 0);
532   fail_unless (segment.accum == 0);
533
534   /* and a new accumulated one */
535   gst_segment_set_newsegment (&segment, FALSE, 1.0,
536       GST_FORMAT_BYTES, 100, 400, 300);
537
538   fail_unless (segment.start == 100);
539   fail_unless (segment.stop == 400);
540   fail_unless (segment.time == 300);
541   fail_unless (segment.accum == 300);
542
543   /* and a new updated one */
544   gst_segment_set_newsegment (&segment, TRUE, 1.0,
545       GST_FORMAT_BYTES, 100, 500, 300);
546
547   fail_unless (segment.start == 100);
548   fail_unless (segment.stop == 500);
549   fail_unless (segment.time == 300);
550   fail_unless (segment.accum == 300);
551
552   /* and a new partially updated one */
553   gst_segment_set_newsegment (&segment, TRUE, 1.0,
554       GST_FORMAT_BYTES, 200, 500, 400);
555
556   fail_unless (segment.start == 200);
557   fail_unless (segment.stop == 500);
558   fail_unless (segment.time == 400);
559   fail_unless (segment.accum == 400);
560 }
561
562 GST_END_TEST;
563
564 /* mess with the segment structure in the time format */
565 GST_START_TEST (segment_newsegment_streamtime)
566 {
567   GstSegment segment;
568   gint64 result;
569
570   gst_segment_init (&segment, GST_FORMAT_TIME);
571
572   /***************************
573    * Normal segment
574    ***************************/
575   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
576       GST_FORMAT_TIME, 0, 200, 0);
577
578   fail_unless (segment.rate == 1.0);
579   fail_unless (segment.applied_rate == 1.0);
580   fail_unless (segment.format == GST_FORMAT_TIME);
581   fail_unless (segment.flags == 0);
582   fail_unless (segment.start == 0);
583   fail_unless (segment.stop == 200);
584   fail_unless (segment.time == 0);
585   fail_unless (segment.accum == 0);
586   fail_unless (segment.last_stop == 0);
587   fail_unless (segment.duration == -1);
588
589   /* invalid time gives invalid result */
590   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
591   fail_unless (result == -1);
592
593   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
594   fail_unless (result == 0);
595
596   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
597   fail_unless (result == 100);
598
599   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
600   fail_unless (result == 200);
601
602   /* outside of the segment */
603   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
604   fail_unless (result == -1);
605
606   /*********************
607    * time shifted by 500
608    *********************/
609   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
610       GST_FORMAT_TIME, 0, 200, 500);
611
612   fail_unless (segment.accum == 200);
613
614   /* invalid time gives invalid result */
615   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
616   fail_unless (result == -1);
617
618   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
619   fail_unless (result == 500);
620
621   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
622   fail_unless (result == 600);
623
624   /* outside of the segment */
625   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
626   fail_unless (result == -1);
627
628   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
629   fail_unless (result == -1);
630
631   /*********************
632    * time offset by 500
633    *********************/
634   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
635       GST_FORMAT_TIME, 500, 700, 0);
636
637   fail_unless (segment.accum == 400);
638
639   /* invalid time gives invalid result */
640   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
641   fail_unless (result == -1);
642
643   /* before segment is invalid */
644   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
645   fail_unless (result == -1);
646
647   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
648   fail_unless (result == 0);
649
650   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
651   fail_unless (result == 100);
652
653   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
654   fail_unless (result == 200);
655
656   /* outside of the segment */
657   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
658   fail_unless (result == -1);
659
660   /*************************************
661    * time offset by 500, shifted by 200
662    *************************************/
663   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
664       GST_FORMAT_TIME, 500, 700, 200);
665
666   fail_unless (segment.accum == 600);
667
668   /* invalid time gives invalid result */
669   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
670   fail_unless (result == -1);
671
672   /* before segment is invalid */
673   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
674   fail_unless (result == -1);
675
676   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
677   fail_unless (result == 200);
678
679   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
680   fail_unless (result == 300);
681
682   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
683   fail_unless (result == 400);
684
685   /* outside of the segment */
686   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
687   fail_unless (result == -1);
688 }
689
690 GST_END_TEST;
691
692 /* mess with the segment structure in the time format */
693 GST_START_TEST (segment_newsegment_streamtime_rate)
694 {
695   GstSegment segment;
696   gint64 result;
697
698   gst_segment_init (&segment, GST_FORMAT_TIME);
699
700   /***************************
701    * Normal segment rate 2.0
702    ***************************/
703   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
704       GST_FORMAT_TIME, 0, 200, 0);
705
706   fail_unless (segment.rate == 2.0);
707   fail_unless (segment.applied_rate == 1.0);
708   fail_unless (segment.format == GST_FORMAT_TIME);
709   fail_unless (segment.flags == 0);
710   fail_unless (segment.start == 0);
711   fail_unless (segment.stop == 200);
712   fail_unless (segment.time == 0);
713   fail_unless (segment.accum == 0);
714   fail_unless (segment.last_stop == 0);
715   fail_unless (segment.duration == -1);
716
717   /* invalid time gives invalid result */
718   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
719   fail_unless (result == -1);
720
721   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
722   fail_unless (result == 0);
723
724   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
725   fail_unless (result == 100);
726
727   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
728   fail_unless (result == 150);
729
730   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
731   fail_unless (result == 200);
732
733   /* outside of the segment */
734   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
735   fail_unless (result == -1);
736
737   /***************************************
738    * Normal segment rate 2.0, offset
739    ***************************************/
740   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
741       GST_FORMAT_TIME, 100, 300, 0);
742
743   fail_unless (segment.accum == 100);
744
745   /* invalid time gives invalid result */
746   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
747   fail_unless (result == -1);
748
749   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
750   fail_unless (result == 0);
751
752   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
753   fail_unless (result == 100);
754
755   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
756   fail_unless (result == 150);
757
758   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
759   fail_unless (result == 200);
760
761   /* outside of the segment */
762   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
763   fail_unless (result == -1);
764
765   /***************************************
766    * Normal segment rate -1.0, offset
767    ***************************************/
768
769   /* buffers will arrive from 300 to 100 in a sink, stream time
770    * calculation is unaffected by the rate */
771   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
772       GST_FORMAT_TIME, 100, 300, 0);
773
774   fail_unless (segment.accum == 200);
775
776   /* invalid time gives invalid result */
777   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
778   fail_unless (result == -1);
779
780   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
781   fail_unless (result == 0);
782
783   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
784   fail_unless (result == 100);
785
786   /***********************************************
787    * Normal segment rate -1.0, offset, time = 200
788    ***********************************************/
789   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
790       GST_FORMAT_TIME, 100, 300, 200);
791
792   /* invalid time gives invalid result */
793   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
794   fail_unless (result == -1);
795
796   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
797   fail_unless (result == 200);
798
799   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
800   fail_unless (result == 300);
801
802   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
803   fail_unless (result == 400);
804
805   /* outside of the segment */
806   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
807   fail_unless (result == -1);
808 }
809
810 GST_END_TEST;
811
812 /* mess with the segment structure in the time format */
813 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
814 {
815   GstSegment segment;
816   gint64 result;
817
818   gst_segment_init (&segment, GST_FORMAT_TIME);
819
820   /***********************************************************
821    * Normal segment rate 1.0, applied rate -1.0
822    * This means the timestamps represents a stream going backwards
823    * starting from @time to 0.
824    ************************************************************/
825   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -1.0,
826       GST_FORMAT_TIME, 0, 200, 200);
827
828   fail_unless (segment.rate == 1.0);
829   fail_unless (segment.applied_rate == -1.0);
830   fail_unless (segment.format == GST_FORMAT_TIME);
831   fail_unless (segment.flags == 0);
832   fail_unless (segment.start == 0);
833   fail_unless (segment.stop == 200);
834   fail_unless (segment.time == 200);
835   fail_unless (segment.accum == 0);
836   fail_unless (segment.last_stop == 0);
837   fail_unless (segment.duration == -1);
838
839   /* invalid time gives invalid result */
840   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
841   fail_unless (result == -1);
842
843   /* we count backwards from 200 */
844   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
845   fail_unless (result == 200);
846
847   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
848   fail_unless (result == 100);
849
850   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
851   fail_unless (result == 50);
852
853   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
854   fail_unless (result == 0);
855
856   /* outside of the segment */
857   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
858   fail_unless (result == -1);
859
860   /***********************************************************
861    * Normal segment rate 1.0, applied rate 2.0
862    * This means the timestamps represents a stream at twice the
863    * normal rate
864    ************************************************************/
865   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 2.0,
866       GST_FORMAT_TIME, 0, 200, 0);
867
868   fail_unless (segment.rate == 1.0);
869   fail_unless (segment.applied_rate == 2.0);
870   fail_unless (segment.format == GST_FORMAT_TIME);
871   fail_unless (segment.flags == 0);
872   fail_unless (segment.start == 0);
873   fail_unless (segment.stop == 200);
874   fail_unless (segment.time == 0);
875   fail_unless (segment.accum == 200);
876   fail_unless (segment.last_stop == 0);
877   fail_unless (segment.duration == -1);
878
879   /* invalid time gives invalid result */
880   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
881   fail_unless (result == -1);
882
883   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
884   fail_unless (result == 0);
885
886   /* the stream prepresents a stream going twice as fast, the position 
887    * in the segment is therefore scaled by the applied rate */
888   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
889   fail_unless (result == 200);
890
891   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
892   fail_unless (result == 300);
893
894   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
895   fail_unless (result == 400);
896
897   /* outside of the segment */
898   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
899   fail_unless (result == -1);
900
901   /***********************************************************
902    * Normal segment rate 1.0, applied rate -2.0
903    * This means the timestamps represents a stream at twice the
904    * reverse rate
905    ************************************************************/
906   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -2.0,
907       GST_FORMAT_TIME, 0, 200, 400);
908
909   fail_unless (segment.rate == 1.0);
910   fail_unless (segment.applied_rate == -2.0);
911   fail_unless (segment.format == GST_FORMAT_TIME);
912   fail_unless (segment.flags == 0);
913   fail_unless (segment.start == 0);
914   fail_unless (segment.stop == 200);
915   fail_unless (segment.time == 400);
916   /* previous segment lasted 200, rate of 2.0 was already applied */
917   fail_unless (segment.accum == 400);
918   fail_unless (segment.last_stop == 0);
919   fail_unless (segment.duration == -1);
920
921   /* invalid time gives invalid result */
922   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
923   fail_unless (result == -1);
924
925   /* we count backwards from 400 */
926   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
927   fail_unless (result == 400);
928
929   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
930   fail_unless (result == 200);
931
932   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
933   fail_unless (result == 100);
934
935   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
936   fail_unless (result == 0);
937
938   /* outside of the segment */
939   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
940   fail_unless (result == -1);
941
942   /***********************************************************
943    * Normal segment rate 1.0, applied rate -2.0
944    * This means the timestamps represents a stream at twice the
945    * reverse rate, start time cannot compensate the complete
946    * duration of the segment so we stop at 0
947    ************************************************************/
948   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, -2.0,
949       GST_FORMAT_TIME, 0, 200, 200);
950
951   fail_unless (segment.rate == 1.0);
952   fail_unless (segment.applied_rate == -2.0);
953   fail_unless (segment.format == GST_FORMAT_TIME);
954   fail_unless (segment.flags == 0);
955   fail_unless (segment.start == 0);
956   fail_unless (segment.stop == 200);
957   fail_unless (segment.time == 200);
958   fail_unless (segment.accum == 600);
959   fail_unless (segment.last_stop == 0);
960   fail_unless (segment.duration == -1);
961
962   /* invalid time gives invalid result */
963   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
964   fail_unless (result == -1);
965
966   /* we count backwards from 200 */
967   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
968   fail_unless (result == 200);
969
970   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
971   fail_unless (result == 0);
972
973   /* clamp at 0 */
974   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
975   fail_unless (result == 0);
976
977   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
978   fail_unless (result == 0);
979
980   /* outside of the segment */
981   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
982   fail_unless (result == -1);
983 }
984
985 GST_END_TEST;
986
987 /* mess with the segment structure in the time format */
988 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
989 {
990   GstSegment segment;
991   gint64 result;
992
993   gst_segment_init (&segment, GST_FORMAT_TIME);
994
995   /***********************************************************
996    * Segment rate 2.0, applied rate 2.0
997    * this means we have a double speed stream that we should
998    * speed up by a factor of 2.0 some more. the resulting
999    * stream will be played at four times the speed. 
1000    ************************************************************/
1001   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 2.0,
1002       GST_FORMAT_TIME, 0, 200, 0);
1003
1004   fail_unless (segment.rate == 2.0);
1005   fail_unless (segment.applied_rate == 2.0);
1006   fail_unless (segment.format == GST_FORMAT_TIME);
1007   fail_unless (segment.flags == 0);
1008   fail_unless (segment.start == 0);
1009   fail_unless (segment.stop == 200);
1010   fail_unless (segment.time == 0);
1011   fail_unless (segment.accum == 0);
1012   fail_unless (segment.last_stop == 0);
1013   fail_unless (segment.duration == -1);
1014
1015   /* invalid time gives invalid result */
1016   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1017   fail_unless (result == -1);
1018
1019   /* only applied rate affects our calculation of the stream time */
1020   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1021   fail_unless (result == 0);
1022
1023   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1024   fail_unless (result == 200);
1025
1026   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1027   fail_unless (result == 300);
1028
1029   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1030   fail_unless (result == 400);
1031
1032   /* outside of the segment */
1033   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1034   fail_unless (result == -1);
1035
1036   /***********************************************************
1037    * Segment rate 2.0, applied rate -1.0
1038    * this means we have a reverse stream that we should
1039    * speed up by a factor of 2.0
1040    ************************************************************/
1041   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, -1.0,
1042       GST_FORMAT_TIME, 0, 200, 200);
1043
1044   fail_unless (segment.rate == 2.0);
1045   fail_unless (segment.applied_rate == -1.0);
1046   fail_unless (segment.format == GST_FORMAT_TIME);
1047   fail_unless (segment.flags == 0);
1048   fail_unless (segment.start == 0);
1049   fail_unless (segment.stop == 200);
1050   fail_unless (segment.time == 200);
1051   /* previous segment lasted 100 */
1052   fail_unless (segment.accum == 100);
1053   fail_unless (segment.last_stop == 0);
1054   fail_unless (segment.duration == -1);
1055
1056   /* invalid time gives invalid result */
1057   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1058   fail_unless (result == -1);
1059
1060   /* only applied rate affects our calculation of the stream time */
1061   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1062   fail_unless (result == 200);
1063
1064   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1065   fail_unless (result == 100);
1066
1067   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1068   fail_unless (result == 50);
1069
1070   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1071   fail_unless (result == 0);
1072
1073   /* outside of the segment */
1074   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1075   fail_unless (result == -1);
1076
1077   /***********************************************************
1078    * Segment rate -1.0, applied rate -1.0
1079    * this means we have a reverse stream that we should
1080    * reverse to get the normal stream again.
1081    ************************************************************/
1082   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, -1.0,
1083       GST_FORMAT_TIME, 0, 200, 200);
1084
1085   fail_unless (segment.rate == -1.0);
1086   fail_unless (segment.applied_rate == -1.0);
1087   fail_unless (segment.format == GST_FORMAT_TIME);
1088   fail_unless (segment.flags == 0);
1089   fail_unless (segment.start == 0);
1090   fail_unless (segment.stop == 200);
1091   fail_unless (segment.time == 200);
1092   /* accumulated 100 of previous segment to make 200 */
1093   fail_unless (segment.accum == 200);
1094   fail_unless (segment.last_stop == 0);
1095   fail_unless (segment.duration == -1);
1096
1097   /* invalid time gives invalid result */
1098   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1099   fail_unless (result == -1);
1100
1101   /* only applied rate affects our calculation of the stream time */
1102   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1103   fail_unless (result == 200);
1104
1105   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1106   fail_unless (result == 100);
1107
1108   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1109   fail_unless (result == 50);
1110
1111   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1112   fail_unless (result == 0);
1113
1114   /* outside of the segment */
1115   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1116   fail_unless (result == -1);
1117
1118   /***********************************************************
1119    * Segment rate -1.0, applied rate -1.0
1120    * this means we have a reverse stream that we should
1121    * reverse to get the normal stream again.
1122    ************************************************************/
1123   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 2.0,
1124       GST_FORMAT_TIME, 0, 200, 0);
1125
1126   fail_unless (segment.rate == -1.0);
1127   fail_unless (segment.applied_rate == 2.0);
1128   fail_unless (segment.format == GST_FORMAT_TIME);
1129   fail_unless (segment.flags == 0);
1130   fail_unless (segment.start == 0);
1131   fail_unless (segment.stop == 200);
1132   fail_unless (segment.time == 0);
1133   fail_unless (segment.accum == 400);
1134   fail_unless (segment.last_stop == 0);
1135   fail_unless (segment.duration == -1);
1136
1137   /* invalid time gives invalid result */
1138   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1139   fail_unless (result == -1);
1140
1141   /* only applied rate affects our calculation of the stream time */
1142   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1143   fail_unless (result == 0);
1144
1145   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1146   fail_unless (result == 200);
1147
1148   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1149   fail_unless (result == 300);
1150
1151   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1152   fail_unless (result == 400);
1153
1154   /* outside of the segment */
1155   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1156   fail_unless (result == -1);
1157 }
1158
1159 GST_END_TEST;
1160
1161 /* mess with the segment structure in the time format */
1162 GST_START_TEST (segment_newsegment_runningtime)
1163 {
1164   GstSegment segment;
1165   gint64 result;
1166
1167   gst_segment_init (&segment, GST_FORMAT_TIME);
1168
1169   /***************************
1170    * Normal segment
1171    ***************************/
1172   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 1.0,
1173       GST_FORMAT_TIME, 0, 200, 0);
1174
1175   fail_unless (segment.rate == 1.0);
1176   fail_unless (segment.applied_rate == 1.0);
1177   fail_unless (segment.format == GST_FORMAT_TIME);
1178   fail_unless (segment.flags == 0);
1179   fail_unless (segment.start == 0);
1180   fail_unless (segment.stop == 200);
1181   fail_unless (segment.time == 0);
1182   fail_unless (segment.accum == 0);
1183   fail_unless (segment.last_stop == 0);
1184   fail_unless (segment.duration == -1);
1185
1186   /* invalid time gives invalid result */
1187   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1188   fail_unless (result == -1);
1189
1190   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1191   fail_unless (result == 0);
1192
1193   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1194   fail_unless (result == 100);
1195
1196   /* at edge is exactly the segment duration */
1197   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1198   fail_unless (result == 200);
1199
1200   /* outside of the segment */
1201   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1202   fail_unless (result == -1);
1203
1204   /***********************************************************
1205    * time shifted by 500, check if accumulation worked.
1206    * Rate convert to twice the speed which means scaling down
1207    * all positions by 2.0 in this segment.
1208    * Then time argument is not used at all here.
1209    ***********************************************************/
1210   gst_segment_set_newsegment_full (&segment, FALSE, 2.0, 1.0,
1211       GST_FORMAT_TIME, 0, 200, 500);
1212
1213   /* normal speed gives elapsed of 200 */
1214   fail_unless (segment.accum == 200);
1215
1216   /* invalid time gives invalid result */
1217   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1218   fail_unless (result == -1);
1219
1220   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1221   fail_unless (result == 200);
1222
1223   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1224   fail_unless (result == 250);
1225
1226   /* outside of the segment */
1227   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1228   fail_unless (result == -1);
1229
1230   /********************************************
1231    * time offset by 500
1232    * applied rate is not used for running time
1233    ********************************************/
1234   gst_segment_set_newsegment_full (&segment, FALSE, 1.0, 2.0,
1235       GST_FORMAT_TIME, 500, 700, 0);
1236
1237   /* previous segment played at double speed gives elapsed time of
1238    * 100 added to previous accum of 200 gives 300. */
1239   fail_unless (segment.accum == 300);
1240
1241   /* invalid time gives invalid result */
1242   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1243   fail_unless (result == -1);
1244
1245   /* before segment is invalid */
1246   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1247   fail_unless (result == -1);
1248
1249   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1250   fail_unless (result == 300);
1251
1252   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1253   fail_unless (result == 400);
1254
1255   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1256   fail_unless (result == 500);
1257
1258   /* outside of the segment */
1259   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1260   fail_unless (result == -1);
1261
1262   /**********************************************************
1263    * time offset by 500, shifted by 200
1264    * Negative rate makes the running time go backwards 
1265    * relative to the segment stop position. again time
1266    * is ignored.
1267    **********************************************************/
1268   gst_segment_set_newsegment_full (&segment, FALSE, -1.0, 1.0,
1269       GST_FORMAT_TIME, 500, 700, 200);
1270
1271   fail_unless (segment.accum == 500);
1272
1273   /* invalid time gives invalid result */
1274   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1275   fail_unless (result == -1);
1276
1277   /* before segment is invalid */
1278   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1279   fail_unless (result == -1);
1280
1281   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1282   fail_unless (result == 700);
1283
1284   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1285   fail_unless (result == 600);
1286
1287   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1288   fail_unless (result == 500);
1289
1290   /* outside of the segment */
1291   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1292   fail_unless (result == -1);
1293
1294   /**********************************************************
1295    * time offset by 500, shifted by 200
1296    * Negative rate makes the running time go backwards at
1297    * twice speed relative to the segment stop position. again 
1298    * time is ignored.
1299    **********************************************************/
1300   gst_segment_set_newsegment_full (&segment, FALSE, -2.0, -2.0,
1301       GST_FORMAT_TIME, 500, 700, 200);
1302
1303   fail_unless (segment.accum == 700);
1304
1305   /* invalid time gives invalid result */
1306   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1307   fail_unless (result == -1);
1308
1309   /* before segment is invalid */
1310   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1311   fail_unless (result == -1);
1312
1313   /* total scaled segment time is 100, accum is 700, so we get 800 */
1314   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1315   fail_unless (result == 800);
1316
1317   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1318   fail_unless (result == 750);
1319
1320   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1321   fail_unless (result == 700);
1322
1323   /* outside of the segment */
1324   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1325   fail_unless (result == -1);
1326
1327   /* see if negative rate closed segment correctly */
1328   gst_segment_set_newsegment_full (&segment, FALSE, -2.0, -1.0,
1329       GST_FORMAT_TIME, 500, 700, 200);
1330
1331   /* previous segment lasted 100, and was at 700 so we should get 800 */
1332   fail_unless (segment.accum == 800);
1333 }
1334
1335 GST_END_TEST;
1336
1337 Suite *
1338 gst_segment_suite (void)
1339 {
1340   Suite *s = suite_create ("GstSegment");
1341   TCase *tc_chain = tcase_create ("segments");
1342
1343   tcase_set_timeout (tc_chain, 20);
1344
1345   suite_add_tcase (s, tc_chain);
1346   tcase_add_test (tc_chain, segment_seek_nosize);
1347   tcase_add_test (tc_chain, segment_seek_size);
1348   tcase_add_test (tc_chain, segment_seek_reverse);
1349   tcase_add_test (tc_chain, segment_newsegment_open);
1350   tcase_add_test (tc_chain, segment_newsegment_closed);
1351   tcase_add_test (tc_chain, segment_newsegment_streamtime);
1352   tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1353   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1354   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1355   tcase_add_test (tc_chain, segment_newsegment_runningtime);
1356
1357   return s;
1358 }
1359
1360 GST_CHECK_MAIN (gst_segment);