Added segment helper structure and methods. Not fully implemented yet.

Original commit message from CVS:
* check/Makefile.am:
* check/gst/gstsegment.c: (GST_START_TEST), (gstevents_suite),
(main):
* gst/Makefile.am:
* gst/gst.h:
* gst/gstsegment.c: (gst_segment_init), (gst_segment_set_duration),
(gst_segment_set_seek), (gst_segment_set_newsegment),
(gst_segment_to_stream_time), (gst_segment_to_running_time),
(gst_segment_clip):
* gst/gstsegment.h:
Added segment helper structure and methods. Not fully implemented
yet.
Added segment check.
This commit is contained in:
Wim Taymans 2005-11-20 19:11:09 +00:00
parent c05ba2d666
commit 2754334f05
9 changed files with 1243 additions and 0 deletions

View file

@ -1,3 +1,19 @@
2005-11-20 Wim Taymans <wim@fluendo.com>
* check/Makefile.am:
* check/gst/gstsegment.c: (GST_START_TEST), (gstevents_suite),
(main):
* gst/Makefile.am:
* gst/gst.h:
* gst/gstsegment.c: (gst_segment_init), (gst_segment_set_duration),
(gst_segment_set_seek), (gst_segment_set_newsegment),
(gst_segment_to_stream_time), (gst_segment_to_running_time),
(gst_segment_clip):
* gst/gstsegment.h:
Added segment helper structure and methods. Not fully implemented
yet.
Added segment check.
2005-11-20 Jan Schmidt <thaytan@mad.scientist.com>
* check/gst/gstvalue.c: (GST_START_TEST), (gst_value_suite):

View file

@ -43,6 +43,7 @@ check_PROGRAMS = \
gst/gstpad \
gst/gstpipeline \
gst/gstplugin \
gst/gstsegment \
gst/gstsystemclock \
gst/gststructure \
gst/gsttag \

391
check/gst/gstsegment.c Normal file
View file

