docs/design/part-synchronisation.txt: Small updates.
[platform/upstream/gstreamer.git] / gst / gstsegment.c
1 /* GStreamer
2  * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3  *
4  * gstsegment.c: GstSegment subsystem
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #include "gst_private.h"
24
25 #include "gstutils.h"
26 #include "gstsegment.h"
27
28 /**
29  * SECTION:gstsegment
30  * @short_description: Structure describing the configured region of interest
31  *                     in a media file.
32  * @see_also: #GstEvent
33  *
34  * This helper structure holds the relevant values for tracking the region of
35  * interest in a media file, called a segment. 
36  *
37  * The structure can be used for two purposes:
38  * <itemizedlist>
39  *   <listitem><para>performing seeks (handling seek events)</para></listitem>
40  *   <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
41  * </itemizedlist>
42  *
43  * The segment is usually configured by the application with a seek event which 
44  * is propagated upstream and eventually handled by an element that performs the seek.
45  *
46  * The configured segment is then propagated back downstream with a newsegment event.
47  * This information is then used to clip media to the segment boundaries.
48  *
49  * A segment structure is initialized with gst_segment_init(), which takes a #GstFormat
50  * that will be used as the format of the segment values. The segment will be configured
51  * with a start value of 0 and a stop/duration of -1, which is undefined. The default
52  * rate and applied_rate is 1.0.
53  *
54  * If the segment is used for managing seeks, the segment duration should be set with
55  * gst_segment_set_duration(). The public duration field contains the duration of the
56  * segment. When using the segment for seeking, the start and time members should 
57  * normally be left to their default 0 value. The stop position is left to -1 unless
58  * explicitly configured to a different value after a seek event.
59  *
60  * The current position in the segment should be set with the gst_segment_set_last_stop().
61  * The public last_stop field contains the last set stop position in the segment.
62  *
63  * For elements that perform seeks, the current segment should be updated with the
64  * gst_segment_set_seek() and the values from the seek event. This method will update
65  * all the segment fields. The last_stop field will contain the new playback position.
66  * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
67  * the last_stop position, possibly with updated flags or rate.
68  *
69  * For elements that want to use #GstSegment to track the playback region, use
70  * gst_segment_set_newsegment() to update the segment fields with the information from
71  * the newsegment event. The gst_segment_clip() method can be used to check and clip
72  * the media data to the segment boundaries.
73  *
74  * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
75  * can be used to convert a timestamp to a value that can be used to synchronize
76  * to the clock. This function takes into account all accumulated segments as well as
77  * any rate or applied_rate conversions.
78  *
79  * For elements that need to perform operations on media data in stream_time, 
80  * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
81  * info to stream time (which is always between 0 and the duration of the stream).
82  *
83  * Last reviewed on 2007-05-17 (0.10.13)
84  */
85
86 static GstSegment *
87 gst_segment_copy (GstSegment * segment)
88 {
89   GstSegment *result = NULL;
90
91   if (segment) {
92     result = gst_segment_new ();
93     memcpy (result, segment, sizeof (GstSegment));
94   }
95   return result;
96 }
97
98 GType
99 gst_segment_get_type (void)
100 {
101   static GType gst_segment_type = 0;
102
103   if (G_UNLIKELY (gst_segment_type == 0)) {
104     gst_segment_type = g_boxed_type_register_static ("GstSegment",
105         (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
106   }
107
108   return gst_segment_type;
109 }
110
111 /**
112  * gst_segment_new:
113  *
114  * Allocate a new #GstSegment structure and initialize it using 
115  * gst_segment_init().
116  *
117  * Returns: a new #GstSegment, free with gst_segment_free().
118  */
119 GstSegment *
120 gst_segment_new (void)
121 {
122   GstSegment *result;
123
124   result = g_new0 (GstSegment, 1);
125   gst_segment_init (result, GST_FORMAT_UNDEFINED);
126
127   return result;
128 }
129
130 /**
131  * gst_segment_free:
132  * @segment: a #GstSegment
133  *
134  * Free the allocated segment @segment.
135  */
136 void
137 gst_segment_free (GstSegment * segment)
138 {
139   g_free (segment);
140 }
141
142 /**
143  * gst_segment_init:
144  * @segment: a #GstSegment structure.
145  * @format: the format of the segment.
146  *
147  * The start/last_stop positions are set to 0 and the stop/duration
148  * fields are set to -1 (unknown). The default rate of 1.0 and no
149  * flags are set.
150  *
151  * Initialize @segment to its default values.
152  */
153 void
154 gst_segment_init (GstSegment * segment, GstFormat format)
155 {
156   g_return_if_fail (segment != NULL);
157
158   segment->rate = 1.0;
159   segment->abs_rate = 1.0;
160   segment->applied_rate = 1.0;
161   segment->format = format;
162   segment->flags = 0;
163   segment->start = 0;
164   segment->stop = -1;
165   segment->time = 0;
166   segment->accum = 0;
167   segment->last_stop = 0;
168   segment->duration = -1;
169 }
170
171 /**
172  * gst_segment_set_duration:
173  * @segment: a #GstSegment structure.
174  * @format: the format of the segment.
175  * @duration: the duration of the segment info or -1 if unknown.
176  *
177  * Set the duration of the segment to @duration. This function is mainly
178  * used by elements that perform seeking and know the total duration of the
179  * segment. 
180  * 
181  * This field should be set to allow seeking requests relative to the
182  * duration.
183  */
184 void
185 gst_segment_set_duration (GstSegment * segment, GstFormat format,
186     gint64 duration)
187 {
188   g_return_if_fail (segment != NULL);
189
190   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
191     segment->format = format;
192   else
193     g_return_if_fail (segment->format == format);
194
195   segment->duration = duration;
196 }
197
198 /**
199  * gst_segment_set_last_stop:
200  * @segment: a #GstSegment structure.
201  * @format: the format of the segment.
202  * @position: the position 
203  *
204  * Set the last observed stop position in the segment to @position.
205  *
206  * This field should be set to allow seeking requests relative to the
207  * current playing position.
208  */
209 void
210 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
211     gint64 position)
212 {
213   g_return_if_fail (segment != NULL);
214
215   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
216     segment->format = format;
217   else
218     g_return_if_fail (segment->format == format);
219
220   segment->last_stop = MAX (segment->start, position);
221 }
222
223 /**
224  * gst_segment_set_seek:
225  * @segment: a #GstSegment structure.
226  * @rate: the rate of the segment.
227  * @format: the format of the segment.
228  * @flags: the seek flags for the segment
229  * @start_type: the seek method
230  * @start: the seek start value
231  * @stop_type: the seek method
232  * @stop: the seek stop value
233  * @update: boolean holding whether last_stop was updated.
234  *
235  * Update the segment structure with the field values of a seek event (see
236  * gst_event_new_seek()).
237  *
238  * After calling this method, the segment field last_stop and time will
239  * contain the requested new position in the segment. The new requested
240  * position in the segment depends on @rate and @start_type and @stop_type. 
241  *
242  * For positive @rate, the new position in the segment is the new @segment
243  * start field when it was updated with a @start_type different from
244  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
245  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment last_stop is
246  * unmodified.
247  *
248  * For negative @rate, the new position in the segment is the new @segment
249  * stop field when it was updated with a @stop_type different from
250  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
251  * duration of the segment will be used to update the stop position.
252  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
253  * @stop is ignored and @segment last_stop is unmodified.
254  *
255  * The applied rate of the segment will be set to 1.0 by default.
256  * If the caller can apply a rate change, it should update @segment
257  * rate and applied_rate after calling this function.
258  *
259  * @update will be set to TRUE if a seek should be performed to the segment 
260  * last_stop field. This field can be FALSE if, for example, only the @rate
261  * has been changed but not the playback position.
262  */
263 void
264 gst_segment_set_seek (GstSegment * segment, gdouble rate,
265     GstFormat format, GstSeekFlags flags,
266     GstSeekType start_type, gint64 start,
267     GstSeekType stop_type, gint64 stop, gboolean * update)
268 {
269   gboolean update_stop, update_start;
270   gint64 last_stop;
271
272   g_return_if_fail (rate != 0.0);
273   g_return_if_fail (segment != NULL);
274
275   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
276     segment->format = format;
277
278   update_start = update_stop = TRUE;
279
280   /* segment->start is never invalid */
281   switch (start_type) {
282     case GST_SEEK_TYPE_NONE:
283       /* no update to segment, take previous start */
284       start = segment->start;
285       update_start = FALSE;
286       break;
287     case GST_SEEK_TYPE_SET:
288       /* start holds desired position, map -1 to the start */
289       if (start == -1)
290         start = 0;
291       /* start must be 0 or the formats must match */
292       g_return_if_fail (start == 0 || segment->format == format);
293       break;
294     case GST_SEEK_TYPE_CUR:
295       g_return_if_fail (start == 0 || segment->format == format);
296       /* add start to currently configured segment */
297       start = segment->start + start;
298       break;
299     case GST_SEEK_TYPE_END:
300       if (segment->duration != -1) {
301         g_return_if_fail (start == 0 || segment->format == format);
302         /* add start to total length */
303         start = segment->duration + start;
304       } else {
305         /* no update if duration unknown */
306         start = segment->start;
307         update_start = FALSE;
308       }
309       break;
310   }
311   /* bring in sane range */
312   if (segment->duration != -1)
313     start = CLAMP (start, 0, segment->duration);
314   else
315     start = MAX (start, 0);
316
317   /* stop can be -1 if we have not configured a stop. */
318   switch (stop_type) {
319     case GST_SEEK_TYPE_NONE:
320       stop = segment->stop;
321       update_stop = FALSE;
322       break;
323     case GST_SEEK_TYPE_SET:
324       /* stop holds required value, if it's not -1, it must be of the same
325        * format as the segment. */
326       g_return_if_fail (stop == -1 || segment->format == format);
327       break;
328     case GST_SEEK_TYPE_CUR:
329       if (segment->stop != -1) {
330         /* only add compatible formats or 0 */
331         g_return_if_fail (stop == 0 || segment->format == format);
332         stop = segment->stop + stop;
333       } else
334         stop = -1;
335       break;
336     case GST_SEEK_TYPE_END:
337       if (segment->duration != -1) {
338         /* only add compatible formats or 0 */
339         g_return_if_fail (stop == 0 || segment->format == format);
340         stop = segment->duration + stop;
341       } else {
342         stop = segment->stop;
343         update_stop = FALSE;
344       }
345       break;
346   }
347
348   /* if we have a valid stop time, make sure it is clipped */
349   if (stop != -1) {
350     if (segment->duration != -1)
351       stop = CLAMP (stop, 0, segment->duration);
352     else
353       stop = MAX (stop, 0);
354   }
355
356   /* we can't have stop before start */
357   if (stop != -1)
358     g_return_if_fail (start <= stop);
359
360   segment->rate = rate;
361   segment->abs_rate = ABS (rate);
362   segment->applied_rate = 1.0;
363   segment->flags = flags;
364   segment->start = start;
365   last_stop = segment->last_stop;
366   if (update_start && rate > 0.0) {
367     last_stop = start;
368   }
369   if (update_stop && rate < 0.0) {
370     if (stop != -1)
371       last_stop = stop;
372     else {
373       if (segment->duration != -1)
374         last_stop = segment->duration;
375       else
376         last_stop = 0;
377     }
378   }
379   /* set update arg to reflect update of last_stop */
380   if (update)
381     *update = last_stop != segment->last_stop;
382
383   /* update new position */
384   segment->last_stop = last_stop;
385
386   segment->time = start;
387   segment->stop = stop;
388 }
389
390 /**
391  * gst_segment_set_newsegment:
392  * @segment: a #GstSegment structure.
393  * @update: flag indicating a new segment is started or updated
394  * @rate: the rate of the segment.
395  * @format: the format of the segment.
396  * @start: the new start value
397  * @stop: the new stop value
398  * @time: the new stream time
399  *
400  * Update the segment structure with the field values of a new segment event and
401  * with a default applied_rate of 1.0.
402  *
403  * Since: 0.10.6
404  */
405 void
406 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
407     GstFormat format, gint64 start, gint64 stop, gint64 time)
408 {
409   gst_segment_set_newsegment_full (segment, update, rate, 1.0, format, start,
410       stop, time);
411 }
412
413 /**
414  * gst_segment_set_newsegment_full:
415  * @segment: a #GstSegment structure.
416  * @update: flag indicating a new segment is started or updated
417  * @rate: the rate of the segment.
418  * @applied_rate: the applied rate of the segment.
419  * @format: the format of the segment.
420  * @start: the new start value
421  * @stop: the new stop value
422  * @time: the new stream time
423  *
424  * Update the segment structure with the field values of a new segment event.
425  */
426 void
427 gst_segment_set_newsegment_full (GstSegment * segment, gboolean update,
428     gdouble rate, gdouble applied_rate, GstFormat format, gint64 start,
429     gint64 stop, gint64 time)
430 {
431   gint64 duration;
432
433   g_return_if_fail (rate != 0.0);
434   g_return_if_fail (applied_rate != 0.0);
435   g_return_if_fail (segment != NULL);
436
437   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
438     segment->format = format;
439
440   /* any other format with 0 also gives time 0, the other values are
441    * invalid in the format though. */
442   if (format != segment->format && start == 0) {
443     format = segment->format;
444     if (stop != 0)
445       stop = -1;
446     if (time != 0)
447       time = -1;
448   }
449
450   g_return_if_fail (segment->format == format);
451
452   if (update) {
453     if (segment->rate > 0.0) {
454       /* an update to the current segment is done, elapsed time is
455        * difference between the old start and new start. */
456       if (start > segment->start)
457         duration = start - segment->start;
458       else
459         duration = 0;
460     } else {
461       /* for negative rates, the elapsed duration is the diff between the stop
462        * positions */
463       if (stop != -1 && stop < segment->stop)
464         duration = segment->stop - stop;
465       else
466         duration = 0;
467     }
468   } else {
469     /* the new segment has to be aligned with the old segment.
470      * We first update the accumulated time of the previous
471      * segment. the accumulated time is used when syncing to the
472      * clock. 
473      */
474     if (segment->stop != -1) {
475       duration = segment->stop - segment->start;
476     } else if (segment->last_stop != -1) {
477       /* else use last seen timestamp as segment stop */
478       duration = segment->last_stop - segment->start;
479     } else {
480       /* else we don't know and throw a warning.. really, this should
481        * be fixed in the element. */
482       g_warning ("closing segment of unknown duration, assuming duration of 0");
483       duration = 0;
484     }
485   }
486   /* use previous rate to calculate duration */
487   if (segment->abs_rate != 1.0)
488     duration /= segment->abs_rate;
489
490   /* accumulate duration */
491   segment->accum += duration;
492
493   /* then update the current segment */
494   segment->rate = rate;
495   segment->abs_rate = ABS (rate);
496   segment->applied_rate = applied_rate;
497   segment->start = start;
498   segment->last_stop = start;
499   segment->stop = stop;
500   segment->time = time;
501 }
502
503 /**
504  * gst_segment_to_stream_time:
505  * @segment: a #GstSegment structure.
506  * @format: the format of the segment.
507  * @position: the position in the segment
508  *
509  * Translate @position to stream time using the currently configured 
510  * segment. The @position value must be between @segment start and
511  * stop value. 
512  *
513  * This function is typically used by elements that need to operate on
514  * the stream time of the buffers it receives, such as effect plugins.
515  * In those use cases, @position is typically the buffer timestamp or 
516  * clock time that one wants to convert to the stream time.
517  * The stream time is always between 0 and the total duration of the 
518  * media stream. 
519  *
520  * Returns: the position in stream_time or -1 when an invalid position
521  * was given.
522  */
523 gint64
524 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
525     gint64 position)
526 {
527   gint64 result, start, stop, time;
528   gdouble abs_applied_rate;
529
530   g_return_val_if_fail (segment != NULL, -1);
531
532   /* format does not matter for -1 */
533   if (G_UNLIKELY (position == -1))
534     return -1;
535
536   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
537     segment->format = format;
538
539   /* if we have the position for the same format as the segment, we can compare
540    * the start and stop values, otherwise we assume 0 and -1 */
541   if (segment->format == format) {
542     start = segment->start;
543     stop = segment->stop;
544     time = segment->time;
545   } else {
546     start = 0;
547     stop = -1;
548     time = 0;
549   }
550
551   /* outside of the segment boundary stop */
552   if (G_UNLIKELY (stop != -1 && position > stop))
553     return -1;
554
555   /* before the segment boundary */
556   if (G_UNLIKELY (position < start))
557     return -1;
558
559   /* time must be known */
560   if (G_UNLIKELY (time == -1))
561     return -1;
562
563   /* bring to uncorrected position in segment */
564   result = position - start;
565
566   abs_applied_rate = ABS (segment->applied_rate);
567
568   /* correct for applied rate if needed */
569   if (abs_applied_rate != 1.0)
570     result *= abs_applied_rate;
571
572   /* add or subtract from segment time based on applied rate */
573   if (segment->applied_rate > 0.0) {
574     /* correct for segment time */
575     result += time;
576   } else {
577     /* correct for segment time, clamp at 0 */
578     if (time > result)
579       result = time - result;
580     else
581       result = 0;
582   }
583
584   return result;
585 }
586
587 /**
588  * gst_segment_to_running_time:
589  * @segment: a #GstSegment structure.
590  * @format: the format of the segment.
591  * @position: the position in the segment
592  *
593  * Translate @position to the total running time using the currently configured 
594  * and previously accumulated segments. Position is a value between @segment
595  * start and stop time.
596  *
597  * This function is typically used by elements that need to synchronize to the
598  * global clock in a pipeline. The runnning time is a constantly increasing value
599  * starting from 0. When gst_segment_init() is called, this value will reset to
600  * 0.
601  *
602  * This function returns -1 if the position is outside of @segment start and stop.
603  *
604  * Returns: the position as the total running time or -1 when an invalid position
605  * was given.
606  */
607 gint64
608 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
609     gint64 position)
610 {
611   gint64 result;
612   gint64 start, stop, accum;
613
614   g_return_val_if_fail (segment != NULL, -1);
615
616   if (G_UNLIKELY (position == -1))
617     return -1;
618
619   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
620     segment->format = format;
621
622   /* if we have the position for the same format as the segment, we can compare
623    * the start and stop values, otherwise we assume 0 and -1 */
624   if (segment->format == format) {
625     start = segment->start;
626     stop = segment->stop;
627     accum = segment->accum;
628   } else {
629     start = 0;
630     stop = -1;
631     accum = 0;
632   }
633
634   /* before the segment boundary */
635   if (G_UNLIKELY (position < start))
636     return -1;
637
638   if (segment->rate > 0.0) {
639     /* outside of the segment boundary stop */
640     if (G_UNLIKELY (stop != -1 && position > stop))
641       return -1;
642
643     /* bring to uncorrected position in segment */
644     result = position - start;
645   } else {
646     /* cannot continue if no stop position set or outside of
647      * the segment. */
648     if (G_UNLIKELY (stop == -1 || position > stop))
649       return -1;
650
651     /* bring to uncorrected position in segment */
652     result = stop - position;
653   }
654
655   /* scale based on the rate, avoid division by and conversion to 
656    * float when not needed */
657   if (segment->abs_rate != 1.0)
658     result /= segment->abs_rate;
659
660   /* correct for accumulated segments */
661   result += accum;
662
663   return result;
664 }
665
666 /**
667  * gst_segment_clip:
668  * @segment: a #GstSegment structure.
669  * @format: the format of the segment.
670  * @start: the start position in the segment
671  * @stop: the stop position in the segment
672  * @clip_start: the clipped start position in the segment
673  * @clip_stop: the clipped stop position in the segment
674  *
675  * Clip the given @start and @stop values to the segment boundaries given
676  * in @segment. @start and @stop are compared and clipped to @segment 
677  * start and stop values.
678  *
679  * If the function returns FALSE, @start and @stop are known to fall
680  * outside of @segment and @clip_start and @clip_stop are not updated.
681  *
682  * When the function returns TRUE, @clip_start and @clip_stop will be
683  * updated. If @clip_start or @clip_stop are different from @start or @stop
684  * respectively, the region fell partially in the segment.
685  *
686  * Note that when @stop is -1, @clip_stop will be set to the end of the
687  * segment. Depending on the use case, this may or may not be what you want.
688  *
689  * Returns: TRUE if the given @start and @stop times fall partially or 
690  *     completely in @segment, FALSE if the values are completely outside 
691  *     of the segment.
692  */
693 gboolean
694 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
695     gint64 stop, gint64 * clip_start, gint64 * clip_stop)
696 {
697   g_return_val_if_fail (segment != NULL, FALSE);
698
699   if (G_UNLIKELY (segment->format == GST_FORMAT_UNDEFINED))
700     segment->format = format;
701   else
702     g_return_val_if_fail (segment->format == format, FALSE);
703
704   /* if we have a stop position and a valid start and start is bigger, 
705    * we're outside of the segment */
706   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
707     return FALSE;
708
709   /* if a stop position is given and is before the segment start,
710    * we're outside of the segment */
711   if (G_UNLIKELY (stop != -1 && stop != start && stop <= segment->start))
712     return FALSE;
713
714   if (clip_start) {
715     if (start == -1)
716       *clip_start = -1;
717     else
718       *clip_start = MAX (start, segment->start);
719   }
720
721   if (clip_stop) {
722     if (stop == -1)
723       *clip_stop = segment->stop;
724     else if (segment->stop == -1)
725       *clip_stop = MAX (-1, stop);
726     else
727       *clip_stop = MIN (stop, segment->stop);
728
729     if (segment->duration != -1)
730       *clip_stop = MIN (*clip_stop, segment->duration);
731   }
732
733   return TRUE;
734 }