Merging origin/master
[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   guint64 cstart, cstop;
31   gboolean update;
32
33   gst_segment_init (&segment, GST_FORMAT_BYTES);
34
35   /* configure segment to start 100 */
36   gst_segment_do_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_do_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_do_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_do_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_do_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   guint64 cstart, cstop;
230   gboolean update;
231
232   gst_segment_init (&segment, GST_FORMAT_BYTES);
233   segment.duration = 200;
234
235   /* configure segment to start 100 */
236   gst_segment_do_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_do_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_do_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_do_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_do_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   segment.duration = 200;
414
415   /* configure segment to stop 100 */
416   gst_segment_do_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.position == 100);
424   fail_unless (update == TRUE);
425
426   /* update */
427   gst_segment_do_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.position == 80);
435   fail_unless (update == TRUE);
436
437   gst_segment_do_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.position == 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 */
459   gst_segment_do_seek (&segment, 2.0,
460       GST_FORMAT_BYTES,
461       GST_SEEK_FLAG_NONE,
462       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_NONE, -1, &update);
463   fail_unless (segment.format == GST_FORMAT_BYTES);
464   fail_unless (segment.start == 0);
465   fail_unless (segment.stop == -1);
466   fail_unless (segment.rate == 2.0);
467   fail_unless (update == FALSE);
468
469 #if 0
470   /* 0 is the same in all formats and should not fail */
471   gst_segment_do_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_do_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_do_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_do_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_do_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_do_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_do_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 #endif
505
506   /* set a real stop position, this must happen in bytes */
507   gst_segment_do_seek (&segment, 3.0,
508       GST_FORMAT_BYTES,
509       GST_SEEK_FLAG_NONE,
510       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, 100, &update);
511   fail_unless (segment.format == GST_FORMAT_BYTES);
512   fail_unless (segment.start == 0);
513   fail_unless (segment.stop == 100);
514   fail_unless (segment.rate == 3.0);
515   /* no seek should happen, we just updated the stop position in forward
516    * playback mode.*/
517   fail_unless (update == FALSE);
518
519 #if 0
520   /* 0 as relative end is fine too */
521   gst_segment_do_seek (&segment, 2.0,
522       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
523       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_CUR, 0, &update);
524   fail_unless (segment.stop == 100);
525
526   gst_segment_do_seek (&segment, 2.0,
527       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
528       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
529   fail_unless (segment.stop == 100);
530
531   /* -1 for end is fine too in all formats */
532   gst_segment_do_seek (&segment, 2.0,
533       GST_FORMAT_TIME, GST_SEEK_FLAG_NONE,
534       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_SET, -1, &update);
535   fail_unless (segment.stop == -1);
536 #endif
537
538   /* set some duration, stop -1 END seeks will now work with the
539    * duration, if the formats match */
540   segment.duration = 200;
541   fail_unless (segment.duration == 200);
542
543   /* seek to end with 0 should set the stop to the duration */
544   gst_segment_do_seek (&segment, 2.0,
545       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
546       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 0, &update);
547   fail_unless (segment.stop == 200);
548   fail_unless (segment.duration == 200);
549
550   /* subtract 100 from the end */
551   gst_segment_do_seek (&segment, 2.0,
552       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
553       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, -100, &update);
554   fail_unless (segment.stop == 100);
555   fail_unless (segment.duration == 200);
556
557   /* add 100 to the duration, this should be clamped to the duration */
558   gst_segment_do_seek (&segment, 2.0,
559       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
560       GST_SEEK_TYPE_NONE, -1, GST_SEEK_TYPE_END, 100, &update);
561   fail_unless (segment.stop == 200);
562   fail_unless (segment.duration == 200);
563
564   /* add 300 to the start, this should be clamped to the duration */
565   gst_segment_do_seek (&segment, 2.0,
566       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
567       GST_SEEK_TYPE_CUR, 300, GST_SEEK_TYPE_END, 0, &update);
568   fail_unless (segment.start == 200);
569   fail_unless (segment.stop == 200);
570   fail_unless (segment.duration == 200);
571
572   /* subtract 300 from the start, this should be clamped to 0 */
573   gst_segment_do_seek (&segment, 2.0,
574       GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE,
575       GST_SEEK_TYPE_CUR, -300, GST_SEEK_TYPE_END, 0, &update);
576   GST_DEBUG ("%" G_GINT64_FORMAT, segment.start);
577   fail_unless (segment.start == 0);
578   fail_unless (segment.stop == 200);
579   fail_unless (segment.duration == 200);
580 }
581
582 GST_END_TEST;
583
584 #if 0
585 /* mess with the segment structure in the bytes format */
586 GST_START_TEST (segment_newsegment_open)
587 {
588   GstSegment segment;
589
590   gst_segment_init (&segment, GST_FORMAT_BYTES);
591
592   /* time should also work for starting from 0 */
593   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0, -1,
594       0);
595
596   fail_unless (segment.rate == 1.0);
597   fail_unless (segment.format == GST_FORMAT_BYTES);
598   fail_unless (segment.flags == 0);
599   fail_unless (segment.start == 0);
600   fail_unless (segment.stop == -1);
601   fail_unless (segment.time == 0);
602   fail_unless (segment.base == 0);
603   fail_unless (segment.position == 0);
604   fail_unless (segment.duration == -1);
605
606   /* we set stop but in the wrong format, stop stays open. */
607   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_TIME, 0,
608       200, 0);
609
610   fail_unless (segment.start == 0);
611   fail_unless (segment.stop == -1);
612   fail_unless (segment.time == 0);
613   fail_unless (segment.base == 0);
614   fail_unless (segment.position == 0);
615
616   /* update, nothing changes */
617   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 0, -1,
618       0);
619
620   fail_unless (segment.start == 0);
621   fail_unless (segment.stop == -1);
622   fail_unless (segment.time == 0);
623   fail_unless (segment.base == 0);
624   fail_unless (segment.position == 0);
625
626   /* update */
627   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
628       GST_FORMAT_BYTES, 100, -1, 100);
629
630   fail_unless (segment.start == 100);
631   fail_unless (segment.stop == -1);
632   fail_unless (segment.time == 100);
633   fail_unless (segment.base == 100);
634   fail_unless (segment.position == 100);
635
636   /* last_stop 0, base does not change */
637   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
638       -1, 0);
639
640   fail_unless (segment.start == 0);
641   fail_unless (segment.stop == -1);
642   fail_unless (segment.time == 0);
643   fail_unless (segment.base == 100);
644
645   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 200);
646
647   fail_unless (segment.position == 200);
648
649   /* last_stop 200, base changes */
650   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0, GST_FORMAT_BYTES, 0,
651       -1, 0);
652
653   fail_unless (segment.start == 0);
654   fail_unless (segment.stop == -1);
655   fail_unless (segment.time == 0);
656   fail_unless (segment.base == 300);
657   fail_unless (segment.position == 0);
658 }
659
660 GST_END_TEST;
661
662
663 /* mess with the segment structure in the bytes format */
664 GST_START_TEST (segment_newsegment_closed)
665 {
666   GstSegment segment;
667
668   gst_segment_init (&segment, GST_FORMAT_BYTES);
669
670   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
671       GST_FORMAT_BYTES, 0, 200, 0);
672
673   fail_unless (segment.rate == 1.0);
674   fail_unless (segment.format == GST_FORMAT_BYTES);
675   fail_unless (segment.flags == 0);
676   fail_unless (segment.start == 0);
677   fail_unless (segment.stop == 200);
678   fail_unless (segment.time == 0);
679   fail_unless (segment.base == 0);
680   fail_unless (segment.position == 0);
681   fail_unless (segment.duration == -1);
682
683   /* assume we advanced to position 40 */
684   gst_segment_set_last_stop (&segment, GST_FORMAT_BYTES, 40);
685   fail_unless (segment.position == 40);
686
687   /* do an update to the start, last_stop is unchanged because it's bigger */
688   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 20,
689       200, 20);
690
691   fail_unless (segment.start == 20);
692   fail_unless (segment.stop == 200);
693   fail_unless (segment.time == 20);
694   fail_unless (segment.base == 20);
695   fail_unless (segment.position == 40);
696
697   /* do an update past our last_stop, it should be updated now */
698   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0, GST_FORMAT_BYTES, 50,
699       300, 50);
700
701   fail_unless (segment.start == 50);
702   fail_unless (segment.stop == 300);
703   fail_unless (segment.time == 50);
704   fail_unless (segment.base == 50);
705   fail_unless (segment.position == 50);
706
707   /* and a new accumulated one */
708   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
709       GST_FORMAT_BYTES, 100, 400, 300);
710
711   fail_unless (segment.start == 100);
712   fail_unless (segment.stop == 400);
713   fail_unless (segment.time == 300);
714   fail_unless (segment.base == 300);
715
716   /* and a new updated one */
717   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
718       GST_FORMAT_BYTES, 100, 500, 300);
719
720   fail_unless (segment.start == 100);
721   fail_unless (segment.stop == 500);
722   fail_unless (segment.time == 300);
723   fail_unless (segment.base == 300);
724
725   /* and a new partially updated one */
726   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
727       GST_FORMAT_BYTES, 200, 500, 400);
728
729   fail_unless (segment.start == 200);
730   fail_unless (segment.stop == 500);
731   fail_unless (segment.time == 400);
732   fail_unless (segment.base == 400);
733 }
734
735 GST_END_TEST;
736
737 /* mess with the segment structure in the time format */
738 GST_START_TEST (segment_newsegment_streamtime)
739 {
740   GstSegment segment;
741   guint64 result;
742
743   gst_segment_init (&segment, GST_FORMAT_TIME);
744
745   /***************************
746    * Normal segment
747    ***************************/
748   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
749       GST_FORMAT_TIME, 0, 200, 0);
750
751   fail_unless (segment.rate == 1.0);
752   fail_unless (segment.applied_rate == 1.0);
753   fail_unless (segment.format == GST_FORMAT_TIME);
754   fail_unless (segment.flags == 0);
755   fail_unless (segment.start == 0);
756   fail_unless (segment.stop == 200);
757   fail_unless (segment.time == 0);
758   fail_unless (segment.base == 0);
759   fail_unless (segment.position == 0);
760   fail_unless (segment.duration == -1);
761
762   /* invalid time gives invalid result */
763   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
764   fail_unless (result == -1);
765
766   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
767   fail_unless (result == 0);
768
769   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
770   fail_unless (result == 100);
771
772   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
773   fail_unless (result == 200);
774
775   /* outside of the segment */
776   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
777   fail_unless (result == -1);
778
779   /*********************
780    * time shifted by 500
781    *********************/
782   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
783       GST_FORMAT_TIME, 0, 200, 500);
784
785   fail_unless (segment.base == 200);
786
787   /* invalid time gives invalid result */
788   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
789   fail_unless (result == -1);
790
791   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
792   fail_unless (result == 500);
793
794   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
795   fail_unless (result == 600);
796
797   /* outside of the segment */
798   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
799   fail_unless (result == -1);
800
801   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
802   fail_unless (result == -1);
803
804   /*********************
805    * time offset by 500
806    *********************/
807   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
808       GST_FORMAT_TIME, 500, 700, 0);
809
810   fail_unless (segment.base == 400);
811
812   /* invalid time gives invalid result */
813   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
814   fail_unless (result == -1);
815
816   /* before segment is invalid */
817   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
818   fail_unless (result == -1);
819
820   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
821   fail_unless (result == 0);
822
823   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
824   fail_unless (result == 100);
825
826   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
827   fail_unless (result == 200);
828
829   /* outside of the segment */
830   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
831   fail_unless (result == -1);
832
833   /*************************************
834    * time offset by 500, shifted by 200
835    *************************************/
836   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
837       GST_FORMAT_TIME, 500, 700, 200);
838
839   fail_unless (segment.base == 600);
840
841   /* invalid time gives invalid result */
842   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
843   fail_unless (result == -1);
844
845   /* before segment is invalid */
846   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
847   fail_unless (result == -1);
848
849   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 500);
850   fail_unless (result == 200);
851
852   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 600);
853   fail_unless (result == 300);
854
855   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 700);
856   fail_unless (result == 400);
857
858   /* outside of the segment */
859   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 800);
860   fail_unless (result == -1);
861 }
862
863 GST_END_TEST;
864
865 /* mess with the segment structure in the time format */
866 GST_START_TEST (segment_newsegment_streamtime_rate)
867 {
868   GstSegment segment;
869   guint64 result;
870
871   gst_segment_init (&segment, GST_FORMAT_TIME);
872
873   /***************************
874    * Normal segment rate 2.0
875    ***************************/
876   gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
877       GST_FORMAT_TIME, 0, 200, 0);
878
879   fail_unless (segment.rate == 2.0);
880   fail_unless (segment.applied_rate == 1.0);
881   fail_unless (segment.format == GST_FORMAT_TIME);
882   fail_unless (segment.flags == 0);
883   fail_unless (segment.start == 0);
884   fail_unless (segment.stop == 200);
885   fail_unless (segment.time == 0);
886   fail_unless (segment.base == 0);
887   fail_unless (segment.position == 0);
888   fail_unless (segment.duration == -1);
889
890   /* invalid time gives invalid result */
891   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
892   fail_unless (result == -1);
893
894   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
895   fail_unless (result == 0);
896
897   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
898   fail_unless (result == 100);
899
900   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
901   fail_unless (result == 150);
902
903   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
904   fail_unless (result == 200);
905
906   /* outside of the segment */
907   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
908   fail_unless (result == -1);
909
910   /***************************************
911    * Normal segment rate 2.0, offset
912    ***************************************/
913   gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
914       GST_FORMAT_TIME, 100, 300, 0);
915
916   fail_unless (segment.base == 100);
917
918   /* invalid time gives invalid result */
919   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
920   fail_unless (result == -1);
921
922   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
923   fail_unless (result == 0);
924
925   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
926   fail_unless (result == 100);
927
928   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 250);
929   fail_unless (result == 150);
930
931   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
932   fail_unless (result == 200);
933
934   /* outside of the segment */
935   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
936   fail_unless (result == -1);
937
938   /***************************************
939    * Normal segment rate -1.0, offset
940    ***************************************/
941
942   /* buffers will arrive from 300 to 100 in a sink, stream time
943    * calculation is unaffected by the rate */
944   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
945       GST_FORMAT_TIME, 100, 300, 0);
946
947   fail_unless (segment.base == 200);
948
949   /* invalid time gives invalid result */
950   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
951   fail_unless (result == -1);
952
953   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
954   fail_unless (result == 0);
955
956   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
957   fail_unless (result == 100);
958
959   /***********************************************
960    * Normal segment rate -1.0, offset, time = 200
961    ***********************************************/
962   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
963       GST_FORMAT_TIME, 100, 300, 200);
964
965   /* invalid time gives invalid result */
966   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
967   fail_unless (result == -1);
968
969   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
970   fail_unless (result == 200);
971
972   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
973   fail_unless (result == 300);
974
975   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
976   fail_unless (result == 400);
977
978   /* outside of the segment */
979   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 400);
980   fail_unless (result == -1);
981 }
982
983 GST_END_TEST;
984
985 /* mess with the segment structure in the time format */
986 GST_START_TEST (segment_newsegment_streamtime_applied_rate)
987 {
988   GstSegment segment;
989   guint64 result;
990
991   gst_segment_init (&segment, GST_FORMAT_TIME);
992
993   /***********************************************************
994    * Normal segment rate 1.0, applied rate -1.0
995    * This means the timestamps represents a stream going backwards
996    * starting from @time to 0.
997    ************************************************************/
998   gst_segment_set_newsegment (&segment, FALSE, 1.0, -1.0,
999       GST_FORMAT_TIME, 0, 200, 200);
1000
1001   fail_unless (segment.rate == 1.0);
1002   fail_unless (segment.applied_rate == -1.0);
1003   fail_unless (segment.format == GST_FORMAT_TIME);
1004   fail_unless (segment.flags == 0);
1005   fail_unless (segment.start == 0);
1006   fail_unless (segment.stop == 200);
1007   fail_unless (segment.time == 200);
1008   fail_unless (segment.base == 0);
1009   fail_unless (segment.position == 0);
1010   fail_unless (segment.duration == -1);
1011
1012   /* invalid time gives invalid result */
1013   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1014   fail_unless (result == -1);
1015
1016   /* we count backwards from 200 */
1017   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1018   fail_unless (result == 200);
1019
1020   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1021   fail_unless (result == 100);
1022
1023   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1024   fail_unless (result == 50);
1025
1026   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1027   fail_unless (result == 0);
1028
1029   /* outside of the segment */
1030   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1031   fail_unless (result == -1);
1032
1033   /***********************************************************
1034    * Normal segment rate 1.0, applied rate 2.0
1035    * This means the timestamps represents a stream at twice the
1036    * normal rate
1037    ************************************************************/
1038   gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1039       GST_FORMAT_TIME, 0, 200, 0);
1040
1041   fail_unless (segment.rate == 1.0);
1042   fail_unless (segment.applied_rate == 2.0);
1043   fail_unless (segment.format == GST_FORMAT_TIME);
1044   fail_unless (segment.flags == 0);
1045   fail_unless (segment.start == 0);
1046   fail_unless (segment.stop == 200);
1047   fail_unless (segment.time == 0);
1048   fail_unless (segment.base == 200);
1049   fail_unless (segment.position == 0);
1050   fail_unless (segment.duration == -1);
1051
1052   /* invalid time gives invalid result */
1053   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1054   fail_unless (result == -1);
1055
1056   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1057   fail_unless (result == 0);
1058
1059   /* the stream prepresents a stream going twice as fast, the position 
1060    * in the segment is therefore scaled by the applied rate */
1061   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1062   fail_unless (result == 200);
1063
1064   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1065   fail_unless (result == 300);
1066
1067   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1068   fail_unless (result == 400);
1069
1070   /* outside of the segment */
1071   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1072   fail_unless (result == -1);
1073
1074   /***********************************************************
1075    * Normal segment rate 1.0, applied rate -2.0
1076    * This means the timestamps represents a stream at twice the
1077    * reverse rate
1078    ************************************************************/
1079   gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1080       GST_FORMAT_TIME, 0, 200, 400);
1081
1082   fail_unless (segment.rate == 1.0);
1083   fail_unless (segment.applied_rate == -2.0);
1084   fail_unless (segment.format == GST_FORMAT_TIME);
1085   fail_unless (segment.flags == 0);
1086   fail_unless (segment.start == 0);
1087   fail_unless (segment.stop == 200);
1088   fail_unless (segment.time == 400);
1089   /* previous segment lasted 200, rate of 2.0 was already applied */
1090   fail_unless (segment.base == 400);
1091   fail_unless (segment.position == 0);
1092   fail_unless (segment.duration == -1);
1093
1094   /* invalid time gives invalid result */
1095   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1096   fail_unless (result == -1);
1097
1098   /* we count backwards from 400 */
1099   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1100   fail_unless (result == 400);
1101
1102   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1103   fail_unless (result == 200);
1104
1105   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1106   fail_unless (result == 100);
1107
1108   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1109   fail_unless (result == 0);
1110
1111   /* outside of the segment */
1112   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1113   fail_unless (result == -1);
1114
1115   /***********************************************************
1116    * Normal segment rate 1.0, applied rate -2.0
1117    * This means the timestamps represents a stream at twice the
1118    * reverse rate, start time cannot compensate the complete
1119    * duration of the segment so we stop at 0
1120    ************************************************************/
1121   gst_segment_set_newsegment (&segment, FALSE, 1.0, -2.0,
1122       GST_FORMAT_TIME, 0, 200, 200);
1123
1124   fail_unless (segment.rate == 1.0);
1125   fail_unless (segment.applied_rate == -2.0);
1126   fail_unless (segment.format == GST_FORMAT_TIME);
1127   fail_unless (segment.flags == 0);
1128   fail_unless (segment.start == 0);
1129   fail_unless (segment.stop == 200);
1130   fail_unless (segment.time == 200);
1131   fail_unless (segment.base == 600);
1132   fail_unless (segment.position == 0);
1133   fail_unless (segment.duration == -1);
1134
1135   /* invalid time gives invalid result */
1136   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1137   fail_unless (result == -1);
1138
1139   /* we count backwards from 200 */
1140   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1141   fail_unless (result == 200);
1142
1143   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1144   fail_unless (result == 0);
1145
1146   /* clamp at 0 */
1147   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1148   fail_unless (result == 0);
1149
1150   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1151   fail_unless (result == 0);
1152
1153   /* outside of the segment */
1154   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1155   fail_unless (result == -1);
1156 }
1157
1158 GST_END_TEST;
1159
1160 /* mess with the segment structure in the time format */
1161 GST_START_TEST (segment_newsegment_streamtime_applied_rate_rate)
1162 {
1163   GstSegment segment;
1164   guint64 result;
1165
1166   gst_segment_init (&segment, GST_FORMAT_TIME);
1167
1168   /***********************************************************
1169    * Segment rate 2.0, applied rate 2.0
1170    * this means we have a double speed stream that we should
1171    * speed up by a factor of 2.0 some more. the resulting
1172    * stream will be played at four times the speed. 
1173    ************************************************************/
1174   gst_segment_set_newsegment (&segment, FALSE, 2.0, 2.0,
1175       GST_FORMAT_TIME, 0, 200, 0);
1176
1177   fail_unless (segment.rate == 2.0);
1178   fail_unless (segment.applied_rate == 2.0);
1179   fail_unless (segment.format == GST_FORMAT_TIME);
1180   fail_unless (segment.flags == 0);
1181   fail_unless (segment.start == 0);
1182   fail_unless (segment.stop == 200);
1183   fail_unless (segment.time == 0);
1184   fail_unless (segment.base == 0);
1185   fail_unless (segment.position == 0);
1186   fail_unless (segment.duration == -1);
1187
1188   /* invalid time gives invalid result */
1189   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1190   fail_unless (result == -1);
1191
1192   /* only applied rate affects our calculation of the stream time */
1193   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1194   fail_unless (result == 0);
1195
1196   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1197   fail_unless (result == 200);
1198
1199   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1200   fail_unless (result == 300);
1201
1202   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1203   fail_unless (result == 400);
1204
1205   /* outside of the segment */
1206   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1207   fail_unless (result == -1);
1208
1209   /***********************************************************
1210    * Segment rate 2.0, applied rate -1.0
1211    * this means we have a reverse stream that we should
1212    * speed up by a factor of 2.0
1213    ************************************************************/
1214   gst_segment_set_newsegment (&segment, FALSE, 2.0, -1.0,
1215       GST_FORMAT_TIME, 0, 200, 200);
1216
1217   fail_unless (segment.rate == 2.0);
1218   fail_unless (segment.applied_rate == -1.0);
1219   fail_unless (segment.format == GST_FORMAT_TIME);
1220   fail_unless (segment.flags == 0);
1221   fail_unless (segment.start == 0);
1222   fail_unless (segment.stop == 200);
1223   fail_unless (segment.time == 200);
1224   /* previous segment lasted 100 */
1225   fail_unless (segment.base == 100);
1226   fail_unless (segment.position == 0);
1227   fail_unless (segment.duration == -1);
1228
1229   /* invalid time gives invalid result */
1230   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1231   fail_unless (result == -1);
1232
1233   /* only applied rate affects our calculation of the stream time */
1234   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1235   fail_unless (result == 200);
1236
1237   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1238   fail_unless (result == 100);
1239
1240   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1241   fail_unless (result == 50);
1242
1243   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1244   fail_unless (result == 0);
1245
1246   /* outside of the segment */
1247   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1248   fail_unless (result == -1);
1249
1250   /***********************************************************
1251    * Segment rate -1.0, applied rate -1.0
1252    * this means we have a reverse stream that we should
1253    * reverse to get the normal stream again.
1254    ************************************************************/
1255   gst_segment_set_newsegment (&segment, FALSE, -1.0, -1.0,
1256       GST_FORMAT_TIME, 0, 200, 200);
1257
1258   fail_unless (segment.rate == -1.0);
1259   fail_unless (segment.applied_rate == -1.0);
1260   fail_unless (segment.format == GST_FORMAT_TIME);
1261   fail_unless (segment.flags == 0);
1262   fail_unless (segment.start == 0);
1263   fail_unless (segment.stop == 200);
1264   fail_unless (segment.time == 200);
1265   /* accumulated 100 of previous segment to make 200 */
1266   fail_unless (segment.base == 200);
1267   fail_unless (segment.position == 200);
1268   fail_unless (segment.duration == -1);
1269
1270   /* invalid time gives invalid result */
1271   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1272   fail_unless (result == -1);
1273
1274   /* only applied rate affects our calculation of the stream time */
1275   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1276   fail_unless (result == 200);
1277
1278   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1279   fail_unless (result == 100);
1280
1281   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1282   fail_unless (result == 50);
1283
1284   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1285   fail_unless (result == 0);
1286
1287   /* outside of the segment */
1288   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1289   fail_unless (result == -1);
1290
1291   /***********************************************************
1292    * Segment rate -1.0, applied rate -1.0
1293    * this means we have a reverse stream that we should
1294    * reverse to get the normal stream again.
1295    ************************************************************/
1296   gst_segment_set_newsegment (&segment, FALSE, -1.0, 2.0,
1297       GST_FORMAT_TIME, 0, 200, 0);
1298
1299   fail_unless (segment.rate == -1.0);
1300   fail_unless (segment.applied_rate == 2.0);
1301   fail_unless (segment.format == GST_FORMAT_TIME);
1302   fail_unless (segment.flags == 0);
1303   fail_unless (segment.start == 0);
1304   fail_unless (segment.stop == 200);
1305   fail_unless (segment.time == 0);
1306   fail_unless (segment.base == 400);
1307   fail_unless (segment.position == 200);
1308   fail_unless (segment.duration == -1);
1309
1310   /* invalid time gives invalid result */
1311   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, -1);
1312   fail_unless (result == -1);
1313
1314   /* only applied rate affects our calculation of the stream time */
1315   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 0);
1316   fail_unless (result == 0);
1317
1318   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 100);
1319   fail_unless (result == 200);
1320
1321   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 150);
1322   fail_unless (result == 300);
1323
1324   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 200);
1325   fail_unless (result == 400);
1326
1327   /* outside of the segment */
1328   result = gst_segment_to_stream_time (&segment, GST_FORMAT_TIME, 300);
1329   fail_unless (result == -1);
1330 }
1331
1332 GST_END_TEST;
1333
1334 /* mess with the segment structure in the time format */
1335 GST_START_TEST (segment_newsegment_runningtime)
1336 {
1337   GstSegment segment;
1338   guint64 result;
1339
1340   gst_segment_init (&segment, GST_FORMAT_TIME);
1341
1342   /***************************
1343    * Normal segment
1344    ***************************/
1345   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1346       GST_FORMAT_TIME, 0, 200, 0);
1347
1348   fail_unless (segment.rate == 1.0);
1349   fail_unless (segment.applied_rate == 1.0);
1350   fail_unless (segment.format == GST_FORMAT_TIME);
1351   fail_unless (segment.flags == 0);
1352   fail_unless (segment.start == 0);
1353   fail_unless (segment.stop == 200);
1354   fail_unless (segment.time == 0);
1355   fail_unless (segment.base == 0);
1356   fail_unless (segment.position == 0);
1357   fail_unless (segment.duration == -1);
1358
1359   /* invalid time gives invalid result */
1360   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1361   fail_unless (result == -1);
1362
1363   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1364   fail_unless (result == 0);
1365   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1366   fail_unless (result == 0);
1367
1368   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1369   fail_unless (result == 100);
1370   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1371   fail_unless (result == 100);
1372
1373   /* at edge is exactly the segment duration */
1374   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1375   fail_unless (result == 200);
1376   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1377   fail_unless (result == 200);
1378
1379   /* outside of the segment */
1380   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 300);
1381   fail_unless (result == -1);
1382   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 300);
1383   fail_unless (result == -1);
1384
1385   /***********************************************************
1386    * time shifted by 500, check if accumulation worked.
1387    * Rate convert to twice the speed which means scaling down
1388    * all positions by 2.0 in this segment.
1389    * Then time argument is not used at all here.
1390    ***********************************************************/
1391   gst_segment_set_newsegment (&segment, FALSE, 2.0, 1.0,
1392       GST_FORMAT_TIME, 0, 200, 500);
1393
1394   /* normal speed gives elapsed of 200 */
1395   fail_unless (segment.base == 200);
1396
1397   /* invalid time gives invalid result */
1398   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1399   fail_unless (result == -1);
1400
1401   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 0);
1402   fail_unless (result == 200);
1403   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1404   fail_unless (result == 0);
1405
1406   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1407   fail_unless (result == 250);
1408   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1409   fail_unless (result == 100);
1410
1411   /* outside of the segment */
1412   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1413   fail_unless (result == -1);
1414   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 310);
1415   fail_unless (result == -1);
1416
1417   /********************************************
1418    * time offset by 500
1419    * applied rate is not used for running time
1420    ********************************************/
1421   gst_segment_set_newsegment (&segment, FALSE, 1.0, 2.0,
1422       GST_FORMAT_TIME, 500, 700, 0);
1423
1424   /* previous segment played at double speed gives elapsed time of
1425    * 100 added to previous accum of 200 gives 300. */
1426   fail_unless (segment.base == 300);
1427
1428   /* invalid time gives invalid result */
1429   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1430   fail_unless (result == -1);
1431
1432   /* before segment is invalid */
1433   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1434   fail_unless (result == -1);
1435   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 200);
1436   fail_unless (result == -1);
1437
1438   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1439   fail_unless (result == 300);
1440   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1441   fail_unless (result == 500);
1442
1443   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1444   fail_unless (result == 400);
1445   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1446   fail_unless (result == 600);
1447
1448   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1449   fail_unless (result == 500);
1450   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1451   fail_unless (result == 700);
1452
1453   /* outside of the segment */
1454   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1455   fail_unless (result == -1);
1456   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1457   fail_unless (result == -1);
1458
1459   /**********************************************************
1460    * time offset by 500, shifted by 200
1461    * Negative rate makes the running time go backwards 
1462    * relative to the segment stop position. again time
1463    * is ignored.
1464    **********************************************************/
1465   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1466       GST_FORMAT_TIME, 500, 700, 200);
1467
1468   fail_unless (segment.base == 500);
1469
1470   /* invalid time gives invalid result */
1471   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1472   fail_unless (result == -1);
1473
1474   /* before segment is invalid */
1475   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1476   fail_unless (result == -1);
1477   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 400);
1478   fail_unless (result == -1);
1479
1480   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1481   fail_unless (result == 700);
1482   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1483   fail_unless (result == 500);
1484
1485   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1486   fail_unless (result == 600);
1487   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1488   fail_unless (result == 600);
1489
1490   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1491   fail_unless (result == 500);
1492   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1493   fail_unless (result == 700);
1494
1495   /* outside of the segment */
1496   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1497   fail_unless (result == -1);
1498   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1499   fail_unless (result == -1);
1500
1501   /**********************************************************
1502    * time offset by 500, shifted by 200
1503    * Negative rate makes the running time go backwards at
1504    * twice speed relative to the segment stop position. again 
1505    * time is ignored.
1506    **********************************************************/
1507   gst_segment_set_newsegment (&segment, FALSE, -2.0, -2.0,
1508       GST_FORMAT_TIME, 500, 700, 200);
1509
1510   fail_unless (segment.base == 700);
1511
1512   /* invalid time gives invalid result */
1513   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1514   fail_unless (result == -1);
1515
1516   /* before segment is invalid */
1517   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 400);
1518   fail_unless (result == -1);
1519   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 600);
1520   fail_unless (result == -1);
1521
1522   /* total scaled segment time is 100, accum is 700, so we get 800 */
1523   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 500);
1524   fail_unless (result == 800);
1525   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1526   fail_unless (result == 500);
1527
1528   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 600);
1529   fail_unless (result == 750);
1530   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1531   fail_unless (result == 600);
1532
1533   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 700);
1534   fail_unless (result == 700);
1535   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1536   fail_unless (result == 700);
1537
1538   /* outside of the segment */
1539   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 800);
1540   fail_unless (result == -1);
1541   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 900);
1542   fail_unless (result == -1);
1543
1544   /* see if negative rate closed segment correctly */
1545   gst_segment_set_newsegment (&segment, FALSE, -2.0, -1.0,
1546       GST_FORMAT_TIME, 500, 700, 200);
1547
1548   /* previous segment lasted 100, and was at 700 so we should get 800 */
1549   fail_unless (segment.base == 800);
1550   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, 800);
1551   fail_unless (result == 700);
1552 }
1553
1554 GST_END_TEST;
1555
1556 /* mess with the segment structure in the time format */
1557 GST_START_TEST (segment_newsegment_accum)
1558 {
1559   GstSegment segment;
1560   guint64 result;
1561
1562   gst_segment_init (&segment, GST_FORMAT_TIME);
1563
1564   /***************************
1565    * Normal reverse segment
1566    ***************************/
1567   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1568       GST_FORMAT_TIME, 0, 200, 0);
1569
1570   fail_unless (segment.rate == -1.0);
1571   fail_unless (segment.applied_rate == 1.0);
1572   fail_unless (segment.format == GST_FORMAT_TIME);
1573   fail_unless (segment.flags == 0);
1574   fail_unless (segment.start == 0);
1575   fail_unless (segment.stop == 200);
1576   fail_unless (segment.time == 0);
1577   fail_unless (segment.base == 0);
1578   fail_unless (segment.position == 200);
1579   fail_unless (segment.duration == -1);
1580
1581   /* invalid time gives invalid result */
1582   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1583   fail_unless (result == -1);
1584
1585   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1586   fail_unless (result == 0);
1587   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1588   fail_unless (result == 200);
1589
1590   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1591   fail_unless (result == 50);
1592   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1593   fail_unless (result == 150);
1594
1595   /* update segment, this accumulates 50 from the previous segment. */
1596   gst_segment_set_newsegment (&segment, TRUE, -2.0, 1.0,
1597       GST_FORMAT_TIME, 0, 150, 0);
1598
1599   fail_unless (segment.rate == -2.0);
1600   fail_unless (segment.applied_rate == 1.0);
1601   fail_unless (segment.format == GST_FORMAT_TIME);
1602   fail_unless (segment.flags == 0);
1603   fail_unless (segment.start == 0);
1604   fail_unless (segment.stop == 150);
1605   fail_unless (segment.time == 0);
1606   fail_unless (segment.base == 50);
1607   fail_unless (segment.position == 150);
1608   fail_unless (segment.duration == -1);
1609
1610   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1611   fail_unless (result == 50);
1612   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1613   fail_unless (result == 150);
1614
1615   /* 50 accumulated + 50 / 2 */
1616   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1617   fail_unless (result == 75);
1618   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1619   fail_unless (result == 100);
1620
1621   /* update segment, this does not accumulate anything. */
1622   gst_segment_set_newsegment (&segment, TRUE, 1.0, 1.0,
1623       GST_FORMAT_TIME, 100, 200, 100);
1624
1625   fail_unless (segment.rate == 1.0);
1626   fail_unless (segment.applied_rate == 1.0);
1627   fail_unless (segment.format == GST_FORMAT_TIME);
1628   fail_unless (segment.flags == 0);
1629   fail_unless (segment.start == 100);
1630   fail_unless (segment.stop == 200);
1631   fail_unless (segment.time == 100);
1632   fail_unless (segment.base == 50);
1633   fail_unless (segment.position == 150);
1634   fail_unless (segment.duration == -1);
1635
1636   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 100);
1637   fail_unless (result == 50);
1638   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1639   fail_unless (result == 100);
1640
1641   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1642   fail_unless (result == 100);
1643   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1644   fail_unless (result == 150);
1645 }
1646
1647 GST_END_TEST;
1648
1649 /* mess with the segment structure in the time format */
1650 GST_START_TEST (segment_newsegment_accum2)
1651 {
1652   GstSegment segment;
1653   guint64 result;
1654
1655   gst_segment_init (&segment, GST_FORMAT_TIME);
1656
1657   /***************************
1658    * Normal reverse segment
1659    ***************************/
1660   gst_segment_set_newsegment (&segment, FALSE, -1.0, 1.0,
1661       GST_FORMAT_TIME, 0, 200, 0);
1662
1663   fail_unless (segment.rate == -1.0);
1664   fail_unless (segment.applied_rate == 1.0);
1665   fail_unless (segment.format == GST_FORMAT_TIME);
1666   fail_unless (segment.flags == 0);
1667   fail_unless (segment.start == 0);
1668   fail_unless (segment.stop == 200);
1669   fail_unless (segment.time == 0);
1670   fail_unless (segment.base == 0);
1671   fail_unless (segment.position == 200);
1672   fail_unless (segment.duration == -1);
1673
1674   /* invalid time gives invalid result */
1675   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1676   fail_unless (result == -1);
1677   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1678   fail_unless (result == -1);
1679
1680   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1681   fail_unless (result == 0);
1682   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1683   fail_unless (result == 200);
1684
1685   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1686   fail_unless (result == 50);
1687   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1688   fail_unless (result == 150);
1689
1690   /* close segment, this accumulates nothing. */
1691   gst_segment_set_newsegment (&segment, TRUE, -1.0, 1.0,
1692       GST_FORMAT_TIME, 150, 200, 0);
1693
1694   fail_unless (segment.rate == -1.0);
1695   fail_unless (segment.applied_rate == 1.0);
1696   fail_unless (segment.format == GST_FORMAT_TIME);
1697   fail_unless (segment.flags == 0);
1698   fail_unless (segment.start == 150);
1699   fail_unless (segment.stop == 200);
1700   fail_unless (segment.time == 0);
1701   fail_unless (segment.base == 0);
1702   fail_unless (segment.position == 200);
1703   fail_unless (segment.duration == -1);
1704
1705   /* new segment, this accumulates 50. */
1706   gst_segment_set_newsegment (&segment, FALSE, 1.0, 1.0,
1707       GST_FORMAT_TIME, 150, 300, 150);
1708
1709   fail_unless (segment.rate == 1.0);
1710   fail_unless (segment.applied_rate == 1.0);
1711   fail_unless (segment.format == GST_FORMAT_TIME);
1712   fail_unless (segment.flags == 0);
1713   fail_unless (segment.start == 150);
1714   fail_unless (segment.stop == 300);
1715   fail_unless (segment.time == 150);
1716   fail_unless (segment.base == 50);
1717   fail_unless (segment.position == 150);
1718   fail_unless (segment.duration == -1);
1719
1720   /* invalid time gives invalid result */
1721   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, -1);
1722   fail_unless (result == -1);
1723
1724   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 150);
1725   fail_unless (result == 50);
1726   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1727   fail_unless (result == 150);
1728
1729   result = gst_segment_to_running_time (&segment, GST_FORMAT_TIME, 200);
1730   fail_unless (result == 100);
1731   result = gst_segment_to_position (&segment, GST_FORMAT_TIME, result);
1732   fail_unless (result == 200);
1733 }
1734
1735 GST_END_TEST;
1736 #endif
1737
1738 GST_START_TEST (segment_copy)
1739 {
1740   GstSegment *copy;
1741   GstSegment segment = { 0.0, };
1742
1743   /* this is a boxed type copy function, we support copying NULL */
1744   fail_unless (gst_segment_copy (NULL) == NULL);
1745
1746   gst_segment_init (&segment, GST_FORMAT_TIME);
1747
1748   segment.rate = -1.0;
1749   segment.applied_rate = 1.0;
1750   segment.start = 0;
1751   segment.stop = 200;
1752   segment.time = 0;
1753
1754   copy = gst_segment_copy (&segment);
1755   fail_unless (copy != NULL);
1756   /* we inited the struct on the stack to zeroes, so direct comparison should
1757    * be ok here despite the padding field and regardless of implementation */
1758   fail_unless (memcmp (copy, &segment, sizeof (GstSegment)) == 0);
1759   gst_segment_free (copy);
1760 }
1761
1762 GST_END_TEST;
1763
1764 static Suite *
1765 gst_segment_suite (void)
1766 {
1767   Suite *s = suite_create ("GstSegment");
1768   TCase *tc_chain = tcase_create ("segments");
1769
1770   tcase_set_timeout (tc_chain, 20);
1771
1772   suite_add_tcase (s, tc_chain);
1773   tcase_add_test (tc_chain, segment_seek_nosize);
1774   tcase_add_test (tc_chain, segment_seek_size);
1775   tcase_add_test (tc_chain, segment_seek_reverse);
1776   tcase_add_test (tc_chain, segment_seek_rate);
1777 #if 0
1778   tcase_add_test (tc_chain, segment_newsegment_open);
1779   tcase_add_test (tc_chain, segment_newsegment_closed);
1780   tcase_add_test (tc_chain, segment_newsegment_streamtime);
1781   tcase_add_test (tc_chain, segment_newsegment_streamtime_rate);
1782   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate);
1783   tcase_add_test (tc_chain, segment_newsegment_streamtime_applied_rate_rate);
1784   tcase_add_test (tc_chain, segment_newsegment_runningtime);
1785   tcase_add_test (tc_chain, segment_newsegment_accum);
1786   tcase_add_test (tc_chain, segment_newsegment_accum2);
1787 #endif
1788   tcase_add_test (tc_chain, segment_copy);
1789
1790   return s;
1791 }
1792
1793 GST_CHECK_MAIN (gst_segment);