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