gst/videorate/gstvideorate.c: Port videorate, do a better job at negotiation while we're at it.

Original commit message from CVS:
* gst/videorate/gstvideorate.c: (gst_videorate_class_init),
(gst_videorate_getcaps), (gst_videorate_setcaps),
(gst_videorate_blank_data), (gst_videorate_init),
(gst_videorate_event), (gst_videorate_chain),
(gst_videorate_change_state):
Port videorate, do a better job at negotiation while we're at
it.
This commit is contained in:
Wim Taymans 2005-05-09 14:03:20 +00:00
parent 8a124c2c66
commit fe62a9caa2
2 changed files with 182 additions and 102 deletions

View file

@ -1,3 +1,13 @@
2005-05-09 Wim Taymans <wim@fluendo.com>
* gst/videorate/gstvideorate.c: (gst_videorate_class_init),
(gst_videorate_getcaps), (gst_videorate_setcaps),
(gst_videorate_blank_data), (gst_videorate_init),
(gst_videorate_event), (gst_videorate_chain),
(gst_videorate_change_state):
Port videorate, do a better job at negotiation while we're at
it.
2005-05-09 Jan Schmidt <thaytan@mad.scientist.com> 2005-05-09 Jan Schmidt <thaytan@mad.scientist.com>
* configure.ac: * configure.ac:

View file