@ -0,0 +1,391 @@
/* GStreamer
* Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
*
* gstsegment.c: Unit test for segments
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <gst/check/gstcheck.h>
/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_nosize)
{
GstSegment segment;
gboolean res;
gint64 cstart, cstop;
gst_segment_init (&segment, GST_FORMAT_BYTES);
/* configure segment to start 100 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* configure segment to stop relative, should not do anything since
* size is unknown. */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* do some clipping on the open range */
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
100, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 200, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 200);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == -1);
/* add 100 to start, set stop to 300 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 300);
/* add 100 to start (to 300), set stop to 200, this is not allowed.
* nothing should be updated in the segment. */
ASSERT_CRITICAL (gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200));
fail_unless (segment.start == 200);
fail_unless (segment.stop == 300);
/* seek relative to end, should not do anything since size is
* unknown. */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 300);
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 250);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
200, 250, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 250);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
250, 290, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 250);
fail_unless (cstop == 290);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
250, 350, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 250);
fail_unless (cstop == 300);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 300);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 300);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 250);
fail_unless (cstop == 300);
/* start outside on boundary */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
fail_unless (res == FALSE);
/* start completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
fail_unless (res == FALSE);
}
GST_END_TEST;
/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_size)
{
GstSegment segment;
gboolean res;
gint64 cstart, cstop;
gst_segment_init (&segment, GST_FORMAT_BYTES);
gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
/* configure segment to start 100 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* configure segment to stop relative, does not update stop
* since we did not set it before. */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* do some clipping on the open range */
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
100, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 200, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 200);
/* partially inside, clip to size */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 300, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 200);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == -1);
/* add 100 to start, set stop to 300, stop clips to 200 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 200);
/* add 100 to start (to 300), set stop to 200, this clips start
* to duration */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 200);
/* seek relative to end */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20);
fail_unless (segment.start == 100);
fail_unless (segment.stop == 180);
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
100, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 170, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 170);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 250, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 180);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 180);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 180);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 180);
/* start outside on boundary */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
fail_unless (res == FALSE);
/* start completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
fail_unless (res == FALSE);
}
GST_END_TEST;
Suite *
gstevents_suite (void)
{
Suite *s = suite_create ("GstSegment");
TCase *tc_chain = tcase_create ("segments");
tcase_set_timeout (tc_chain, 20);
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, segment_seek_nosize);
tcase_add_test (tc_chain, segment_seek_size);
return s;
}
int
main (int argc, char **argv)
{
int nf;
Suite *s = gstevents_suite ();
SRunner *sr = srunner_create (s);
gst_check_init (&argc, &argv);
srunner_run_all (sr, CK_NORMAL);
nf = srunner_ntests_failed (sr);
srunner_free (sr);
return nf;
}

View file

@ -113,6 +113,7 @@ libgstreamer_@GST_MAJORMINOR@_la_SOURCES = \
gstqueue.c \
gstregistry.c \
gstregistryxml.c \
gstsegment.c \
gststructure.c \
gstsystemclock.c \
gsttaglist.c \
@ -193,6 +194,7 @@ gst_headers = \
gstpluginfeature.h \
gstquery.h \
gstqueue.h \
gstsegment.h \
gststructure.h \
gstsystemclock.h \
gsttaglist.h \

View file

@ -51,6 +51,7 @@
#include <gst/gstplugin.h>
#include <gst/gstquery.h>
#include <gst/gstregistry.h>
#include <gst/gstsegment.h>
#include <gst/gststructure.h>
#include <gst/gstsystemclock.h>
#include <gst/gsttaglist.h>

353
gst/gstsegment.c Normal file
View file

@ -0,0 +1,353 @@
/* GStreamer
* Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
*
* gstsegment.c: GstSegment subsystem
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "gst_private.h"
#include "gstutils.h"
#include "gstsegment.h"
/**
* SECTION:gstsegment
* @short_description: Structure describing the configured region of interest
* in a media file.
* @see_also: #GstEvent
*
* This helper structure holds the relevant values for tracking the region of
* interest in a media file, called a segment.
*
* Last reviewed on 2005-20-09 (0.9.5)
*/
/**
* gst_segment_init:
* @segment: a #GstSegment structure.
* @format: the format of the segment.
*
* Initialize @segment to its default values, which is a rate of 1.0, a
* start time of 0.
*/
void
gst_segment_init (GstSegment * segment, GstFormat format)
{
g_return_if_fail (segment != NULL);
segment->rate = 1.0;
segment->format = format;
segment->flags = 0;
segment->start = 0;
segment->stop = -1;
segment->time = 0;
segment->accum = 0;
segment->last_stop = 0;
segment->duration = -1;
}
/**
* gst_segment_set_duration:
* @segment: a #GstSegment structure.
* @format: the format of the segment.
* @duration: the duration of the segment info.
*
* Set the duration of the segment to @duration. This function is mainly
* used by elements that perform seeking and know the total duration of the
* segment.
*/
void
gst_segment_set_duration (GstSegment * segment, GstFormat format,
gint64 duration)
{
g_return_if_fail (segment != NULL);
g_return_if_fail (segment->format == format);
segment->duration = duration;
}
/**
* gst_segment_set_seek:
* @segment: a #GstSegment structure.
* @rate: the rate of the segment.
* @format: the format of the segment.
* @flags: the seek flags for the segment
* @cur_type: the seek method
* @cur: the seek start value
* @stop_type: the seek method
* @stop: the seek stop value
*
* Update the segment structure with the field values of a seek event.
*/
void
gst_segment_set_seek (GstSegment * segment, gdouble rate,
GstFormat format, GstSeekFlags flags,
GstSeekType cur_type, gint64 cur, GstSeekType stop_type, gint64 stop)
{
gboolean update_stop, update_start;
g_return_if_fail (rate != 0.0);
g_return_if_fail (segment != NULL);
g_return_if_fail (segment->format == format);
update_stop = update_start = TRUE;
/* start is never invalid */
switch (cur_type) {
case GST_SEEK_TYPE_NONE:
/* no update to segment */
cur = segment->start;
update_start = FALSE;
break;
case GST_SEEK_TYPE_SET:
/* cur holds desired position */
break;
case GST_SEEK_TYPE_CUR:
/* add cur to currently configure segment */
cur = segment->start + cur;
break;
case GST_SEEK_TYPE_END:
if (segment->duration != -1) {
/* add cur to total length */
cur = segment->duration + cur;
} else {
/* no update if duration unknown */
cur = segment->start;
update_start = FALSE;
}
break;
}
/* bring in sane range */
if (segment->duration != -1)
cur = CLAMP (cur, 0, segment->duration);
else
cur = MAX (cur, 0);
/* stop can be -1 if we have not configured a stop. */
switch (stop_type) {
case GST_SEEK_TYPE_NONE:
stop = segment->stop;
update_stop = FALSE;
break;
case GST_SEEK_TYPE_SET:
/* stop folds required value */
break;
case GST_SEEK_TYPE_CUR:
if (segment->stop != -1)
stop = segment->stop + stop;
else
stop = -1;
break;
case GST_SEEK_TYPE_END:
if (segment->duration != -1)
stop = segment->duration + stop;
else {
stop = segment->stop;
update_stop = FALSE;
}
break;
}
/* if we have a valid stop time, make sure it is clipped */
if (stop != -1) {
if (segment->duration != -1)
stop = CLAMP (stop, 0, segment->duration);
else
stop = MAX (stop, 0);
}
/* we can't have stop before start */
if (stop != -1)
g_return_if_fail (cur <= stop);
segment->rate = rate;
segment->abs_rate = ABS (rate);
segment->flags = flags;
segment->start = cur;
segment->stop = stop;
}
/**
* gst_segment_set_newsegment:
* @segment: a #GstSegment structure.
* @update: flag indicating a new segment is started or updated
* @rate: the rate of the segment.
* @format: the format of the segment.
* @start: the new start value
* @stop: the new stop value
* @time: the new stream time
*
* Update the segment structure with the field values of a new segment event.
*/
void
gst_segment_set_newsegment (GstSegment * segment, gboolean update, gdouble rate,
GstFormat format, gint64 start, gint64 stop, gint64 time)
{
gint64 duration;
g_return_if_fail (rate != 0.0);
g_return_if_fail (segment != NULL);
g_return_if_fail (segment->format == format);
if (update) {
/* an update to the current segment is done, elapsed time is
* difference between the old start and new start. */
duration = start - segment->start;
} else {
/* the new segment has to be aligned with the old segment.
* We first update the accumulated time of the previous
* segment. the accumulated time is used when syncing to the
* clock. A flush event sets the accumulated time back to 0
*/
if (GST_CLOCK_TIME_IS_VALID (segment->stop)) {
duration = segment->stop - segment->start;
} else if (GST_CLOCK_TIME_IS_VALID (segment->duration)) {
/* else use last seen timestamp as segment stop */
duration = segment->duration - segment->start;
} else {
/* else we don't know */
duration = 0;
}
}
/* use previous rate to calculate duration */
segment->accum += gst_gdouble_to_guint64 (
(gst_guint64_to_gdouble (duration) / segment->abs_rate));
/* then update the current segment */
segment->rate = rate;
segment->abs_rate = ABS (rate);
segment->start = start;
segment->stop = stop;
segment->time = time;
GST_DEBUG ("received NEWSEGMENT %" GST_TIME_FORMAT " -- %"
GST_TIME_FORMAT ", time %" GST_TIME_FORMAT ", accum %"
GST_TIME_FORMAT,
GST_TIME_ARGS (segment->start),
GST_TIME_ARGS (segment->stop),
GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->accum));
}
/**
* gst_segment_to_stream_time:
* @segment: a #GstSegment structure.
* @format: the format of the segment.
* @position: the position in the segment
*
* Translate @position to stream time using the currently configured
* segment.
*
* This function is typically used by elements that need to operate on
* the stream time of the buffers it receives, such as effect plugins.
*
* Returns: the position in stream_time.
*/
gint64
gst_segment_to_stream_time (GstSegment * segment, GstFormat format,
gint64 position)
{
gint64 result;
g_return_val_if_fail (segment != NULL, FALSE);
g_return_val_if_fail (segment->format == format, FALSE);
result = ((position - segment->start) / segment->abs_rate) + segment->time;
return result;
}
/**
* gst_segment_to_running_time:
* @segment: a #GstSegment structure.
* @format: the format of the segment.
* @position: the position in the segment
*
* Translate @position to the total running time using the currently configured
* segment.
*
* This function is typically used by elements that need to synchronize to the
* global clock in a pipeline.
*
* Returns: the position as the total running time.
*/
gint64
gst_segment_to_running_time (GstSegment * segment, GstFormat format,
gint64 position)
{
gint64 result;
g_return_val_if_fail (segment != NULL, FALSE);
g_return_val_if_fail (segment->format == format, FALSE);
result = ((position - segment->start) / segment->abs_rate) + segment->accum;
return result;
}
/**
* gst_segment_clip:
* @segment: a #GstSegment structure.
* @format: the format of the segment.
* @start: the start position in the segment
* @stop: the stop position in the segment
* @clip_start: the clipped start position in the segment
* @clip_stop: the clipped stop position in the segment
*
* Clip the given @start and @stop values to the segment boundaries given
* in @segment.
*
* Returns: TRUE if the given @start and @stop times fall partially in
* @segment, FALSE if the values are completely outside of the segment.
*/
gboolean
gst_segment_clip (GstSegment * segment, GstFormat format, gint64 start,
gint64 stop, gint64 * clip_start, gint64 * clip_stop)
{
g_return_val_if_fail (segment != NULL, FALSE);
g_return_val_if_fail (segment->format == format, FALSE);
/* we need a valid start position */
if (start == -1)
return FALSE;
/* if we have a stop position and start is bigger, we're
* outside of the segment */
if (segment->stop != -1 && start >= segment->stop)
return FALSE;
/* if a stop position is given and is before the segment start,
* we're outside of the segment */
if (stop != -1 && stop <= segment->start)
return FALSE;
if (clip_start)
*clip_start = MAX (start, segment->start);
if (clip_stop) {
if (stop == -1)
*clip_stop = segment->stop;
else if (segment->stop == -1)
*clip_stop = MAX (-1, stop);
else
*clip_stop = MIN (stop, segment->stop);
if (segment->duration != -1)
*clip_stop = MIN (*clip_stop, segment->duration);
}
return TRUE;
}

