2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
4 * gstsegment.c: GstSegment subsystem
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.
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.
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.
23 #include "gst_private.h"
26 #include "gstsegment.h"
30 * @short_description: Structure describing the configured region of interest
32 * @see_also: #GstEvent
34 * This helper structure holds the relevant values for tracking the region of
35 * interest in a media file, called a segment.
37 * The structure can be used for two purposes:
39 * <listitem><para>performing seeks (handling seek events)</para></listitem>
40 * <listitem><para>tracking playback regions (handling newsegment events)</para></listitem>
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.
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.
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
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
58 * The current position in the segment should be set with the gst_segment_set_last_stop().
59 * The public last_stop field contains the last set stop position in the segment.
61 * For elements that perform seeks, the current segment should be updated with the
62 * gst_segment_set_seek() and the values from the seek event. This method will update
63 * all the segment fields. The last_pos field will contain the new playback position.
64 * If the cur_type was different from GST_SEEK_TYPE_NONE, playback continues from
65 * the last_pos position, possibly with updated flags or rate.
67 * For elements that want to us #GstSegment to track the playback region, use
68 * gst_segment_set_newsegment() to update the segment fields with the information from
69 * the newsegment event. The gst_segment_clip() method can be used to check and clip
70 * the media data to the segment boundaries.
72 * For elements that want to synchronize to the pipeline clock, gst_segment_to_running_time()
73 * can be used to convert a timestamp to a value that can be used to synchronize
74 * to the clock. This function takes into account all accumulated segments.
76 * For elements that need to perform operations on media data in stream_time,
77 * gst_segment_to_stream_time() can be used to convert a timestamp and the segment
78 * info to stream time (which is always between 0 and the duration of the stream).
80 * Last reviewed on 2005-12-12 (0.10.0)
84 gst_segment_copy (GstSegment * segment)
86 GstSegment *result = NULL;
89 result = gst_segment_new ();
90 memcpy (result, segment, sizeof (GstSegment));
96 gst_segment_get_type (void)
98 static GType gst_segment_type = 0;
100 if (!gst_segment_type) {
101 gst_segment_type = g_boxed_type_register_static ("GstSegment",
102 (GBoxedCopyFunc) gst_segment_copy, (GBoxedFreeFunc) gst_segment_free);
105 return gst_segment_type;
111 * Allocate a new #GstSegment structure and initialize it using
112 * gst_segment_init().
114 * Returns: a new #GstSegment, free with gst_segment_free().
117 gst_segment_new (void)
121 result = g_new0 (GstSegment, 1);
122 gst_segment_init (result, GST_FORMAT_UNDEFINED);
129 * @segment: a #GstSegment
131 * Free the allocated segment @segment.
134 gst_segment_free (GstSegment * segment)
141 * @segment: a #GstSegment structure.
142 * @format: the format of the segment.
144 * The start/last_stop positions are set to 0 and the stop/duration
145 * fields are set to -1 (unknown). The default rate of 1.0 and no
148 * Initialize @segment to its default values.
151 gst_segment_init (GstSegment * segment, GstFormat format)
153 g_return_if_fail (segment != NULL);
156 segment->abs_rate = 1.0;
157 segment->format = format;
163 segment->last_stop = 0;
164 segment->duration = -1;
168 * gst_segment_set_duration:
169 * @segment: a #GstSegment structure.
170 * @format: the format of the segment.
171 * @duration: the duration of the segment info.
173 * Set the duration of the segment to @duration. This function is mainly
174 * used by elements that perform seeking and know the total duration of the
177 * This field should be set to allow seeking request relative to the
181 gst_segment_set_duration (GstSegment * segment, GstFormat format,
184 g_return_if_fail (segment != NULL);
186 if (segment->format == GST_FORMAT_UNDEFINED)
187 segment->format = format;
189 g_return_if_fail (segment->format == format);
191 segment->duration = duration;
195 * gst_segment_set_last_stop:
196 * @segment: a #GstSegment structure.
197 * @format: the format of the segment.
198 * @position: the position
200 * Set the last observed stop position in the segment to @position.
203 gst_segment_set_last_stop (GstSegment * segment, GstFormat format,
206 g_return_if_fail (segment != NULL);
208 if (segment->format == GST_FORMAT_UNDEFINED)
209 segment->format = format;
211 g_return_if_fail (segment->format == format);
213 segment->last_stop = MAX (segment->start, position);
217 * gst_segment_set_seek:
218 * @segment: a #GstSegment structure.
219 * @rate: the rate of the segment.
220 * @format: the format of the segment.
221 * @flags: the seek flags for the segment
222 * @cur_type: the seek method
223 * @cur: the seek start value
224 * @stop_type: the seek method
225 * @stop: the seek stop value
226 * @update: boolean holding whether an update the current segment is
229 * Update the segment structure with the field values of a seek event.
231 * After calling this method, the segment field last_stop will contain
232 * the requested new position in the segment. If the cur_type is different
233 * from GST_SEEK_TYPE_NONE, the current position is not updated and
234 * streaming should continue from the last position, possibly with
235 * updated rate, flags or stop position.
238 gst_segment_set_seek (GstSegment * segment, gdouble rate,
239 GstFormat format, GstSeekFlags flags,
240 GstSeekType cur_type, gint64 cur,
241 GstSeekType stop_type, gint64 stop, gboolean * update)
243 gboolean update_stop, update_start;
245 g_return_if_fail (rate != 0.0);
246 g_return_if_fail (segment != NULL);
248 if (segment->format == GST_FORMAT_UNDEFINED)
249 segment->format = format;
251 g_return_if_fail (segment->format == format);
253 update_stop = update_start = TRUE;
255 /* start is never invalid */
257 case GST_SEEK_TYPE_NONE:
258 /* no update to segment */
259 cur = segment->start;
260 update_start = FALSE;
262 case GST_SEEK_TYPE_SET:
263 /* cur holds desired position */
265 case GST_SEEK_TYPE_CUR:
266 /* add cur to currently configure segment */
267 cur = segment->start + cur;
269 case GST_SEEK_TYPE_END:
270 if (segment->duration != -1) {
271 /* add cur to total length */
272 cur = segment->duration + cur;
274 /* no update if duration unknown */
275 cur = segment->start;
276 update_start = FALSE;
280 /* bring in sane range */
281 if (segment->duration != -1)
282 cur = CLAMP (cur, 0, segment->duration);
286 /* stop can be -1 if we have not configured a stop. */
288 case GST_SEEK_TYPE_NONE:
289 stop = segment->stop;
292 case GST_SEEK_TYPE_SET:
293 /* stop folds required value */
295 case GST_SEEK_TYPE_CUR:
296 if (segment->stop != -1)
297 stop = segment->stop + stop;
301 case GST_SEEK_TYPE_END:
302 if (segment->duration != -1)
303 stop = segment->duration + stop;
305 stop = segment->stop;
311 /* if we have a valid stop time, make sure it is clipped */
313 if (segment->duration != -1)
314 stop = CLAMP (stop, 0, segment->duration);
316 stop = MAX (stop, 0);
319 /* we can't have stop before start */
321 g_return_if_fail (cur <= stop);
323 segment->rate = rate;
324 segment->abs_rate = ABS (rate);
325 segment->flags = flags;
326 segment->start = cur;
328 segment->last_stop = cur;
330 segment->time = segment->last_stop;
331 segment->stop = stop;
334 *update = update_start || update_stop;
338 * gst_segment_set_newsegment:
339 * @segment: a #GstSegment structure.
340 * @update: flag indicating a new segment is started or updated
341 * @rate: the rate of the segment.
342 * @format: the format of the segment.
343 * @start: the new start value
344 * @stop: the new stop value
345 * @time: the new stream time
347 * Update the segment structure with the field values of a new segment event.
350 gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
351 GstFormat format, gint64 start, gint64 stop, gint64 time)
355 g_return_if_fail (rate != 0.0);
356 g_return_if_fail (segment != NULL);
358 if (segment->format == GST_FORMAT_UNDEFINED)
359 segment->format = format;
361 /* any other format with 0 also gives time 0, the other values are
362 * invalid in the format though. */
363 if (format != segment->format && start == 0) {
364 format = segment->format;
371 g_return_if_fail (segment->format == format);
374 /* an update to the current segment is done, elapsed time is
375 * difference between the old start and new start. */
376 duration = start - segment->start;
378 /* the new segment has to be aligned with the old segment.
379 * We first update the accumulated time of the previous
380 * segment. the accumulated time is used when syncing to the
383 if (GST_CLOCK_TIME_IS_VALID (segment->stop)) {
384 duration = segment->stop - segment->start;
385 } else if (GST_CLOCK_TIME_IS_VALID (segment->last_stop)) {
386 /* else use last seen timestamp as segment stop */
387 duration = segment->last_stop - segment->start;
389 /* else we don't know */
393 /* use previous rate to calculate duration */
394 segment->accum += gst_gdouble_to_guint64 (
395 (gst_guint64_to_gdouble (duration) / segment->abs_rate));
397 /* then update the current segment */
398 segment->rate = rate;
399 segment->abs_rate = ABS (rate);
400 segment->start = start;
401 segment->last_stop = start;
402 segment->stop = stop;
403 segment->time = time;
407 * gst_segment_to_stream_time:
408 * @segment: a #GstSegment structure.
409 * @format: the format of the segment.
410 * @position: the position in the segment
412 * Translate @position to stream time using the currently configured
415 * This function is typically used by elements that need to operate on
416 * the stream time of the buffers it receives, such as effect plugins.
417 * The stream time is always between 0 and the total duration of the
420 * Returns: the position in stream_time.
423 gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
428 g_return_val_if_fail (segment != NULL, FALSE);
430 if (segment->format == GST_FORMAT_UNDEFINED)
431 segment->format = format;
433 g_return_val_if_fail (segment->format == format, FALSE);
435 if ((time = segment->time) == -1)
438 if (position != -1 && position >= segment->start)
439 result = ((position - segment->start) / segment->abs_rate) + time;
447 * gst_segment_to_running_time:
448 * @segment: a #GstSegment structure.
449 * @format: the format of the segment.
450 * @position: the position in the segment
452 * Translate @position to the total running time using the currently configured
455 * This function is typically used by elements that need to synchronize to the
456 * global clock in a pipeline. The runnning time is a constantly increasing value
459 * Returns: the position as the total running time.
462 gst_segment_to_running_time (GstSegment * segment, GstFormat format,
467 g_return_val_if_fail (segment != NULL, -1);
469 if (segment->format == GST_FORMAT_UNDEFINED)
470 segment->format = format;
471 else if (segment->accum)
472 g_return_val_if_fail (segment->format == format, -1);
474 if (position != -1 && position >= segment->start)
475 result = ((position - segment->start) / segment->abs_rate) + segment->accum;
484 * @segment: a #GstSegment structure.
485 * @format: the format of the segment.
486 * @start: the start position in the segment
487 * @stop: the stop position in the segment
488 * @clip_start: the clipped start position in the segment
489 * @clip_stop: the clipped stop position in the segment
491 * Clip the given @start and @stop values to the segment boundaries given
494 * Returns: TRUE if the given @start and @stop times fall partially in
495 * @segment, FALSE if the values are completely outside of the segment.
498 gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
499 gint64 stop, gint64 * clip_start, gint64 * clip_stop)
501 g_return_val_if_fail (segment != NULL, FALSE);
503 if (segment->format == GST_FORMAT_UNDEFINED)
504 segment->format = format;
506 g_return_val_if_fail (segment->format == format, FALSE);
508 /* if we have a stop position and a valid start and start is bigger,
509 * we're outside of the segment */
510 if (segment->stop != -1 && start != -1 && start >= segment->stop)
513 /* if a stop position is given and is before the segment start,
514 * we're outside of the segment */
515 if (stop != -1 && stop <= segment->start)
522 *clip_start = MAX (start, segment->start);
527 *clip_stop = segment->stop;
528 else if (segment->stop == -1)
529 *clip_stop = MAX (-1, stop);
531 *clip_stop = MIN (stop, segment->stop);
533 if (segment->duration != -1)
534 *clip_stop = MIN (*clip_stop, segment->duration);