@ -110,7 +110,8 @@ static GstStaticPadTemplate gst_videorate_sink_template =
static void gst_videorate_base_init (gpointer g_class); static void gst_videorate_base_init (gpointer g_class);
static void gst_videorate_class_init (GstVideorateClass * klass); static void gst_videorate_class_init (GstVideorateClass * klass);
static void gst_videorate_init (GstVideorate * videorate); static void gst_videorate_init (GstVideorate * videorate);
static void gst_videorate_chain (GstPad * pad, GstData * _data); static gboolean gst_videorate_event (GstPad * pad, GstEvent * event);
static GstFlowReturn gst_videorate_chain (GstPad * pad, GstBuffer * buffer);
static void gst_videorate_set_property (GObject * object, static void gst_videorate_set_property (GObject * object,
guint prop_id, const GValue * value, GParamSpec * pspec); guint prop_id, const GValue * value, GParamSpec * pspec);
@ -168,6 +169,9 @@ gst_videorate_class_init (GstVideorateClass * klass)
parent_class = g_type_class_peek_parent (klass); parent_class = g_type_class_peek_parent (klass);
object_class->set_property = gst_videorate_set_property;
object_class->get_property = gst_videorate_get_property;
g_object_class_install_property (object_class, ARG_IN, g_object_class_install_property (object_class, ARG_IN,
g_param_spec_uint64 ("in", "In", g_param_spec_uint64 ("in", "In",
"Number of input frames", 0, G_MAXUINT64, 0, G_PARAM_READABLE)); "Number of input frames", 0, G_MAXUINT64, 0, G_PARAM_READABLE));
@ -189,9 +193,6 @@ gst_videorate_class_init (GstVideorateClass * klass)
"Value indicating how much to prefer new frames", "Value indicating how much to prefer new frames",
0.0, 1.0, DEFAULT_NEW_PREF, G_PARAM_READWRITE)); 0.0, 1.0, DEFAULT_NEW_PREF, G_PARAM_READWRITE));
object_class->set_property = gst_videorate_set_property;
object_class->get_property = gst_videorate_get_property;
element_class->change_state = gst_videorate_change_state; element_class->change_state = gst_videorate_change_state;
} }
@ -200,90 +201,121 @@ gst_videorate_getcaps (GstPad * pad)
{ {
GstVideorate *videorate; GstVideorate *videorate;
GstPad *otherpad; GstPad *otherpad;
GstCaps *caps, *copy, *copy2 = NULL; GstCaps *caps;
int i;
gdouble otherfps;
GstStructure *structure;
gboolean negotiated;
videorate = GST_VIDEORATE (gst_pad_get_parent (pad)); videorate = GST_VIDEORATE (GST_PAD_PARENT (pad));
otherpad = (pad == videorate->srcpad) ? videorate->sinkpad : otherpad = (pad == videorate->srcpad) ? videorate->sinkpad :
videorate->srcpad; videorate->srcpad;
negotiated = gst_pad_is_negotiated (otherpad);
otherfps = (pad == videorate->srcpad) ? videorate->from_fps :
videorate->to_fps;
caps = gst_pad_get_allowed_caps (otherpad); /* we can do what the peer can */
copy = gst_caps_copy (caps); caps = gst_pad_peer_get_caps (otherpad);
if (negotiated) { if (caps) {
copy2 = gst_caps_copy (caps); int i;
} GstCaps *intersect;
for (i = 0; i < gst_caps_get_size (caps); i++) {
structure = gst_caps_get_structure (caps, i);
gst_structure_set (structure, /* filter against our caps */
"framerate", GST_TYPE_DOUBLE_RANGE, 0.0, G_MAXDOUBLE, NULL); intersect = gst_caps_intersect (caps, gst_pad_get_pad_template_caps (pad));
} gst_caps_unref (caps);
if ((negotiated)) { caps = intersect;
for (i = 0; i < gst_caps_get_size (copy2); i++) {
structure = gst_caps_get_structure (copy2, i);
gst_structure_set (structure, "framerate", G_TYPE_DOUBLE, otherfps, NULL); caps = gst_caps_make_writable (caps);
/* all possible framerates are allowed */
for (i = 0; i < gst_caps_get_size (caps); i++) {
GstStructure *structure;
structure = gst_caps_get_structure (caps, i);
gst_structure_set (structure,
"framerate", GST_TYPE_DOUBLE_RANGE, 0.0, G_MAXDOUBLE, NULL);
} }
gst_caps_append (copy2, copy); } else {
copy = copy2; /* no peer, our padtemplate is enough then */
caps = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
} }
gst_caps_append (copy, caps);
return copy; return caps;
} }
static GstPadLinkReturn static gboolean
gst_videorate_link (GstPad * pad, const GstCaps * caps) gst_videorate_setcaps (GstPad * pad, GstCaps * caps)
{ {
GstVideorate *videorate; GstVideorate *videorate;
GstStructure *structure; GstStructure *structure;
gboolean ret; gboolean ret = TRUE;
double fps; double fps;
GstPad *otherpad; GstPad *otherpad, *opeer;
videorate = GST_VIDEORATE (gst_pad_get_parent (pad)); videorate = GST_VIDEORATE (GST_PAD_PARENT (pad));
otherpad = (pad == videorate->srcpad) ? videorate->sinkpad :
videorate->srcpad;
structure = gst_caps_get_structure (caps, 0); structure = gst_caps_get_structure (caps, 0);
ret = gst_structure_get_double (structure, "framerate", &fps); if (!(ret = gst_structure_get_double (structure, "framerate", &fps)))
if (!ret) goto done;
return GST_PAD_LINK_REFUSED;
if (pad == videorate->srcpad) { if (pad == videorate->srcpad) {
videorate->to_fps = fps; videorate->to_fps = fps;
otherpad = videorate->sinkpad;
} else { } else {
videorate->from_fps = fps; videorate->from_fps = fps;
otherpad = videorate->srcpad;
} }
/* now try to find something for the peer */
opeer = gst_pad_get_peer (otherpad);
if (opeer) {
if (gst_pad_accept_caps (opeer, caps)) {
/* the peer accepts the caps as they are */
gst_pad_set_caps (otherpad, caps);
if (gst_pad_is_negotiated (otherpad)) { ret = TRUE;
/*
* Ensure that the other side talks the format we're trying to set
*/
GstCaps *newcaps = gst_caps_copy (caps);
if (pad == videorate->srcpad) {
gst_caps_set_simple (newcaps,
"framerate", G_TYPE_DOUBLE, videorate->from_fps, NULL);
} else { } else {
gst_caps_set_simple (newcaps, GstCaps *peercaps;
"framerate", G_TYPE_DOUBLE, videorate->to_fps, NULL); GstCaps *intersect;
} gint i;
ret = gst_pad_try_set_caps (otherpad, newcaps);
if (GST_PAD_LINK_FAILED (ret)) {
return GST_PAD_LINK_REFUSED;
}
}
return GST_PAD_LINK_OK; /* the peer does not accept this caps, see what it can do */
peercaps = gst_pad_get_caps (opeer);
caps = gst_caps_make_writable (caps);
structure = gst_caps_get_structure (caps, 0);
/* remove the framerate */
gst_structure_remove_field (structure, "framerate");
/* get all possibilities without framerate */
intersect = gst_caps_intersect (peercaps, caps);
gst_caps_unref (peercaps);
if (gst_caps_is_empty (intersect)) {
/* nothing possible, we're done */
gst_caps_unref (intersect);
ret = FALSE;
goto done;
}
/* loop through the caps, fixate the framerate */
for (i = 0; i < gst_caps_get_size (intersect); i++) {
GstStructure *istruct;
istruct = gst_caps_get_structure (intersect, i);
if (gst_caps_structure_fixate_field_nearest_int (istruct, "framerate",
fps)) {
gst_structure_get_double (structure, "framerate", &fps);
if (otherpad == videorate->srcpad) {
videorate->to_fps = fps;
} else {
videorate->from_fps = fps;
}
gst_pad_set_caps (otherpad, intersect);
break;
}
}
gst_caps_unref (intersect);
}
gst_object_unref (GST_OBJECT (opeer));
}
done:
return ret;
} }
static void static void
@ -293,6 +325,9 @@ gst_videorate_blank_data (GstVideorate * videorate)
if (videorate->prevbuf) if (videorate->prevbuf)
gst_buffer_unref (videorate->prevbuf); gst_buffer_unref (videorate->prevbuf);
videorate->prevbuf = NULL; videorate->prevbuf = NULL;
videorate->from_fps = 0;
videorate->to_fps = 0;
videorate->in = 0; videorate->in = 0;
videorate->out = 0; videorate->out = 0;
videorate->drop = 0; videorate->drop = 0;
@ -304,63 +339,83 @@ gst_videorate_blank_data (GstVideorate * videorate)
static void static void
gst_videorate_init (GstVideorate * videorate) gst_videorate_init (GstVideorate * videorate)
{ {
GST_FLAG_SET (videorate, GST_ELEMENT_EVENT_AWARE);
GST_DEBUG ("gst_videorate_init"); GST_DEBUG ("gst_videorate_init");
videorate->sinkpad = videorate->sinkpad =
gst_pad_new_from_template (gst_static_pad_template_get gst_pad_new_from_template (gst_static_pad_template_get
(&gst_videorate_sink_template), "sink"); (&gst_videorate_sink_template), "sink");
gst_element_add_pad (GST_ELEMENT (videorate), videorate->sinkpad); gst_element_add_pad (GST_ELEMENT (videorate), videorate->sinkpad);
gst_pad_set_event_function (videorate->sinkpad, gst_videorate_event);
gst_pad_set_chain_function (videorate->sinkpad, gst_videorate_chain); gst_pad_set_chain_function (videorate->sinkpad, gst_videorate_chain);
gst_pad_set_getcaps_function (videorate->sinkpad, gst_videorate_getcaps); gst_pad_set_getcaps_function (videorate->sinkpad, gst_videorate_getcaps);
gst_pad_set_link_function (videorate->sinkpad, gst_videorate_link); gst_pad_set_setcaps_function (videorate->sinkpad, gst_videorate_setcaps);
videorate->srcpad = videorate->srcpad =
gst_pad_new_from_template (gst_static_pad_template_get gst_pad_new_from_template (gst_static_pad_template_get
(&gst_videorate_src_template), "src"); (&gst_videorate_src_template), "src");
gst_element_add_pad (GST_ELEMENT (videorate), videorate->srcpad); gst_element_add_pad (GST_ELEMENT (videorate), videorate->srcpad);
gst_pad_set_getcaps_function (videorate->srcpad, gst_videorate_getcaps); gst_pad_set_getcaps_function (videorate->srcpad, gst_videorate_getcaps);
gst_pad_set_link_function (videorate->srcpad, gst_videorate_link); gst_pad_set_setcaps_function (videorate->srcpad, gst_videorate_setcaps);
gst_videorate_blank_data (videorate); gst_videorate_blank_data (videorate);
videorate->silent = DEFAULT_SILENT; videorate->silent = DEFAULT_SILENT;
videorate->new_pref = DEFAULT_NEW_PREF; videorate->new_pref = DEFAULT_NEW_PREF;
} }
static void static GstFlowReturn
gst_videorate_chain (GstPad * pad, GstData * data) gst_videorate_event (GstPad * pad, GstEvent * event)
{ {
GstVideorate *videorate = GST_VIDEORATE (gst_pad_get_parent (pad)); GstVideorate *videorate;
GstBuffer *buf;
if (GST_IS_EVENT (data)) { videorate = GST_VIDEORATE (GST_PAD_PARENT (pad));
GstEvent *event = GST_EVENT (data);
if (!videorate->prevbuf) { if (!videorate->prevbuf)
gst_pad_event_default (pad, event); goto done;
return;
}
if (GST_EVENT_TYPE (event) == GST_EVENT_DISCONTINUOUS) {
gint64 etime;
if (!(gst_event_discont_get_value (event, GST_FORMAT_TIME, &etime))) switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_DISCONTINUOUS:
{
gint64 start, end;
GST_STREAM_LOCK (pad);
if (!(gst_event_discont_get_value (event, GST_FORMAT_TIME, &start, &end)))
GST_WARNING ("Got discont but doesn't have GST_FORMAT_TIME value"); GST_WARNING ("Got discont but doesn't have GST_FORMAT_TIME value");
else { else {
gst_videorate_blank_data (videorate); gst_videorate_blank_data (videorate);
videorate->first_ts = etime; videorate->first_ts = start;
} }
gst_pad_event_default (pad, event); GST_STREAM_UNLOCK (pad);
return;
}
}
buf = GST_BUFFER (data); break;
}
default:
break;
}
done:
return gst_pad_event_default (pad, event);
}
static GstFlowReturn
gst_videorate_chain (GstPad * pad, GstBuffer * buffer)
{
GstVideorate *videorate;
GstFlowReturn res = GST_FLOW_OK;
videorate = GST_VIDEORATE (GST_PAD_PARENT (pad));
if (videorate->from_fps == 0)
return GST_FLOW_NOT_NEGOTIATED;
if (videorate->to_fps == 0)
return GST_FLOW_NOT_NEGOTIATED;
GST_STREAM_LOCK (pad);
/* pull in 2 buffers */ /* pull in 2 buffers */
if (videorate->prevbuf == NULL) { if (videorate->prevbuf == NULL) {
/* We're sure it's a GstBuffer here */ /* We're sure it's a GstBuffer here */
videorate->prevbuf = buf; videorate->prevbuf = buffer;
videorate->next_ts = videorate->first_ts = GST_BUFFER_TIMESTAMP (buf); videorate->next_ts = videorate->first_ts = GST_BUFFER_TIMESTAMP (buffer);
} else { } else {
GstClockTime prevtime, intime; GstClockTime prevtime, intime;
gint count = 0; gint count = 0;
@ -368,17 +423,19 @@ gst_videorate_chain (GstPad * pad, GstData * data)
prevtime = GST_BUFFER_TIMESTAMP (videorate->prevbuf); prevtime = GST_BUFFER_TIMESTAMP (videorate->prevbuf);
/* If it's a GstEvent, we give him the time of the next outputed frame */ /* If it's a GstEvent, we give him the time of the next outputed frame */
#if 0
intime = (GST_IS_EVENT (data)) intime = (GST_IS_EVENT (data))
? videorate->next_ts + ? videorate->next_ts +
(1 / videorate->to_fps * GST_SECOND) : GST_BUFFER_TIMESTAMP (buf); (1 / videorate->to_fps * GST_SECOND) : GST_BUFFER_TIMESTAMP (buffer);
#endif
intime = GST_BUFFER_TIMESTAMP (buffer);
GST_LOG_OBJECT (videorate, GST_LOG_OBJECT (videorate,
"prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT "prev buf %" GST_TIME_FORMAT " new buf %" GST_TIME_FORMAT
" outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime), " outgoing ts %" GST_TIME_FORMAT, GST_TIME_ARGS (prevtime),
GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts)); GST_TIME_ARGS (intime), GST_TIME_ARGS (videorate->next_ts));
if (GST_IS_BUFFER (data)) videorate->in++;
videorate->in++;
/* got 2 buffers, see which one is the best */ /* got 2 buffers, see which one is the best */
do { do {
@ -412,7 +469,10 @@ gst_videorate_chain (GstPad * pad, GstData * data)
(videorate->out / videorate->to_fps * GST_SECOND); (videorate->out / videorate->to_fps * GST_SECOND);
GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (outbuf) =
videorate->next_ts - GST_BUFFER_TIMESTAMP (outbuf); videorate->next_ts - GST_BUFFER_TIMESTAMP (outbuf);
gst_pad_push (videorate->srcpad, GST_DATA (outbuf)); gst_buffer_set_caps (outbuf, GST_PAD_CAPS (videorate->srcpad));
if ((res = gst_pad_push (videorate->srcpad, outbuf)) != GST_FLOW_OK)
goto done;
GST_LOG_OBJECT (videorate, GST_LOG_OBJECT (videorate,
"old is best, dup, outgoing ts %" GST_TIME_FORMAT, "old is best, dup, outgoing ts %" GST_TIME_FORMAT,
@ -444,13 +504,14 @@ gst_videorate_chain (GstPad * pad, GstData * data)
GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts), GST_TIME_ARGS (diff2), GST_TIME_ARGS (videorate->next_ts),
videorate->in, videorate->out, videorate->drop, videorate->dup); videorate->in, videorate->out, videorate->drop, videorate->dup);
if (GST_IS_BUFFER (data)) { /* swap in new one when it's the best */
/* swap in new one when it's the best */ gst_buffer_unref (videorate->prevbuf);
gst_buffer_unref (videorate->prevbuf); videorate->prevbuf = buffer;
videorate->prevbuf = buf;
} else
gst_pad_event_default (pad, GST_EVENT (data));
} }
done:
GST_STREAM_UNLOCK (pad);
return res;
} }
static void static void
@ -506,20 +567,29 @@ gst_videorate_get_property (GObject * object,
static GstElementStateReturn static GstElementStateReturn
gst_videorate_change_state (GstElement * element) gst_videorate_change_state (GstElement * element)
{ {
//GstVideorate *videorate = GST_VIDEORATE (element); GstElementStateReturn ret;
GstVideorate *videorate;
gint transition;
switch (GST_STATE_TRANSITION (element)) { videorate = GST_VIDEORATE (element);
transition = GST_STATE_TRANSITION (videorate);
switch (transition) {
default:
break;
}
ret = parent_class->change_state (element);
switch (transition) {
case GST_STATE_PAUSED_TO_READY: case GST_STATE_PAUSED_TO_READY:
gst_videorate_blank_data (GST_VIDEORATE (element)); gst_videorate_blank_data (videorate);
break; break;
default: default:
break; break;
} }
if (parent_class->change_state) return ret;
return parent_class->change_state (element);
return GST_STATE_SUCCESS;
} }
static gboolean static gboolean