sfdec: add query implementation for position and duration

This commit is contained in:
Stefan Sauer 2013-12-17 22:25:03 +01:00
parent 5f89bee749
commit b884eac02c
2 changed files with 61 additions and 12 deletions

View file

@ -48,6 +48,8 @@ GST_DEBUG_CATEGORY_STATIC (gst_sf_dec_debug);
#define DEFAULT_BUFFER_FRAMES (256) #define DEFAULT_BUFFER_FRAMES (256)
static gboolean gst_sf_dec_src_query (GstPad * pad, GstObject * parent,
GstQuery * query);
static GstStateChangeReturn gst_sf_dec_change_state (GstElement * element, static GstStateChangeReturn gst_sf_dec_change_state (GstElement * element,
GstStateChange transition); GstStateChange transition);
@ -69,13 +71,13 @@ G_DEFINE_TYPE_WITH_CODE (GstSFDec, gst_sf_dec, GST_TYPE_ELEMENT, _do_init);
static sf_count_t static sf_count_t
gst_sf_vio_get_filelen (void *user_data) gst_sf_vio_get_filelen (void *user_data)
{ {
GstElement *element = GST_ELEMENT (user_data); GstSFDec *self = GST_SF_DEC (user_data);
gint64 dur; gint64 dur;
if (gst_element_query_duration (element, GST_FORMAT_BYTES, &dur)) { if (gst_pad_peer_query_duration (self->sinkpad, GST_FORMAT_BYTES, &dur)) {
return (sf_count_t) dur; return (sf_count_t) dur;
} }
GST_WARNING_OBJECT (element, "query_duration failed"); GST_WARNING_OBJECT (self, "query_duration failed");
return -1; return -1;
} }
@ -179,16 +181,62 @@ gst_sf_dec_init (GstSFDec * self)
gst_element_add_pad (GST_ELEMENT (self), self->sinkpad); gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
self->srcpad = gst_pad_new_from_static_template (&sf_dec_src_factory, "src"); self->srcpad = gst_pad_new_from_static_template (&sf_dec_src_factory, "src");
/* TODO(ensonic): event + query functions */ /* TODO(ensonic): event function */
gst_pad_set_query_function (self->srcpad,
GST_DEBUG_FUNCPTR (gst_sf_dec_src_query));
gst_element_add_pad (GST_ELEMENT (self), self->srcpad); gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
} }
static gboolean
gst_sf_dec_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
{
GstSFDec *self = GST_SF_DEC (parent);
GstFormat format;
gboolean res = FALSE;
GST_DEBUG_OBJECT (self, "query %s", GST_QUERY_TYPE_NAME (query));
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_DURATION:
if (!self->file)
goto done;
gst_query_parse_duration (query, &format, NULL);
if (format == GST_FORMAT_TIME) {
gst_query_set_duration (query, format,
gst_util_uint64_scale_int (self->duration, GST_SECOND, self->rate));
res = TRUE;
}
break;
case GST_QUERY_POSITION:
if (!self->file)
goto done;
gst_query_parse_position (query, &format, NULL);
if (format == GST_FORMAT_TIME) {
gst_query_set_position (query, format,
gst_util_uint64_scale_int (self->pos, GST_SECOND, self->rate));
res = TRUE;
}
break;
default:
res = gst_pad_query_default (pad, parent, query);
break;
}
done:
GST_DEBUG_OBJECT (self, "query %s: %d", GST_QUERY_TYPE_NAME (query), res);
return res;
}
static GstStateChangeReturn static GstStateChangeReturn
gst_sf_dec_change_state (GstElement * element, GstStateChange transition) gst_sf_dec_change_state (GstElement * element, GstStateChange transition)
{ {
GstStateChangeReturn ret; GstStateChangeReturn ret;
GstSFDec *self = GST_SF_DEC (element); GstSFDec *self = GST_SF_DEC (element);
GST_INFO_OBJECT (self, "transition: %s -> %s",
gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
switch (transition) { switch (transition) {
case GST_STATE_CHANGE_READY_TO_PAUSED: case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_sf_dec_start (self); gst_sf_dec_start (self);
@ -212,8 +260,6 @@ gst_sf_dec_change_state (GstElement * element, GstStateChange transition)
static gboolean static gboolean
gst_sf_dec_start (GstSFDec * self) gst_sf_dec_start (GstSFDec * self)
{ {
self->file = NULL;
return TRUE; return TRUE;
} }
@ -234,6 +280,9 @@ gst_sf_dec_stop (GstSFDec * self)
self->channels = 0; self->channels = 0;
self->rate = 0; self->rate = 0;
self->pos = 0;
self->duration = 0;
return TRUE; return TRUE;
close_failed: close_failed:
@ -288,6 +337,7 @@ gst_sf_dec_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
case GST_PAD_MODE_PULL: case GST_PAD_MODE_PULL:
if (active) { if (active) {
/* if we have a scheduler we can start the task */ /* if we have a scheduler we can start the task */
GST_DEBUG_OBJECT (sinkpad, "start task");
res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_sf_dec_loop, res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_sf_dec_loop,
sinkpad, NULL); sinkpad, NULL);
} else { } else {
@ -304,7 +354,7 @@ gst_sf_dec_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
static gboolean static gboolean
gst_sf_dec_open_file (GstSFDec * self) gst_sf_dec_open_file (GstSFDec * self)
{ {
SF_INFO info; SF_INFO info = { 0, };
GstCaps *caps; GstCaps *caps;
GstStructure *s; GstStructure *s;
GstSegment seg; GstSegment seg;
@ -313,9 +363,7 @@ gst_sf_dec_open_file (GstSFDec * self)
gchar *stream_id; gchar *stream_id;
GST_DEBUG_OBJECT (self, "opening the stream"); GST_DEBUG_OBJECT (self, "opening the stream");
info.format = 0; if (!(self->file = sf_open_virtual (&gst_sf_vio, SFM_READ, &info, self)))
self->file = sf_open_virtual (&gst_sf_vio, SFM_READ, &info, self);
if (!self->file)
goto open_failed; goto open_failed;
stream_id = stream_id =
@ -325,8 +373,8 @@ gst_sf_dec_open_file (GstSFDec * self)
self->channels = info.channels; self->channels = info.channels;
self->rate = info.samplerate; self->rate = info.samplerate;
self->duration = info.frames;
/* TODO(ensonic): do something with info.seekable? */ /* TODO(ensonic): do something with info.seekable? */
/* TODO(ensonic): calculate duration info.frames */
/* negotiate srcpad caps */ /* negotiate srcpad caps */
if ((caps = gst_pad_get_allowed_caps (self->srcpad)) == NULL) { if ((caps = gst_pad_get_allowed_caps (self->srcpad)) == NULL) {
@ -366,7 +414,7 @@ gst_sf_dec_open_file (GstSFDec * self)
gst_caps_unref (caps); gst_caps_unref (caps);
gst_segment_init (&seg, GST_FORMAT_TIME); gst_segment_init (&seg, GST_FORMAT_TIME);
/* TODO: calculate seg.stop = song_length; */ seg.stop = gst_util_uint64_scale_int (self->duration, GST_SECOND, self->rate);
gst_pad_push_event (self->srcpad, gst_event_new_segment (&seg)); gst_pad_push_event (self->srcpad, gst_event_new_segment (&seg));
return TRUE; return TRUE;

View file

@ -52,6 +52,7 @@ struct _GstSFDec {
GstPad *srcpad; GstPad *srcpad;
guint64 pos; guint64 pos;
guint64 duration;
SNDFILE *file; SNDFILE *file;
sf_count_t offset; sf_count_t offset;