87
gst/gstsegment.h Normal file
View file

@ -0,0 +1,87 @@
/* GStreamer
* Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
*
* gstsegment.h: Header for GstSegment subsystem
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __GST_SEGMENT_H__
#define __GST_SEGMENT_H__
#include <gst/gstevent.h>
#include <gst/gstformat.h>
G_BEGIN_DECLS
typedef struct _GstSegment GstSegment;
/**
* GstSegment:
* @rate: the rate of the segment
* @abs_rate: absolute value of the rate
* @format: the format of the segment values
* @flags: flags for this segment
* @start: the start of the segment
* @stop: the stop of the segment
* @time: the stream time of the segment
* @accum: accumulated segment
* @last_stop: last known stop time
* @duration: total duration of segment
*
* A helper structure that holds the configured region of
* interest in a media file.
*/
struct _GstSegment {
/*< public >*/
gdouble rate;
gdouble abs_rate;
GstFormat format;
GstSeekFlags flags;
gint64 start;
gint64 stop;
gint64 time;
gint64 accum;
gint64 last_stop;
gint64 duration;
/*< private >*/
gpointer _gst_reserved[GST_PADDING];
};
void gst_segment_init (GstSegment *segment, GstFormat format);
void gst_segment_set_duration (GstSegment *segment, GstFormat format, gint64 duration);
void gst_segment_set_seek (GstSegment *segment, gdouble rate,
GstFormat format, GstSeekFlags flags,
GstSeekType cur_type, gint64 cur,
GstSeekType stop_type, gint64 stop);
void gst_segment_set_newsegment (GstSegment *segment, gboolean update, gdouble rate,
GstFormat format, gint64 start, gint64 stop, gint64 time);
gint64 gst_segment_to_stream_time (GstSegment *segment, GstFormat format, gint64 position);
gint64 gst_segment_to_running_time (GstSegment *segment, GstFormat format, gint64 position);
gboolean gst_segment_clip (GstSegment *segment, GstFormat format, gint64 start,
gint64 stop, gint64 *clip_start, gint64 *clip_stop);
G_END_DECLS
#endif /* __GST_SEGMENT_H__ */

