7408e5613cc7d01dd6071476f24cc5835c3af33d
[platform/upstream/gstreamer.git] / tests / check / gst / gstsegment.c
1 /* GStreamer
2  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
3  *               2009 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * gstsegment.c: Unit test for segments
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <gst/check/gstcheck.h>
24
25 /* mess with the segment structure in the bytes format */
26 GST_START_TEST (segment_seek_nosize)
27 {
28   GstSegment segment;
29   gboolean res;
30   gint64 cstart, cstop;
31   gboolean update;
32
33   gst_segment_init (&segment, GST_FORMAT_BYTES);
34
35   /* configure segment to start 100 */
36   gst_segment_set_seek (&segment, 1.0,
37       GST_FORMAT_BYTES,
38       GST_SEEK_FLAG_NONE,
39       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
40   fail_unless (segment.start == 100);
41   fail_unless (segment.stop == -1);
42   fail_unless (update == TRUE);
43
44   /* configure segment to stop relative, should not do anything since 
45    * size is unknown. */
46   gst_segment_set_seek (&segment, 1.0,
47       GST_FORMAT_BYTES,
48       GST_SEEK_FLAG_NONE,
49       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
50   fail_unless (segment.start == 100);
51   fail_unless (segment.stop == -1);
52   fail_unless (update == FALSE);
53
54   /* do some clipping on the open range */
55   /* completely outside */
56   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
57   fail_unless (res == FALSE);
58
59   /* touching lower bound, still outside of the segment */
60   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
61   fail_unless (res == FALSE);
62
63   /* partially inside */
64   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
65   fail_unless (res == TRUE);
66   fail_unless (cstart == 100);
67   fail_unless (cstop == 150);
68
69   /* inside, touching lower bound */
70   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
71       100, 150, &cstart, &cstop);
72   fail_unless (res == TRUE);
73   fail_unless (cstart == 100);
74   fail_unless (cstop == 150);
75
76   /* special case, 0 duration and outside segment */
77   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 90, 90, &cstart, &cstop);
78   fail_unless (res == FALSE);
79
80   /* special case, 0 duration and touching lower bound, i.e. inside segment */
81   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
82       100, 100, &cstart, &cstop);
83   fail_unless (res == TRUE);
84   fail_unless (cstart == 100);
85   fail_unless (cstop == 100);
86
87   /* special case, 0 duration and inside the segment */
88   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
89       120, 120, &cstart, &cstop);
90   fail_unless (res == TRUE);
91   fail_unless (cstart == 120);
92   fail_unless (cstop == 120);
93
94   /* completely inside */
95   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
96       150, 200, &cstart, &cstop);
97   fail_unless (res == TRUE);
98   fail_unless (cstart == 150);
99   fail_unless (cstop == 200);
100
101   /* invalid start */
102   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
103   fail_unless (res == FALSE);
104
105   /* start outside, we don't know the stop */
106   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
107   fail_unless (res == TRUE);
108   fail_unless (cstart == 100);
109   fail_unless (cstop == -1);
110
111   /* start on lower bound */
112   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
113   fail_unless (res == TRUE);
114   fail_unless (cstart == 100);
115   fail_unless (cstop == -1);
116
117   /* start inside */
118   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
119   fail_unless (res == TRUE);
120   fail_unless (cstart == 150);
121   fail_unless (cstop == -1);
122
123   /* add 100 to start, set stop to 300 */
124   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, 300, &update);
128   fail_unless (segment.start == 200);
129   fail_unless (segment.stop == 300);
130   fail_unless (update == TRUE);
131
132   update = FALSE;
133   /* add 100 to start (to 300), set stop to 200, this is not allowed. 
134    * nothing should be updated in the segment. A g_warning is
135    * emited. */
136   ASSERT_CRITICAL (gst_segment_set_seek (&segment, 1.0,
137           GST_FORMAT_BYTES,
138           GST_SEEK_FLAG_NONE,
139           GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update));
140   fail_unless (segment.start == 200);
141   fail_unless (segment.stop == 300);
142   /* update didn't change */
143   fail_unless (update == FALSE);
144
145   update = TRUE;
146   /* seek relative to end, should not do anything since size is
147    * unknown. */
148   gst_segment_set_seek (&segment, 1.0,
149       GST_FORMAT_BYTES,
150       GST_SEEK_FLAG_NONE,
151       GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100, &update);
152   fail_unless (segment.start == 200);
153   fail_unless (segment.stop == 300);
154   fail_unless (update == FALSE);
155
156   /* completely outside */
157   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
158   fail_unless (res == FALSE);
159
160   /* touching lower bound */
161   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
162   fail_unless (res == FALSE);
163
164   /* partially inside */
165   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
166   fail_unless (res == TRUE);
167   fail_unless (cstart == 200);
168   fail_unless (cstop == 250);
169
170   /* inside, touching lower bound */
171   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
172       200, 250, &cstart, &cstop);
173   fail_unless (res == TRUE);
174   fail_unless (cstart == 200);
175   fail_unless (cstop == 250);
176
177   /* completely inside */
178   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
179       250, 290, &cstart, &cstop);
180   fail_unless (res == TRUE);
181   fail_unless (cstart == 250);
182   fail_unless (cstop == 290);
183
184   /* partially inside */
185   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
186       250, 350, &cstart, &cstop);
187   fail_unless (res == TRUE);
188   fail_unless (cstart == 250);
189   fail_unless (cstop == 300);
190
191   /* invalid start */
192   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
193   fail_unless (res == FALSE);
194
195   /* start outside */
196   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
197   fail_unless (res == TRUE);
198   fail_unless (cstart == 200);
199   fail_unless (cstop == 300);
200
201   /* start on lower bound */
202   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
203   fail_unless (res == TRUE);
204   fail_unless (cstart == 200);
205   fail_unless (cstop == 300);
206
207   /* start inside */
208   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
209   fail_unless (res == TRUE);
210   fail_unless (cstart == 250);
211   fail_unless (cstop == 300);
212
213   /* start outside on boundary */
214   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
215   fail_unless (res == FALSE);
216
217   /* start completely outside */
218   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
219   fail_unless (res == FALSE);
220 }
221
222 GST_END_TEST;
223
224 /* mess with the segment structure in the bytes format */
225 GST_START_TEST (segment_seek_size)
226 {
227   GstSegment segment;
228   gboolean res;
229   gint64 cstart, cstop;
230   gboolean update;
231
232   gst_segment_init (&segment, GST_FORMAT_BYTES);
233   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
234
235   /* configure segment to start 100 */
236   gst_segment_set_seek (&segment, 1.0,
237       GST_FORMAT_BYTES,
238       GST_SEEK_FLAG_NONE,
239       GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1, &update);
240   fail_unless (segment.start == 100);
241   fail_unless (segment.stop == -1);
242   fail_unless (update == TRUE);
243
244   /* configure segment to stop relative, does not update stop
245    * since we did not set it before. */
246   gst_segment_set_seek (&segment, 1.0,
247       GST_FORMAT_BYTES,
248       GST_SEEK_FLAG_NONE,
249       GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100, &update);
250   fail_unless (segment.start == 100);
251   fail_unless (segment.stop == -1);
252   fail_unless (update == FALSE);
253
254   /* do some clipping on the open range */
255   /* completely outside */
256   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
257   fail_unless (res == FALSE);
258
259   /* touching lower bound */
260   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
261   fail_unless (res == FALSE);
262
263   /* partially inside */
264   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
265   fail_unless (res == TRUE);
266   fail_unless (cstart == 100);
267   fail_unless (cstop == 150);
268
269   /* inside, touching lower bound */
270   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
271       100, 150, &cstart, &cstop);
272   fail_unless (res == TRUE);
273   fail_unless (cstart == 100);
274   fail_unless (cstop == 150);
275
276   /* completely inside */
277   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
278       150, 200, &cstart, &cstop);
279   fail_unless (res == TRUE);
280   fail_unless (cstart == 150);
281   fail_unless (cstop == 200);
282
283   /* partially inside, clip to size */
284   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
285       150, 300, &cstart, &cstop);
286   fail_unless (res == TRUE);
287   fail_unless (cstart == 150);
288   fail_unless (cstop == 200);
289
290   /* invalid start */
291   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
292   fail_unless (res == FALSE);
293
294   /* start outside */
295   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
296   fail_unless (res == TRUE);
297   fail_unless (cstart == 100);
298   fail_unless (cstop == -1);
299
300   /* start on lower bound */
301   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
302   fail_unless (res == TRUE);
303   fail_unless (cstart == 100);
304   fail_unless (cstop == -1);
305
306   /* start inside */
307   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
308   fail_unless (res == TRUE);
309   fail_unless (cstart == 150);
310   fail_unless (cstop == -1);
311
312   /* add 100 to start, set stop to 300, stop clips to 200 */
313   gst_segment_set_seek (&segment, 1.0,
314       GST_FORMAT_BYTES,
315       GST_SEEK_FLAG_NONE,
316       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300, &update);
317   fail_unless (segment.start == 200);
318   fail_unless (segment.stop == 200);
319
320   /* add 100 to start (to 300), set stop to 200, this clips start
321    * to duration */
322   gst_segment_set_seek (&segment, 1.0,
323       GST_FORMAT_BYTES,
324       GST_SEEK_FLAG_NONE,
325       GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200, &update);
326   fail_unless (segment.start == 200);
327   fail_unless (segment.stop == 200);
328   fail_unless (update == FALSE);
329
330   /* seek relative to end */
331   gst_segment_set_seek (&segment, 1.0,
332       GST_FORMAT_BYTES,
333       GST_SEEK_FLAG_NONE,
334       GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20, &update);
335   fail_unless (segment.start == 100);
336   fail_unless (segment.stop == 180);
337   fail_unless (update == TRUE);
338
339   /* completely outside */
340   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
341   fail_unless (res == FALSE);
342
343   /* touching lower bound */
344   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
345   fail_unless (res == FALSE);
346
347   /* partially inside */
348   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
349   fail_unless (res == TRUE);
350   fail_unless (cstart == 100);
351   fail_unless (cstop == 150);
352
353   /* inside, touching lower bound */
354   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
355       100, 150, &cstart, &cstop);
356   fail_unless (res == TRUE);
357   fail_unless (cstart == 100);
358   fail_unless (cstop == 150);
359
360   /* completely inside */
361   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
362       150, 170, &cstart, &cstop);
363   fail_unless (res == TRUE);
364   fail_unless (cstart == 150);
365   fail_unless (cstop == 170);
366
367   /* partially inside */
368   res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
369       150, 250, &cstart, &cstop);
370   fail_unless (res == TRUE);
371   fail_unless (cstart == 150);
372   fail_unless (cstop == 180);
373
374   /* invalid start */
375   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
376   fail_unless (res == FALSE);
377
378   /* start outside */
379   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
380   fail_unless (res == TRUE);
381   fail_unless (cstart == 100);
382   fail_unless (cstop == 180);
383
384   /* start on lower bound */
385   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
386   fail_unless (res == TRUE);
387   fail_unless (cstart == 100);
388   fail_unless (cstop == 180);
389
390   /* start inside */
391   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
392   fail_unless (res == TRUE);
393   fail_unless (cstart == 150);
394   fail_unless (cstop == 180);
395
396   /* start outside on boundary */
397   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
398   fail_unless (res == FALSE);
399
400   /* start completely outside */
401   res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
402   fail_unless (res == FALSE);
403 }
404
405 GST_END_TEST;
406
407 GST_START_TEST (segment_seek_reverse)
408 {
409   GstSegment segment;
410   gboolean update;
411
412   gst_segment_init (&segment, GST_FORMAT_BYTES);
413   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
414
415   /* configure segment to stop 100 */
416   gst_segment_set_seek (&segment, -1.0,
417       GST_FORMAT_BYTES,
418       GST_SEEK_FLAG_NONE,
419       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, 100, &update);
420   fail_unless (segment.start == 0);
421   fail_unless (segment.stop == 100);
422   fail_unless (segment.time == 0);
423   fail_unless (segment.last_stop == 100);
424   fail_unless (update == TRUE);
425
426   /* update */
427   gst_segment_set_seek (&segment, -1.0,
428       GST_FORMAT_BYTES,
429       GST_SEEK_FLAG_NONE,
430       GST_SEEK_TYPE_SET, 10, GST_SEEK_TYPE_CUR, -20, &update);
431   fail_unless (segment.start == 10);
432   fail_unless (segment.stop == 80);
433   fail_unless (segment.time == 10);
434   fail_unless (segment.last_stop == 80);
435   fail_unless (update == TRUE);
436
437   gst_segment_set_seek (&segment, -1.0,
438       GST_FORMAT_BYTES,
439       GST_SEEK_FLAG_NONE,
440       GST_SEEK_TYPE_SET, 20, GST_SEEK_TYPE_NONE, 0, &update);
441   fail_unless (segment.start == 20);
442   fail_unless (segment.stop == 80);
443   fail_unless (segment.time == 20);
444   fail_unless (segment.last_stop == 80);
445   fail_unless (update == FALSE);
446 }
447
448 GST_END_TEST;
449
450 /* mess with the segment structure in the bytes format */
451 GST_START_TEST (segment_seek_rate)
452 {
453   GstSegment segment;
454   gboolean update;
455
456   gst_segment_init (&segment, GST_FORMAT_BYTES);
457
458   /* configure segment to rate 2.0, format does not matter when we don't specify
459    * a start or stop position. */
460   gst_segment_set_seek (&segment, 2.0,
461       GST_FORMAT_UNDEFINED,
462       GST_SEEK_FLAG_NONE,
463       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1, &update);
464   fail_unless (segment.format == GST_FORMAT_BYTES);
465   fail_unless (segment.start == 0);
466   fail_unless (segment.stop == -1);
467   fail_unless (segment.rate == 2.0);
468   fail_unless (update == FALSE);
469
470   /* 0 is the same in all formats and should not fail */
471   gst_segment_set_seek (&segment, 2.0,
472       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
473       GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_NONE, -1, &update);
474   fail_unless (segment.format == GST_FORMAT_BYTES);
475
476   /* set to -1 means start from 0 */
477   gst_segment_set_seek (&segment, 2.0,
478       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
479       GST_SEEK_TYPE_SET, -1, GST_SEEK_TYPE_NONE, -1, &update);
480   fail_unless (segment.format == GST_FORMAT_BYTES);
481   fail_unless (segment.start == 0);
482
483   gst_segment_set_seek (&segment, 2.0,
484       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
485       GST_SEEK_TYPE_CUR, 0, GST_SEEK_TYPE_NONE, -1, &update);
486
487   gst_segment_set_seek (&segment, 2.0,
488       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
489       GST_SEEK_TYPE_END, 0, GST_SEEK_TYPE_NONE, -1, &update);
490
491   /* -1 for end is fine too in all formats */
492   gst_segment_set_seek (&segment, 2.0,
493       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
494       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
495
496   /* 0 as relative end is fine too */
497   gst_segment_set_seek (&segment, 2.0,
498       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
499       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
500
501   gst_segment_set_seek (&segment, 2.0,
502       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
503       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
504
505   /* set a real stop position, this must happen in bytes */
506   gst_segment_set_seek (&segment, 3.0,
507       GST_FORMAT_BYTES,
508       GST_SEEK_FLAG_NONE,
509       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, 100, &update);
510   fail_unless (segment.format == GST_FORMAT_BYTES);
511   fail_unless (segment.start == 0);
512   fail_unless (segment.stop == 100);
513   fail_unless (segment.rate == 3.0);
514   /* no seek should happen, we just updated the stop position in forward
515    * playback mode.*/
516   fail_unless (update == FALSE);
517
518   /* 0 as relative end is fine too */
519   gst_segment_set_seek (&segment, 2.0,
520       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
521       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
522   fail_unless (segment.stop == 100);
523
524   gst_segment_set_seek (&segment, 2.0,
525       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
526       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
527   fail_unless (segment.stop == 100);
528
529   /* -1 for end is fine too in all formats */
530   gst_segment_set_seek (&segment, 2.0,
531       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
532       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
533   fail_unless (segment.stop == -1);
534
535   /* set some duration, stop -1 END seeks will now work with the
536    * duration, if the formats match */
537   gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
538   fail_unless (segment.duration == 200);
539
540   /* seek to end in any format with 0 should set the stop to the
541    * duration */
542   gst_segment_set_seek (&segment, 2.0,
543       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
544       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
545   fail_unless (segment.stop == 200);
546   fail_unless (segment.duration == 200);
547
548   /* subtract 100 from the end */
549   gst_segment_set_seek (&segment, 2.0,
550       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
551       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, -100, &update);
552   fail_unless (segment.stop == 100);
553   fail_unless (segment.duration == 200);
554
555   /* add 100 to the duration, this should be clamped to the duration */
556   gst_segment_set_seek (&segment, 2.0,
557       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
558       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 100, &update);
559   fail_unless (segment.stop == 200);
560   fail_unless (segment.duration == 200);
561
562   /* add 300 to the start, this should be clamped to the duration */
563   gst_segment_set_seek (&segment, 2.0,
564       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
565       GST_SEEK_TYPE_CUR, 300, GST_SEEK_TYPE_END, 0, &update);
566   fail_unless (segment.start == 200);
567   fail_unless (segment.stop == 200);
568   fail_unless (segment.duration == 200);
569
570   /* subtract 300 from the start, this should be clamped to 0 */
571   gst_segment_set_seek (&segment, 2.0,
572       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
573       GST_SEEK_TYPE_CUR, -300, GST_SEEK_TYPE_END, 0, &update);
574   fail_unless (segment.start == 0);
575   fail_unless (segment.stop == 200);
576   fail_unless (segment.duration == 200);
577 }
578
579 GST_END_TEST;
580
581 /* mess with the segment structure in the bytes format */
582 GST_START_TEST (segment_newsegment_open)
583 {
584   GstSegment segment;
585
586   gst_segment_init (&segment, GST_FORMAT_BYTES);
587
588   /* time should also work for starting from 0 */
589   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0, -1,
590       0);
591
592   fail_unless (segment.rate == 1.0);
593   fail_unless (segment.format == GST_FORMAT_BYTES);
594   fail_unless (segment.flags == 0);
595   fail_unless (segment.start == 0);
596   fail_unless (segment.stop == -1);
597   fail_unless (segment.time == 0);
598   fail_unless (segment.accum == 0);
599   fail_unless (segment.last_stop == 0);
600   fail_unless (segment.duration == -1);
601
602   /* we set stop but in the wrong format, stop stays open. */
603   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0,
604       200, 0);
605
606   fail_unless (segment.start == 0);
607   fail_unless (segment.stop == -1);
608   fail_unless (segment.time == 0);
609   fail_unless (segment.accum == 0);
610   fail_unless (segment.last_stop == 0);
611
612   /* update, nothing changes */
613   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 0, -1,
614       0);
615
616   fail_unless (segment.start == 0);
617   fail_unless (segment.stop == -1);
618   fail_unless (segment.time == 0);
619   fail_unless (segment.accum == 0);
620   fail_unless (segment.last_stop == 0);
621
622   /* update */
623   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
624       GST_FORMAT_BYTES, 100, -1, 100);
625
626   fail_unless (segment.start == 100);
627   fail_unless (segment.stop == -1);
628   fail_unless (segment.time == 100);
629   fail_unless (segment.accum == 100);
630   fail_unless (segment.last_stop == 100);
631
632   /* last_stop 0, accum does not change */
633   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
634       -1, 0);
635
636   fail_unless (segment.start == 0);
637   fail_unless (segment.stop == -1);
638   fail_unless (segment.time == 0);
639   fail_unless (segment.accum == 100);
640
641   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
642
643   fail_unless (segment.last_stop == 200);
644
645   /* last_stop 200, accum changes */
646   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
647       -1, 0);
648
649   fail_unless (segment.start == 0);
650   fail_unless (segment.stop == -1);
651   fail_unless (segment.time == 0);
652   fail_unless (segment.accum == 300);
653   fail_unless (segment.last_stop == 0);
654 }
655
656 GST_END_TEST;
657
658
659 /* mess with the segment structure in the bytes format */
660 GST_START_TEST (segment_newsegment_closed)
661 {
662   GstSegment segment;
663
664   gst_segment_init (&segment, GST_FORMAT_BYTES);
665
666   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
667       GST_FORMAT_BYTES, 0, 200, 0);
668
669   fail_unless (segment.rate == 1.0);
670   fail_unless (segment.format == GST_FORMAT_BYTES);
671   fail_unless (segment.flags == 0);
672   fail_unless (segment.start == 0);
673   fail_unless (segment.stop == 200);
674   fail_unless (segment.time == 0);
675   fail_unless (segment.accum == 0);
676   fail_unless (segment.last_stop == 0);
677   fail_unless (segment.duration == -1);
678
679   /* assume we advanced to position 40 */
680   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 40);
681   fail_unless (segment.last_stop == 40);
682
683   /* do an update to the start, last_stop is unchanged because it's bigger */
684   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 20,
685       200, 20);
686
687   fail_unless (segment.start == 20);
688   fail_unless (segment.stop == 200);
689   fail_unless (segment.time == 20);
690   fail_unless (segment.accum == 20);
691   fail_unless (segment.last_stop == 40);
692
693   /* do an update past our last_stop, it should be updated now */
694   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 50,
695       300, 50);
696
697   fail_unless (segment.start == 50);
698   fail_unless (segment.stop == 300);
699   fail_unless (segment.time == 50);
700   fail_unless (segment.accum == 50);
701   fail_unless (segment.last_stop == 50);
702
703   /* and a new accumulated one */
704   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
705       GST_FORMAT_BYTES, 100, 400, 300);
706
707   fail_unless (segment.start == 100);
708   fail_unless (segment.stop == 400);
709   fail_unless (segment.time == 300);
710   fail_unless (segment.accum == 300);
711
712   /* and a new updated one */
713   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
714       GST_FORMAT_BYTES, 100, 500, 300);
715
716   fail_unless (segment.start == 100);
717   fail_unless (segment.stop == 500);
718   fail_unless (segment.time == 300);
719   fail_unless (segment.accum == 300);
720
721   /* and a new partially updated one */
722   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
723       GST_FORMAT_BYTES, 200, 500, 400);
724
725   fail_unless (segment.start == 200);
726   fail_unless (segment.stop == 500);
727   fail_unless (segment.time == 400);
728   fail_unless (segment.accum == 400);
729 }
730
731 GST_END_TEST;
732
733 /* mess with the segment structure in the time format */
734 GST_START_TEST (segment_newsegment_streamtime)
735 {
736   GstSegment segment;
737   gint64 result;
738
739   gst_segment_init (&segment, GST_FORMAT_TIME);
740
741   /***************************
742    * Normal segment
743    ***************************/
744   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
745       GST_FORMAT_TIME, 0, 200, 0);
746
747   fail_unless (segment.rate == 1.0);
748   fail_unless (segment.applied_rate == 1.0);
749   fail_unless (segment.format == GST_FORMAT_TIME);
750   fail_unless (segment.flags == 0);
751   fail_unless (segment.start == 0);
752   fail_unless (segment.stop == 200);
753   fail_unless (segment.time == 0);
754   fail_unless (segment.accum == 0);
755   fail_unless (segment.last_stop == 0);
756   fail_unless (segment.duration == -1);
757
758   /* invalid time gives invalid result */
759   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
760   fail_unless (result == -1);
761
762   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
763   fail_unless (result == 0);
764
765   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
766   fail_unless (result == 100);
767
768   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
769   fail_unless (result == 200);
770
771   /* outside of the segment */
772   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
773   fail_unless (result == -1);
774
775   /*********************
776    * time shifted by 500
777    *********************/
778   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
779       GST_FORMAT_TIME, 0, 200, 500);
780
781   fail_unless (segment.accum == 200);
782
783   /* invalid time gives invalid result */
784   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
785   fail_unless (result == -1);
786
787   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
788   fail_unless (result == 500);
789
790   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
791   fail_unless (result == 600);
792
793   /* outside of the segment */
794   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
795   fail_unless (result == -1);
796
797   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
798   fail_unless (result == -1);
799
800   /*********************
801    * time offset by 500
802    *********************/
803   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
804       GST_FORMAT_TIME, 500, 700, 0);
805
806   fail_unless (segment.accum == 400);
807
808   /* invalid time gives invalid result */
809   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
810   fail_unless (result == -1);
811
812   /* before segment is invalid */
813   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
814   fail_unless (result == -1);
815
816   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
817   fail_unless (result == 0);
818
819   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
820   fail_unless (result == 100);
821
822   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
823   fail_unless (result == 200);
824
825   /* outside of the segment */
826   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
827   fail_unless (result == -1);
828
829   /*************************************
830    * time offset by 500, shifted by 200
831    *************************************/
832   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
833       GST_FORMAT_TIME, 500, 700, 200);
834
835   fail_unless (segment.accum == 600);
836
837   /* invalid time gives invalid result */
838   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
839   fail_unless (result == -1);
840
841   /* before segment is invalid */
842   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
843   fail_unless (result == -1);
844
845   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
846   fail_unless (result == 200);
847
848   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
849   fail_unless (result == 300);
850
851   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
852   fail_unless (result == 400);
853
854   /* outside of the segment */
855   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
856   fail_unless (result == -1);
857 }
858
859 GST_END_TEST;
860
861 /* mess with the segment structure in the time format */
862 GST_START_TEST (segment_newsegment_streamtime_rate)
863 {
864   GstSegment segment;
865   gint64 result;
866
867   gst_segment_init (&segment, GST_FORMAT_TIME);
868
869   /***************************
870    * Normal segment rate 2.0
871    ***************************/
872   gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
873       GST_FORMAT_TIME, 0, 200, 0);
874
875   fail_unless (segment.rate == 2.0);
876   fail_unless (segment.applied_rate == 1.0);
877   fail_unless (segment.format == GST_FORMAT_TIME);
878   fail_unless (segment.flags == 0);
879   fail_unless (segment.start == 0);
880   fail_unless (segment.stop == 200);
881   fail_unless (segment.time == 0);
882   fail_unless (segment.accum == 0);
883   fail_unless (segment.last_stop == 0);
884   fail_unless (segment.duration == -1);
885
886   /* invalid time gives invalid result */
887   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
888   fail_unless (result == -1);
889
890   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
891   fail_unless (result == 0);
892
893   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
894   fail_unless (result == 100);
895
896   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
897   fail_unless (result == 150);
898
899   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
900   fail_unless (result == 200);
901
902   /* outside of the segment */
903   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
904   fail_unless (result == -1);
905
906   /***************************************
907    * Normal segment rate 2.0, offset
908    ***************************************/
909   gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
910       GST_FORMAT_TIME, 100, 300, 0);
911
912   fail_unless (segment.accum == 100);
913
914   /* invalid time gives invalid result */
915   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
916   fail_unless (result == -1);
917
918   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
919   fail_unless (result == 0);
920
921   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
922   fail_unless (result == 100);
923
924   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
925   fail_unless (result == 150);
926
927   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
928   fail_unless (result == 200);
929
930   /* outside of the segment */
931   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
932   fail_unless (result == -1);
933
934   /***************************************
935    * Normal segment rate -1.0, offset
936    ***************************************/
937
938   /* buffers will arrive from 300 to 100 in a sink, stream time
939    * calculation is unaffected by the rate */
940   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
941       GST_FORMAT_TIME, 100, 300, 0);
942
943   fail_unless (segment.accum == 200);
944
945   /* invalid time gives invalid result */
946   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
947   fail_unless (result == -1);
948
949   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
950   fail_unless (result == 0);
951
952   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
953   fail_unless (result == 100);
954
955   /***********************************************
956    * Normal segment rate -1.0, offset, time = 200
957    ***********************************************/
958   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
959       GST_FORMAT_TIME, 100, 300, 200);
960
961   /* invalid time gives invalid result */
962   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
963   fail_unless (result == -1);
964
965   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
966   fail_unless (result == 200);
967
968   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
969   fail_unless (result == 300);
970
971   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
972   fail_unless (result == 400);
973
974   /* outside of the segment */
975   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
976   fail_unless (result == -1);
977 }
978
979 GST_END_TEST;
980
981 /* mess with the segment structure in the time format */
982 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
983 {
984   GstSegment segment;
985   gint64 result;
986
987   gst_segment_init (&segment, GST_FORMAT_TIME);
988
989   /***********************************************************
990    * Normal segment rate 1.0, applied rate -1.0
991    * This means the timestamps represents a stream going backwards
992    * starting from @time to 0.
993    ************************************************************/
994   gst_segment_set_newsegment (&segment, FALSE, 1.0, -1.0,
995       GST_FORMAT_TIME, 0, 200, 200);
996
997   fail_unless (segment.rate == 1.0);
998   fail_unless (segment.applied_rate == -1.0);
999   fail_unless (segment.format == GST_FORMAT_TIME);
1000   fail_unless (segment.flags == 0);
1001   fail_unless (segment.start == 0);
1002   fail_unless (segment.stop == 200);
1003   fail_unless (segment.time == 200);
1004   fail_unless (segment.accum == 0);
1005   fail_unless (segment.last_stop == 0);
1006   fail_unless (segment.duration == -1);
1007
1008   /* invalid time gives invalid result */
1009   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1010   fail_unless (result == -1);
1011
1012   /* we count backwards from 200 */
1013   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1014   fail_unless (result == 200);
1015
1016   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1017   fail_unless (result == 100);
1018
1019   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1020   fail_unless (result == 50);
1021
1022   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1023   fail_unless (result == 0);
1024
1025   /* outside of the segment */
1026   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1027   fail_unless (result == -1);
1028
1029   /***********************************************************
1030    * Normal segment rate 1.0, applied rate 2.0
1031    * This means the timestamps represents a stream at twice the
1032    * normal rate
1033    ************************************************************/
1034   gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1035       GST_FORMAT_TIME, 0, 200, 0);
1036
1037   fail_unless (segment.rate == 1.0);
1038   fail_unless (segment.applied_rate == 2.0);
1039   fail_unless (segment.format == GST_FORMAT_TIME);
1040   fail_unless (segment.flags == 0);
1041   fail_unless (segment.start == 0);
1042   fail_unless (segment.stop == 200);
1043   fail_unless (segment.time == 0);
1044   fail_unless (segment.accum == 200);
1045   fail_unless (segment.last_stop == 0);
1046   fail_unless (segment.duration == -1);
1047
1048   /* invalid time gives invalid result */
1049   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1050   fail_unless (result == -1);
1051
1052   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1053   fail_unless (result == 0);
1054
1055   /* the stream prepresents a stream going twice as fast, the position 
1056    * in the segment is therefore scaled by the applied rate */
1057   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1058   fail_unless (result == 200);
1059
1060   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1061   fail_unless (result == 300);
1062
1063   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1064   fail_unless (result == 400);
1065
1066   /* outside of the segment */
1067   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1068   fail_unless (result == -1);
1069
1070   /***********************************************************
1071    * Normal segment rate 1.0, applied rate -2.0
1072    * This means the timestamps represents a stream at twice the
1073    * reverse rate
1074    ************************************************************/
1075   gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1076       GST_FORMAT_TIME, 0, 200, 400);
1077
1078   fail_unless (segment.rate == 1.0);
1079   fail_unless (segment.applied_rate == -2.0);
1080   fail_unless (segment.format == GST_FORMAT_TIME);
1081   fail_unless (segment.flags == 0);
1082   fail_unless (segment.start == 0);
1083   fail_unless (segment.stop == 200);
1084   fail_unless (segment.time == 400);
1085   /* previous segment lasted 200, rate of 2.0 was already applied */
1086   fail_unless (segment.accum == 400);
1087   fail_unless (segment.last_stop == 0);
1088   fail_unless (segment.duration == -1);
1089
1090   /* invalid time gives invalid result */
1091   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1092   fail_unless (result == -1);
1093
1094   /* we count backwards from 400 */
1095   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1096   fail_unless (result == 400);
1097
1098   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1099   fail_unless (result == 200);
1100
1101   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1102   fail_unless (result == 100);
1103
1104   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1105   fail_unless (result == 0);
1106
1107   /* outside of the segment */
1108   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1109   fail_unless (result == -1);
1110
1111   /***********************************************************
1112    * Normal segment rate 1.0, applied rate -2.0
1113    * This means the timestamps represents a stream at twice the
1114    * reverse rate, start time cannot compensate the complete
1115    * duration of the segment so we stop at 0
1116    ************************************************************/
1117   gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1118       GST_FORMAT_TIME, 0, 200, 200);
1119
1120   fail_unless (segment.rate == 1.0);
1121   fail_unless (segment.applied_rate == -2.0);
1122   fail_unless (segment.format == GST_FORMAT_TIME);
1123   fail_unless (segment.flags == 0);
1124   fail_unless (segment.start == 0);
1125   fail_unless (segment.stop == 200);
1126   fail_unless (segment.time == 200);
1127   fail_unless (segment.accum == 600);
1128   fail_unless (segment.last_stop == 0);
1129   fail_unless (segment.duration == -1);
1130
1131   /* invalid time gives invalid result */
1132   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1133   fail_unless (result == -1);
1134
1135   /* we count backwards from 200 */
1136   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1137   fail_unless (result == 200);
1138
1139   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1140   fail_unless (result == 0);
1141
1142   /* clamp at 0 */
1143   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1144   fail_unless (result == 0);
1145
1146   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1147   fail_unless (result == 0);
1148
1149   /* outside of the segment */
1150   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1151   fail_unless (result == -1);
1152 }
1153
1154 GST_END_TEST;
1155
1156 /* mess with the segment structure in the time format */
1157 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
1158 {
1159   GstSegment segment;
1160   gint64 result;
1161
1162   gst_segment_init (&segment, GST_FORMAT_TIME);
1163
1164   /***********************************************************
1165    * Segment rate 2.0, applied rate 2.0
1166    * this means we have a double speed stream that we should
1167    * speed up by a factor of 2.0 some more. the resulting
1168    * stream will be played at four times the speed. 
1169    ************************************************************/
1170   gst_segment_set_newsegment (&segment, FALSE, 2.0, 2.0,
1171       GST_FORMAT_TIME, 0, 200, 0);
1172
1173   fail_unless (segment.rate == 2.0);
1174   fail_unless (segment.applied_rate == 2.0);
1175   fail_unless (segment.format == GST_FORMAT_TIME);
1176   fail_unless (segment.flags == 0);
1177   fail_unless (segment.start == 0);
1178   fail_unless (segment.stop == 200);
1179   fail_unless (segment.time == 0);
1180   fail_unless (segment.accum == 0);
1181   fail_unless (segment.last_stop == 0);
1182   fail_unless (segment.duration == -1);
1183
1184   /* invalid time gives invalid result */
1185   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1186   fail_unless (result == -1);
1187
1188   /* only applied rate affects our calculation of the stream time */
1189   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1190   fail_unless (result == 0);
1191
1192   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1193   fail_unless (result == 200);
1194
1195   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1196   fail_unless (result == 300);
1197
1198   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1199   fail_unless (result == 400);
1200
1201   /* outside of the segment */
1202   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1203   fail_unless (result == -1);
1204
1205   /***********************************************************
1206    * Segment rate 2.0, applied rate -1.0
1207    * this means we have a reverse stream that we should
1208    * speed up by a factor of 2.0
1209    ************************************************************/
1210   gst_segment_set_newsegment (&segment, FALSE, 2.0, -1.0,
1211       GST_FORMAT_TIME, 0, 200, 200);
1212
1213   fail_unless (segment.rate == 2.0);
1214   fail_unless (segment.applied_rate == -1.0);
1215   fail_unless (segment.format == GST_FORMAT_TIME);
1216   fail_unless (segment.flags == 0);
1217   fail_unless (segment.start == 0);
1218   fail_unless (segment.stop == 200);
1219   fail_unless (segment.time == 200);
1220   /* previous segment lasted 100 */
1221   fail_unless (segment.accum == 100);
1222   fail_unless (segment.last_stop == 0);
1223   fail_unless (segment.duration == -1);
1224
1225   /* invalid time gives invalid result */
1226   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1227   fail_unless (result == -1);
1228
1229   /* only applied rate affects our calculation of the stream time */
1230   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1231   fail_unless (result == 200);
1232
1233   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1234   fail_unless (result == 100);
1235
1236   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1237   fail_unless (result == 50);
1238
1239   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1240   fail_unless (result == 0);
1241
1242   /* outside of the segment */
1243   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1244   fail_unless (result == -1);
1245
1246   /***********************************************************
1247    * Segment rate -1.0, applied rate -1.0
1248    * this means we have a reverse stream that we should
1249    * reverse to get the normal stream again.
1250    ************************************************************/
1251   gst_segment_set_newsegment (&segment, FALSE, -1.0, -1.0,
1252       GST_FORMAT_TIME, 0, 200, 200);
1253
1254   fail_unless (segment.rate == -1.0);
1255   fail_unless (segment.applied_rate == -1.0);
1256   fail_unless (segment.format == GST_FORMAT_TIME);
1257   fail_unless (segment.flags == 0);
1258   fail_unless (segment.start == 0);
1259   fail_unless (segment.stop == 200);
1260   fail_unless (segment.time == 200);
1261   /* accumulated 100 of previous segment to make 200 */
1262   fail_unless (segment.accum == 200);
1263   fail_unless (segment.last_stop == 200);
1264   fail_unless (segment.duration == -1);
1265
1266   /* invalid time gives invalid result */
1267   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1268   fail_unless (result == -1);
1269
1270   /* only applied rate affects our calculation of the stream time */
1271   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1272   fail_unless (result == 200);
1273
1274   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1275   fail_unless (result == 100);
1276
1277   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1278   fail_unless (result == 50);
1279
1280   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1281   fail_unless (result == 0);
1282
1283   /* outside of the segment */
1284   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1285   fail_unless (result == -1);
1286
1287   /***********************************************************
1288    * Segment rate -1.0, applied rate -1.0
1289    * this means we have a reverse stream that we should
1290    * reverse to get the normal stream again.
1291    ************************************************************/
1292   gst_segment_set_newsegment (&segment, FALSE, -1.0, 2.0,
1293       GST_FORMAT_TIME, 0, 200, 0);
1294
1295   fail_unless (segment.rate == -1.0);
1296   fail_unless (segment.applied_rate == 2.0);
1297   fail_unless (segment.format == GST_FORMAT_TIME);
1298   fail_unless (segment.flags == 0);
1299   fail_unless (segment.start == 0);
1300   fail_unless (segment.stop == 200);
1301   fail_unless (segment.time == 0);
1302   fail_unless (segment.accum == 400);
1303   fail_unless (segment.last_stop == 200);
1304   fail_unless (segment.duration == -1);
1305
1306   /* invalid time gives invalid result */
1307   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1308   fail_unless (result == -1);
1309
1310   /* only applied rate affects our calculation of the stream time */
1311   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1312   fail_unless (result == 0);
1313
1314   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1315   fail_unless (result == 200);
1316
1317   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1318   fail_unless (result == 300);
1319
1320   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1321   fail_unless (result == 400);
1322
1323   /* outside of the segment */
1324   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1325   fail_unless (result == -1);
1326 }
1327
1328 GST_END_TEST;
1329
1330 /* mess with the segment structure in the time format */
1331 GST_START_TEST (segment_newsegment_runningtime)
1332 {
1333   GstSegment segment;
1334   gint64 result;
1335
1336   gst_segment_init (&segment, GST_FORMAT_TIME);
1337
1338   /***************************
1339    * Normal segment
1340    ***************************/
1341   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1342       GST_FORMAT_TIME, 0, 200, 0);
1343
1344   fail_unless (segment.rate == 1.0);
1345   fail_unless (segment.applied_rate == 1.0);
1346   fail_unless (segment.format == GST_FORMAT_TIME);
1347   fail_unless (segment.flags == 0);
1348   fail_unless (segment.start == 0);
1349   fail_unless (segment.stop == 200);
1350   fail_unless (segment.time == 0);
1351   fail_unless (segment.accum == 0);
1352   fail_unless (segment.last_stop == 0);
1353   fail_unless (segment.duration == -1);
1354
1355   /* invalid time gives invalid result */
1356   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1357   fail_unless (result == -1);
1358
1359   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1360   fail_unless (result == 0);
1361   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1362   fail_unless (result == 0);
1363
1364   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1365   fail_unless (result == 100);
1366   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1367   fail_unless (result == 100);
1368
1369   /* at edge is exactly the segment duration */
1370   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1371   fail_unless (result == 200);
1372   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1373   fail_unless (result == 200);
1374
1375   /* outside of the segment */
1376   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1377   fail_unless (result == -1);
1378   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 300);
1379   fail_unless (result == -1);
1380
1381   /***********************************************************
1382    * time shifted by 500, check if accumulation worked.
1383    * Rate convert to twice the speed which means scaling down
1384    * all positions by 2.0 in this segment.
1385    * Then time argument is not used at all here.
1386    ***********************************************************/
1387   gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
1388       GST_FORMAT_TIME, 0, 200, 500);
1389
1390   /* normal speed gives elapsed of 200 */
1391   fail_unless (segment.accum == 200);
1392
1393   /* invalid time gives invalid result */
1394   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1395   fail_unless (result == -1);
1396
1397   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1398   fail_unless (result == 200);
1399   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1400   fail_unless (result == 0);
1401
1402   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1403   fail_unless (result == 250);
1404   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1405   fail_unless (result == 100);
1406
1407   /* outside of the segment */
1408   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1409   fail_unless (result == -1);
1410   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 310);
1411   fail_unless (result == -1);
1412
1413   /********************************************
1414    * time offset by 500
1415    * applied rate is not used for running time
1416    ********************************************/
1417   gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1418       GST_FORMAT_TIME, 500, 700, 0);
1419
1420   /* previous segment played at double speed gives elapsed time of
1421    * 100 added to previous accum of 200 gives 300. */
1422   fail_unless (segment.accum == 300);
1423
1424   /* invalid time gives invalid result */
1425   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1426   fail_unless (result == -1);
1427
1428   /* before segment is invalid */
1429   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1430   fail_unless (result == -1);
1431   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 200);
1432   fail_unless (result == -1);
1433
1434   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1435   fail_unless (result == 300);
1436   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1437   fail_unless (result == 500);
1438
1439   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1440   fail_unless (result == 400);
1441   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1442   fail_unless (result == 600);
1443
1444   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1445   fail_unless (result == 500);
1446   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1447   fail_unless (result == 700);
1448
1449   /* outside of the segment */
1450   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1451   fail_unless (result == -1);
1452   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1453   fail_unless (result == -1);
1454
1455   /**********************************************************
1456    * time offset by 500, shifted by 200
1457    * Negative rate makes the running time go backwards 
1458    * relative to the segment stop position. again time
1459    * is ignored.
1460    **********************************************************/
1461   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1462       GST_FORMAT_TIME, 500, 700, 200);
1463
1464   fail_unless (segment.accum == 500);
1465
1466   /* invalid time gives invalid result */
1467   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1468   fail_unless (result == -1);
1469
1470   /* before segment is invalid */
1471   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1472   fail_unless (result == -1);
1473   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 400);
1474   fail_unless (result == -1);
1475
1476   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1477   fail_unless (result == 700);
1478   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1479   fail_unless (result == 500);
1480
1481   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1482   fail_unless (result == 600);
1483   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1484   fail_unless (result == 600);
1485
1486   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1487   fail_unless (result == 500);
1488   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1489   fail_unless (result == 700);
1490
1491   /* outside of the segment */
1492   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1493   fail_unless (result == -1);
1494   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1495   fail_unless (result == -1);
1496
1497   /**********************************************************
1498    * time offset by 500, shifted by 200
1499    * Negative rate makes the running time go backwards at
1500    * twice speed relative to the segment stop position. again 
1501    * time is ignored.
1502    **********************************************************/
1503   gst_segment_set_newsegment (&segment, FALSE, -2.0, -2.0,
1504       GST_FORMAT_TIME, 500, 700, 200);
1505
1506   fail_unless (segment.accum == 700);
1507
1508   /* invalid time gives invalid result */
1509   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1510   fail_unless (result == -1);
1511
1512   /* before segment is invalid */
1513   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1514   fail_unless (result == -1);
1515   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1516   fail_unless (result == -1);
1517
1518   /* total scaled segment time is 100, accum is 700, so we get 800 */
1519   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1520   fail_unless (result == 800);
1521   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1522   fail_unless (result == 500);
1523
1524   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1525   fail_unless (result == 750);
1526   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1527   fail_unless (result == 600);
1528
1529   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1530   fail_unless (result == 700);
1531   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1532   fail_unless (result == 700);
1533
1534   /* outside of the segment */
1535   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1536   fail_unless (result == -1);
1537   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 900);
1538   fail_unless (result == -1);
1539
1540   /* see if negative rate closed segment correctly */
1541   gst_segment_set_newsegment (&segment, FALSE, -2.0, -1.0,
1542       GST_FORMAT_TIME, 500, 700, 200);
1543
1544   /* previous segment lasted 100, and was at 700 so we should get 800 */
1545   fail_unless (segment.accum == 800);
1546   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1547   fail_unless (result == 700);
1548 }
1549
1550 GST_END_TEST;
1551
1552 /* mess with the segment structure in the time format */
1553 GST_START_TEST (segment_newsegment_accum)
1554 {
1555   GstSegment segment;
1556   gint64 result;
1557
1558   gst_segment_init (&segment, GST_FORMAT_TIME);
1559
1560   /***************************
1561    * Normal reverse segment
1562    ***************************/
1563   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1564       GST_FORMAT_TIME, 0, 200, 0);
1565
1566   fail_unless (segment.rate == -1.0);
1567   fail_unless (segment.applied_rate == 1.0);
1568   fail_unless (segment.format == GST_FORMAT_TIME);
1569   fail_unless (segment.flags == 0);
1570   fail_unless (segment.start == 0);
1571   fail_unless (segment.stop == 200);
1572   fail_unless (segment.time == 0);
1573   fail_unless (segment.accum == 0);
1574   fail_unless (segment.last_stop == 200);
1575   fail_unless (segment.duration == -1);
1576
1577   /* invalid time gives invalid result */
1578   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1579   fail_unless (result == -1);
1580
1581   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1582   fail_unless (result == 0);
1583   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1584   fail_unless (result == 200);
1585
1586   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1587   fail_unless (result == 50);
1588   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1589   fail_unless (result == 150);
1590
1591   /* update segment, this accumulates 50 from the previous segment. */
1592   gst_segment_set_newsegment (&segment, TRUE, -2.0, 1.0,
1593       GST_FORMAT_TIME, 0, 150, 0);
1594
1595   fail_unless (segment.rate == -2.0);
1596   fail_unless (segment.applied_rate == 1.0);
1597   fail_unless (segment.format == GST_FORMAT_TIME);
1598   fail_unless (segment.flags == 0);
1599   fail_unless (segment.start == 0);
1600   fail_unless (segment.stop == 150);
1601   fail_unless (segment.time == 0);
1602   fail_unless (segment.accum == 50);
1603   fail_unless (segment.last_stop == 150);
1604   fail_unless (segment.duration == -1);
1605
1606   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1607   fail_unless (result == 50);
1608   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1609   fail_unless (result == 150);
1610
1611   /* 50 accumulated + 50 / 2 */
1612   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1613   fail_unless (result == 75);
1614   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1615   fail_unless (result == 100);
1616
1617   /* update segment, this does not accumulate anything. */
1618   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
1619       GST_FORMAT_TIME, 100, 200, 100);
1620
1621   fail_unless (segment.rate == 1.0);
1622   fail_unless (segment.applied_rate == 1.0);
1623   fail_unless (segment.format == GST_FORMAT_TIME);
1624   fail_unless (segment.flags == 0);
1625   fail_unless (segment.start == 100);
1626   fail_unless (segment.stop == 200);
1627   fail_unless (segment.time == 100);
1628   fail_unless (segment.accum == 50);
1629   fail_unless (segment.last_stop == 150);
1630   fail_unless (segment.duration == -1);
1631
1632   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1633   fail_unless (result == 50);
1634   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1635   fail_unless (result == 100);
1636
1637   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1638   fail_unless (result == 100);
1639   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1640   fail_unless (result == 150);
1641 }
1642
1643 GST_END_TEST;
1644
1645 /* mess with the segment structure in the time format */
1646 GST_START_TEST (segment_newsegment_accum2)
1647 {
1648   GstSegment segment;
1649   gint64 result;
1650
1651   gst_segment_init (&segment, GST_FORMAT_TIME);
1652
1653   /***************************
1654    * Normal reverse segment
1655    ***************************/
1656   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1657       GST_FORMAT_TIME, 0, 200, 0);
1658
1659   fail_unless (segment.rate == -1.0);
1660   fail_unless (segment.applied_rate == 1.0);
1661   fail_unless (segment.format == GST_FORMAT_TIME);
1662   fail_unless (segment.flags == 0);
1663   fail_unless (segment.start == 0);
1664   fail_unless (segment.stop == 200);
1665   fail_unless (segment.time == 0);
1666   fail_unless (segment.accum == 0);
1667   fail_unless (segment.last_stop == 200);
1668   fail_unless (segment.duration == -1);
1669
1670   /* invalid time gives invalid result */
1671   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1672   fail_unless (result == -1);
1673   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1674   fail_unless (result == -1);
1675
1676   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1677   fail_unless (result == 0);
1678   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1679   fail_unless (result == 200);
1680
1681   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1682   fail_unless (result == 50);
1683   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1684   fail_unless (result == 150);
1685
1686   /* close segment, this accumulates nothing. */
1687   gst_segment_set_newsegment (&segment, TRUE, -1.0, 1.0,
1688       GST_FORMAT_TIME, 150, 200, 0);
1689
1690   fail_unless (segment.rate == -1.0);
1691   fail_unless (segment.applied_rate == 1.0);
1692   fail_unless (segment.format == GST_FORMAT_TIME);
1693   fail_unless (segment.flags == 0);
1694   fail_unless (segment.start == 150);
1695   fail_unless (segment.stop == 200);
1696   fail_unless (segment.time == 0);
1697   fail_unless (segment.accum == 0);
1698   fail_unless (segment.last_stop == 200);
1699   fail_unless (segment.duration == -1);
1700
1701   /* new segment, this accumulates 50. */
1702   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1703       GST_FORMAT_TIME, 150, 300, 150);
1704
1705   fail_unless (segment.rate == 1.0);
1706   fail_unless (segment.applied_rate == 1.0);
1707   fail_unless (segment.format == GST_FORMAT_TIME);
1708   fail_unless (segment.flags == 0);
1709   fail_unless (segment.start == 150);
1710   fail_unless (segment.stop == 300);
1711   fail_unless (segment.time == 150);
1712   fail_unless (segment.accum == 50);
1713   fail_unless (segment.last_stop == 150);
1714   fail_unless (segment.duration == -1);
1715
1716   /* invalid time gives invalid result */
1717   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1718   fail_unless (result == -1);
1719
1720   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1721   fail_unless (result == 50);
1722   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1723   fail_unless (result == 150);
1724
1725   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1726   fail_unless (result == 100);
1727   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1728   fail_unless (result == 200);
1729 }
1730
1731 GST_END_TEST;
1732
1733 GST_START_TEST (segment_copy)
1734 {
1735   GstSegment *copy;
1736   GstSegment segment = { 0.0, };
1737
1738   /* this is a boxed type copy function, we support copying NULL */
1739   fail_unless (gst_segment_copy (NULL) == NULL);
1740
1741   gst_segment_init (&segment, GST_FORMAT_TIME);
1742
1743   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1744       GST_FORMAT_TIME, 0, 200, 0);
1745
1746   copy = gst_segment_copy (&segment);
1747   fail_unless (copy != NULL);
1748   /* we inited the struct on the stack to zeroes, so direct comparison should
1749    * be ok here despite the padding field and regardless of implementation */
1750   fail_unless (memcmp (copy, &segment, sizeof (GstSegment)) == 0);
1751   gst_segment_free (copy);
1752 }
1753
1754 GST_END_TEST;
1755
1756 static Suite *
1757 gst_segment_suite (void)
1758 {
1759   Suite *s = suite_create ("GstSegment");
1760   TCase *tc_chain = tcase_create ("segments");
1761
1762   tcase_set_timeout (tc_chain, 20);
1763
1764   suite_add_tcase (s, tc_chain);
1765   tcase_add_test (tc_chain, segment_seek_nosize);
1766   tcase_add_test (tc_chain, segment_seek_size);
1767   tcase_add_test (tc_chain, segment_seek_reverse);
1768   tcase_add_test (tc_chain, segment_seek_rate);
1769   tcase_add_test (tc_chain, segment_newsegment_open);
1770   tcase_add_test (tc_chain, segment_newsegment_closed);
1771   tcase_add_test (tc_chain, segment_newsegment_streamtime);
1772   tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1773   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1774   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1775   tcase_add_test (tc_chain, segment_newsegment_runningtime);
1776   tcase_add_test (tc_chain, segment_newsegment_accum);
1777   tcase_add_test (tc_chain, segment_newsegment_accum2);
1778   tcase_add_test (tc_chain, segment_copy);
1779
1780   return s;
1781 }
1782
1783 GST_CHECK_MAIN (gst_segment);