seeking: add more logging for seeking
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "gst_private.h"
23
24 #include <math.h>
25
26 #include "gstutils.h"
27 #include "gstsegment.h"
28
29 /**
30  * SECTION:gstsegment
31  * @short_description: Structure describing the configured region of interest
32  *                     in a media file.
33  * @see_also: #GstEvent
34  *
35  * This helper structure holds the relevant values for tracking the region of
36  * interest in a media file, called a segment.
37  *
38  * The structure can be used for two purposes:
39  * <itemizedlist>
40  *   <listitem><para>performing seeks (handling seek events)</para></listitem>
41  *   <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
42  * </itemizedlist>
43  *
44  * The segment is usually configured by the application with a seek event which
45  * is propagated upstream and eventually handled by an element that performs the seek.
46  *
47  * The configured segment is then propagated back downstream with a newsegment event.
48  * This information is then used to clip media to the segment boundaries.
49  *
50  * A segment structure is initialized with gst_segment_init(), which takes a #GstFormat
51  * that will be used as the format of the segment values. The segment will be configured
52  * with a start value of 0 and a stop/duration of -1, which is undefined. The default
53  * rate and applied_rate is 1.0.
54  *
55  * The public duration field contains the duration of the segment. When using
56  * the segment for seeking, the start and time members should normally be left
57  * to their default 0 value. The stop position is left to -1 unless explicitly
58  * configured to a different value after a seek event.
59  *
60  * The current position in the segment should be set by changing the position
61  * member in the structure.
62  *
63  * For elements that perform seeks, the current segment should be updated with the
64  * gst_segment_do_seek() and the values from the seek event. This method will update
65  * all the segment fields. The position field will contain the new playback position.
66  * If the start_type was different from GST_SEEK_TYPE_NONE, playback continues from
67  * the position position, possibly with updated flags or rate.
68  *
69  * For elements that want to use #GstSegment to track the playback region,
70  * update the segment fields with the information from the newsegment event.
71  * 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 the base 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 2012-03-29 (0.11.3)
84  */
85
86 /**
87  * gst_segment_copy:
88  * @segment: (transfer none): a #GstSegment
89  *
90  * Create a copy of given @segment.
91  *
92  * Free-function: gst_segment_free
93  *
94  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
95  */
96 GstSegment *
97 gst_segment_copy (const GstSegment * segment)
98 {
99   GstSegment *result = NULL;
100
101   if (segment) {
102     result = (GstSegment *) g_slice_copy (sizeof (GstSegment), segment);
103   }
104   return result;
105 }
106
107 /**
108  * gst_segment_copy_into:
109  * @src: (transfer none): a #GstSegment
110  * @dest: (transfer none): a #GstSegment
111  *
112  * Copy the contents of @src into @dest.
113  */
114 void
115 gst_segment_copy_into (const GstSegment * src, GstSegment * dest)
116 {
117   memcpy (dest, src, sizeof (GstSegment));
118 }
119
120 G_DEFINE_BOXED_TYPE (GstSegment, gst_segment,
121     (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
122
123 /**
124  * gst_segment_new:
125  *
126  * Allocate a new #GstSegment structure and initialize it using
127  * gst_segment_init().
128  *
129  * Free-function: gst_segment_free
130  *
131  * Returns: (transfer full): a new #GstSegment, free with gst_segment_free().
132  */
133 GstSegment *
134 gst_segment_new (void)
135 {
136   GstSegment *result;
137
138   result = g_slice_new0 (GstSegment);
139   gst_segment_init (result, GST_FORMAT_UNDEFINED);
140
141   return result;
142 }
143
144 /**
145  * gst_segment_free:
146  * @segment: (in) (transfer full): a #GstSegment
147  *
148  * Free the allocated segment @segment.
149  */
150 void
151 gst_segment_free (GstSegment * segment)
152 {
153   g_slice_free (GstSegment, segment);
154 }
155
156 /**
157  * gst_segment_init:
158  * @segment: a #GstSegment structure.
159  * @format: the format of the segment.
160  *
161  * The start/position fields are set to 0 and the stop/duration
162  * fields are set to -1 (unknown). The default rate of 1.0 and no
163  * flags are set.
164  *
165  * Initialize @segment to its default values.
166  */
167 void
168 gst_segment_init (GstSegment * segment, GstFormat format)
169 {
170   g_return_if_fail (segment != NULL);
171
172   segment->flags = GST_SEGMENT_FLAG_NONE;
173   segment->rate = 1.0;
174   segment->applied_rate = 1.0;
175   segment->format = format;
176   segment->base = 0;
177   segment->offset = 0;
178   segment->start = 0;
179   segment->stop = -1;
180   segment->time = 0;
181   segment->position = 0;
182   segment->duration = -1;
183 }
184
185 /**
186  * gst_segment_do_seek:
187  * @segment: a #GstSegment structure.
188  * @rate: the rate of the segment.
189  * @format: the format of the segment.
190  * @flags: the segment flags for the segment
191  * @start_type: the seek method
192  * @start: the seek start value
193  * @stop_type: the seek method
194  * @stop: the seek stop value
195  * @update: boolean holding whether position was updated.
196  *
197  * Update the segment structure with the field values of a seek event (see
198  * gst_event_new_seek()).
199  *
200  * After calling this method, the segment field position and time will
201  * contain the requested new position in the segment. The new requested
202  * position in the segment depends on @rate and @start_type and @stop_type.
203  *
204  * For positive @rate, the new position in the segment is the new @segment
205  * start field when it was updated with a @start_type different from
206  * #GST_SEEK_TYPE_NONE. If no update was performed on @segment start position
207  * (#GST_SEEK_TYPE_NONE), @start is ignored and @segment position is
208  * unmodified.
209  *
210  * For negative @rate, the new position in the segment is the new @segment
211  * stop field when it was updated with a @stop_type different from
212  * #GST_SEEK_TYPE_NONE. If no stop was previously configured in the segment, the
213  * duration of the segment will be used to update the stop position.
214  * If no update was performed on @segment stop position (#GST_SEEK_TYPE_NONE),
215  * @stop is ignored and @segment position is unmodified.
216  *
217  * The applied rate of the segment will be set to 1.0 by default.
218  * If the caller can apply a rate change, it should update @segment
219  * rate and applied_rate after calling this function.
220  *
221  * @update will be set to TRUE if a seek should be performed to the segment
222  * position field. This field can be FALSE if, for example, only the @rate
223  * has been changed but not the playback position.
224  *
225  * Returns: %TRUE if the seek could be performed.
226  */
227 gboolean
228 gst_segment_do_seek (GstSegment * segment, gdouble rate,
229     GstFormat format, GstSeekFlags flags,
230     GstSeekType start_type, guint64 start,
231     GstSeekType stop_type, guint64 stop, gboolean * update)
232 {
233   gboolean update_stop, update_start;
234   guint64 position, base;
235
236   g_return_val_if_fail (rate != 0.0, FALSE);
237   g_return_val_if_fail (segment != NULL, FALSE);
238   g_return_val_if_fail (segment->format == format, FALSE);
239
240   update_start = update_stop = TRUE;
241
242   position = segment->position;
243
244   /* segment->start is never invalid */
245   switch (start_type) {
246     case GST_SEEK_TYPE_NONE:
247       /* no update to segment, take previous start */
248       start = segment->start;
249       update_start = FALSE;
250       break;
251     case GST_SEEK_TYPE_SET:
252       /* start holds desired position, map -1 to the start */
253       if (start == -1)
254         start = 0;
255       break;
256     case GST_SEEK_TYPE_END:
257       if (segment->duration != -1) {
258         /* add start to total length */
259         start = segment->duration + start;
260       } else {
261         /* no update if duration unknown */
262         start = segment->start;
263         update_start = FALSE;
264       }
265       break;
266   }
267   /* bring in sane range */
268   if (segment->duration != -1)
269     start = MIN (start, segment->duration);
270   else
271     start = MAX (start, 0);
272
273   /* stop can be -1 if we have not configured a stop. */
274   switch (stop_type) {
275     case GST_SEEK_TYPE_NONE:
276       stop = segment->stop;
277       update_stop = FALSE;
278       break;
279     case GST_SEEK_TYPE_SET:
280       /* stop holds required value */
281       break;
282     case GST_SEEK_TYPE_END:
283       if (segment->duration != -1) {
284         stop = segment->duration + stop;
285       } else {
286         stop = segment->stop;
287         update_stop = FALSE;
288       }
289       break;
290   }
291
292   /* if we have a valid stop time, make sure it is clipped */
293   if (stop != -1) {
294     if (segment->duration != -1)
295       stop = CLAMP (stop, 0, segment->duration);
296     else
297       stop = MAX (stop, 0);
298   }
299
300   /* we can't have stop before start */
301   if (stop != -1) {
302     if (start > stop) {
303       g_return_val_if_fail (start <= stop, FALSE);
304       GST_WARNING ("segment update failed: start(%" G_GUINT64_FORMAT
305           ") > stop(%" G_GUINT64_FORMAT ")", start, stop);
306       return FALSE;
307     }
308   }
309
310   if (flags & GST_SEEK_FLAG_FLUSH) {
311     /* flush resets the running_time */
312     base = 0;
313   } else {
314     /* remember the elapsed time */
315     base = gst_segment_to_running_time (segment, format, position);
316     GST_DEBUG ("updated segment.base: %" G_GUINT64_FORMAT, base);
317   }
318
319   if (update_start && rate > 0.0) {
320     position = start;
321   }
322   if (update_stop && rate < 0.0) {
323     if (stop != -1)
324       position = stop;
325     else {
326       if (segment->duration != -1)
327         position = segment->duration;
328       else
329         position = 0;
330     }
331   }
332
333   /* set update arg to reflect update of position */
334   if (update)
335     *update = position != segment->position;
336
337   /* update new values */
338   /* be explicit about our GstSeekFlag -> GstSegmentFlag conversion */
339   segment->flags = GST_SEGMENT_FLAG_NONE;
340   if ((flags & GST_SEEK_FLAG_FLUSH) != 0)
341     segment->flags |= GST_SEGMENT_FLAG_RESET;
342   if ((flags & GST_SEEK_FLAG_SKIP) != 0)
343     segment->flags |= GST_SEGMENT_FLAG_SKIP;
344   if ((flags & GST_SEEK_FLAG_SEGMENT) != 0)
345     segment->flags |= GST_SEGMENT_FLAG_SEGMENT;
346
347   segment->rate = rate;
348   segment->applied_rate = 1.0;
349
350   segment->base = base;
351   if (rate > 0.0)
352     segment->offset = position - start;
353   else {
354     if (stop != -1)
355       segment->offset = stop - position;
356     else if (segment->duration != -1)
357       segment->offset = segment->duration - position;
358     else
359       segment->offset = 0;
360   }
361
362   segment->start = start;
363   segment->stop = stop;
364   segment->time = start;
365   segment->position = position;
366
367   GST_INFO ("segment updated: %" GST_SEGMENT_FORMAT, segment);
368
369   return TRUE;
370 }
371
372 /**
373  * gst_segment_to_stream_time:
374  * @segment: a #GstSegment structure.
375  * @format: the format of the segment.
376  * @position: the position in the segment
377  *
378  * Translate @position to stream time using the currently configured
379  * segment. The @position value must be between @segment start and
380  * stop value.
381  *
382  * This function is typically used by elements that need to operate on
383  * the stream time of the buffers it receives, such as effect plugins.
384  * In those use cases, @position is typically the buffer timestamp or
385  * clock time that one wants to convert to the stream time.
386  * The stream time is always between 0 and the total duration of the
387  * media stream.
388  *
389  * Returns: the position in stream_time or -1 when an invalid position
390  * was given.
391  */
392 guint64
393 gst_segment_to_stream_time (const GstSegment * segment, GstFormat format,
394     guint64 position)
395 {
396   guint64 result, start, stop, time;
397   gdouble abs_applied_rate;
398
399   /* format does not matter for -1 */
400   if (G_UNLIKELY (position == -1))
401     return -1;
402
403   g_return_val_if_fail (segment != NULL, -1);
404   g_return_val_if_fail (segment->format == format, -1);
405
406   stop = segment->stop;
407
408   /* outside of the segment boundary stop */
409   if (G_UNLIKELY (stop != -1 && position > stop))
410     return -1;
411
412   start = segment->start;
413
414   /* before the segment boundary */
415   if (G_UNLIKELY (position < start))
416     return -1;
417
418   time = segment->time;
419
420   /* time must be known */
421   if (G_UNLIKELY (time == -1))
422     return -1;
423
424   /* bring to uncorrected position in segment */
425   result = position - start;
426
427   abs_applied_rate = ABS (segment->applied_rate);
428
429   /* correct for applied rate if needed */
430   if (G_UNLIKELY (abs_applied_rate != 1.0))
431     result *= abs_applied_rate;
432
433   /* add or subtract from segment time based on applied rate */
434   if (G_LIKELY (segment->applied_rate > 0.0)) {
435     /* correct for segment time */
436     result += time;
437   } else {
438     /* correct for segment time, clamp at 0. Streams with a negative
439      * applied_rate have timestamps between start and stop, as usual, but have
440      * the time member starting high and going backwards.  */
441     if (G_LIKELY (time > result))
442       result = time - result;
443     else
444       result = 0;
445   }
446
447   return result;
448 }
449
450 /**
451  * gst_segment_to_running_time:
452  * @segment: a #GstSegment structure.
453  * @format: the format of the segment.
454  * @position: the position in the segment
455  *
456  * Translate @position to the total running time using the currently configured
457  * segment. Position is a value between @segment start and stop time.
458  *
459  * This function is typically used by elements that need to synchronize to the
460  * global clock in a pipeline. The runnning time is a constantly increasing value
461  * starting from 0. When gst_segment_init() is called, this value will reset to
462  * 0.
463  *
464  * This function returns -1 if the position is outside of @segment start and stop.
465  *
466  * Returns: the position as the total running time or -1 when an invalid position
467  * was given.
468  */
469 guint64
470 gst_segment_to_running_time (const GstSegment * segment, GstFormat format,
471     guint64 position)
472 {
473   guint64 result;
474   guint64 start, stop;
475   gdouble abs_rate;
476
477   if (G_UNLIKELY (position == -1)) {
478     GST_WARNING ("invalid position (-1)");
479     return -1;
480   }
481
482   g_return_val_if_fail (segment != NULL, -1);
483   g_return_val_if_fail (segment->format == format, -1);
484
485   start = segment->start;
486
487   if (segment->rate > 0.0)
488     start += segment->offset;
489
490   /* before the segment boundary */
491   if (G_UNLIKELY (position < start)) {
492     GST_WARNING ("position(%" G_GUINT64_FORMAT ") < start(%" G_GUINT64_FORMAT
493         ")", position, start);
494     return -1;
495   }
496
497   stop = segment->stop;
498
499   if (G_LIKELY (segment->rate > 0.0)) {
500     /* after of the segment boundary */
501     if (G_UNLIKELY (stop != -1 && position > stop)) {
502       GST_WARNING ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
503           ")", position, stop);
504       return -1;
505     }
506
507     /* bring to uncorrected position in segment */
508     result = position - start;
509   } else {
510     /* cannot continue if no stop position set or outside of
511      * the segment. */
512     if (G_UNLIKELY (stop == -1)) {
513       GST_WARNING ("invalid stop (-1)");
514       return -1;
515     }
516
517     stop -= segment->offset;
518     if (G_UNLIKELY (position > stop)) {
519       GST_WARNING ("position(%" G_GUINT64_FORMAT ") > stop(%" G_GUINT64_FORMAT
520           ")", position, stop);
521       return -1;
522     }
523
524     /* bring to uncorrected position in segment */
525     result = stop - position;
526   }
527
528   /* scale based on the rate, avoid division by and conversion to
529    * float when not needed */
530   abs_rate = ABS (segment->rate);
531   if (G_UNLIKELY (abs_rate != 1.0))
532     result /= abs_rate;
533
534   /* correct for base of the segment */
535   result += segment->base;
536
537   return result;
538 }
539
540 /**
541  * gst_segment_clip:
542  * @segment: a #GstSegment structure.
543  * @format: the format of the segment.
544  * @start: the start position in the segment
545  * @stop: the stop position in the segment
546  * @clip_start: (out) (allow-none): the clipped start position in the segment
547  * @clip_stop: (out) (allow-none): the clipped stop position in the segment
548  *
549  * Clip the given @start and @stop values to the segment boundaries given
550  * in @segment. @start and @stop are compared and clipped to @segment
551  * start and stop values.
552  *
553  * If the function returns FALSE, @start and @stop are known to fall
554  * outside of @segment and @clip_start and @clip_stop are not updated.
555  *
556  * When the function returns TRUE, @clip_start and @clip_stop will be
557  * updated. If @clip_start or @clip_stop are different from @start or @stop
558  * respectively, the region fell partially in the segment.
559  *
560  * Note that when @stop is -1, @clip_stop will be set to the end of the
561  * segment. Depending on the use case, this may or may not be what you want.
562  *
563  * Returns: TRUE if the given @start and @stop times fall partially or
564  *     completely in @segment, FALSE if the values are completely outside
565  *     of the segment.
566  */
567 gboolean
568 gst_segment_clip (const GstSegment * segment, GstFormat format, guint64 start,
569     guint64 stop, guint64 * clip_start, guint64 * clip_stop)
570 {
571   g_return_val_if_fail (segment != NULL, FALSE);
572   g_return_val_if_fail (segment->format == format, FALSE);
573
574   /* if we have a stop position and a valid start and start is bigger,
575    * we're outside of the segment */
576   if (G_UNLIKELY (segment->stop != -1 && start != -1 && start >= segment->stop))
577     return FALSE;
578
579   /* if a stop position is given and is before the segment start,
580    * we're outside of the segment. Special case is were start
581    * and stop are equal to the segment start. In that case we
582    * are inside the segment. */
583   if (G_UNLIKELY (stop != -1 && (stop < segment->start || (start != stop
584                   && stop == segment->start))))
585     return FALSE;
586
587   if (clip_start) {
588     if (start == -1)
589       *clip_start = -1;
590     else
591       *clip_start = MAX (start, segment->start);
592   }
593
594   if (clip_stop) {
595     if (stop == -1)
596       *clip_stop = segment->stop;
597     else if (segment->stop == -1)
598       *clip_stop = stop;
599     else
600       *clip_stop = MIN (stop, segment->stop);
601   }
602
603   return TRUE;
604 }
605
606 /**
607  * gst_segment_to_position:
608  * @segment: a #GstSegment structure.
609  * @format: the format of the segment.
610  * @running_time: the running_time in the segment
611  *
612  * Convert @running_time into a position in the segment so that
613  * gst_segment_to_running_time() with that position returns @running_time.
614  *
615  * Returns: the position in the segment for @running_time. This function returns
616  * -1 when @running_time is -1 or when it is not inside @segment.
617  */
618 guint64
619 gst_segment_to_position (const GstSegment * segment, GstFormat format,
620     guint64 running_time)
621 {
622   guint64 result;
623   guint64 start, stop, base;
624   gdouble abs_rate;
625
626   if (G_UNLIKELY (running_time == -1))
627     return -1;
628
629   g_return_val_if_fail (segment != NULL, -1);
630   g_return_val_if_fail (segment->format == format, FALSE);
631
632   base = segment->base;
633
634   /* this running_time was for a previous segment */
635   if (running_time < base)
636     return -1;
637
638   /* start by subtracting the base time */
639   result = running_time - base;
640
641   /* move into the segment at the right rate */
642   abs_rate = ABS (segment->rate);
643   if (G_UNLIKELY (abs_rate != 1.0))
644     result = ceil (result * abs_rate);
645
646   start = segment->start;
647   stop = segment->stop;
648
649   if (G_LIKELY (segment->rate > 0.0)) {
650     /* bring to corrected position in segment */
651     result += start;
652
653     /* outside of the segment boundary stop */
654     if (G_UNLIKELY (stop != -1 && result > stop))
655       return -1;
656   } else {
657     /* cannot continue if no stop position set or outside of
658      * the segment. */
659     if (G_UNLIKELY (stop == -1 || result + start > stop))
660       return -1;
661
662     /* bring to corrected position in segment */
663     result = stop - result;
664   }
665   return result;
666 }
667
668
669 /**
670  * gst_segment_set_running_time:
671  * @segment: a #GstSegment structure.
672  * @format: the format of the segment.
673  * @running_time: the running_time in the segment
674  *
675  * Adjust the start/stop and base values of @segment such that the next valid
676  * buffer will be one with @running_time.
677  *
678  * Returns: %TRUE if the segment could be updated successfully. If %FALSE is
679  * returned, @running_time is -1 or not in @segment.
680  */
681 gboolean
682 gst_segment_set_running_time (GstSegment * segment, GstFormat format,
683     guint64 running_time)
684 {
685   guint64 position;
686   guint64 start, stop;
687
688   /* start by bringing the running_time into the segment position */
689   position = gst_segment_to_position (segment, format, running_time);
690
691   /* we must have a valid position now */
692   if (G_UNLIKELY (position == -1))
693     return FALSE;
694
695   start = segment->start;
696   stop = segment->stop;
697
698   if (G_LIKELY (segment->rate > 0.0)) {
699     /* update the start and time values */
700     start = position;
701   } else {
702     /* reverse, update stop */
703     stop = position;
704   }
705   /* and base time is exactly the running time */
706   segment->time = gst_segment_to_stream_time (segment, format, start);
707   segment->start = start;
708   segment->stop = stop;
709   segment->base = running_time;
710
711   return TRUE;
712 }