View file

@ -43,6 +43,7 @@ check_PROGRAMS = \
gst/gstpad \
gst/gstpipeline \
gst/gstplugin \
gst/gstsegment \
gst/gstsystemclock \
gst/gststructure \
gst/gsttag \

View file

@ -0,0 +1,391 @@
/* GStreamer
* Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
*
* gstsegment.c: Unit test for segments
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <gst/check/gstcheck.h>
/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_nosize)
{
GstSegment segment;
gboolean res;
gint64 cstart, cstop;
gst_segment_init (&segment, GST_FORMAT_BYTES);
/* configure segment to start 100 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* configure segment to stop relative, should not do anything since
* size is unknown. */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* do some clipping on the open range */
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
100, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 200, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 200);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == -1);
/* add 100 to start, set stop to 300 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 300);
/* add 100 to start (to 300), set stop to 200, this is not allowed.
* nothing should be updated in the segment. */
ASSERT_CRITICAL (gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200));
fail_unless (segment.start == 200);
fail_unless (segment.stop == 300);
/* seek relative to end, should not do anything since size is
* unknown. */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_END, -300, GST_SEEK_TYPE_END, -100);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 300);
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 200, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 250, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 250);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
200, 250, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 250);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
250, 290, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 250);
fail_unless (cstop == 290);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
250, 350, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 250);
fail_unless (cstop == 300);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 300);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 200, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 200);
fail_unless (cstop == 300);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 250);
fail_unless (cstop == 300);
/* start outside on boundary */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 300, -1, &cstart, &cstop);
fail_unless (res == FALSE);
/* start completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 350, -1, &cstart, &cstop);
fail_unless (res == FALSE);
}
GST_END_TEST;
/* mess with the segment structure in the bytes format */
GST_START_TEST (segment_seek_size)
{
GstSegment segment;
gboolean res;
gint64 cstart, cstop;
gst_segment_init (&segment, GST_FORMAT_BYTES);
gst_segment_set_duration (&segment, GST_FORMAT_BYTES, 200);
/* configure segment to start 100 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_SET, 100, GST_SEEK_TYPE_NONE, -1);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* configure segment to stop relative, does not update stop
* since we did not set it before. */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_NONE, 200, GST_SEEK_TYPE_CUR, -100);
fail_unless (segment.start == 100);
fail_unless (segment.stop == -1);
/* do some clipping on the open range */
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
100, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 200, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 200);
/* partially inside, clip to size */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 300, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 200);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == -1);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == -1);
/* add 100 to start, set stop to 300, stop clips to 200 */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 300);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 200);
/* add 100 to start (to 300), set stop to 200, this clips start
* to duration */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_CUR, 100, GST_SEEK_TYPE_SET, 200);
fail_unless (segment.start == 200);
fail_unless (segment.stop == 200);
/* seek relative to end */
gst_segment_set_seek (&segment, 1.0,
GST_FORMAT_BYTES,
GST_SEEK_FLAG_NONE, GST_SEEK_TYPE_END, -100, GST_SEEK_TYPE_END, -20);
fail_unless (segment.start == 100);
fail_unless (segment.stop == 180);
/* completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 0, 50, &cstart, &cstop);
fail_unless (res == FALSE);
/* touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* inside, touching lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
100, 150, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 150);
/* completely inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 170, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 170);
/* partially inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES,
150, 250, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 180);
/* invalid start */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, -1, 100, &cstart, &cstop);
fail_unless (res == FALSE);
/* start outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 50, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 180);
/* start on lower bound */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 100, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 100);
fail_unless (cstop == 180);
/* start inside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 150, -1, &cstart, &cstop);
fail_unless (res == TRUE);
fail_unless (cstart == 150);
fail_unless (cstop == 180);
/* start outside on boundary */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 180, -1, &cstart, &cstop);
fail_unless (res == FALSE);
/* start completely outside */
res = gst_segment_clip (&segment, GST_FORMAT_BYTES, 250, -1, &cstart, &cstop);
fail_unless (res == FALSE);
}
GST_END_TEST;
Suite *
gstevents_suite (void)
{
Suite *s = suite_create ("GstSegment");
TCase *tc_chain = tcase_create ("segments");
tcase_set_timeout (tc_chain, 20);
suite_add_tcase (s, tc_chain);
tcase_add_test (tc_chain, segment_seek_nosize);
tcase_add_test (tc_chain, segment_seek_size);
return s;
}
int
main (int argc, char **argv)
{
int nf;
Suite *s = gstevents_suite ();
SRunner *sr = srunner_create (s);
gst_check_init (&argc, &argv);
srunner_run_all (sr, CK_NORMAL);
nf = srunner_ntests_failed (sr);
srunner_free (sr);
return nf;
}