ximagesink: Don't share internal pool

Sharing the internal pool results in situation where the pool may have
two upstream owners. This create a race upon deactivation. Instead,
always offer a new pool, and keep the internal pool internal in case
we absolutely need it.

https://bugzilla.gnome.org/show_bug.cgi?id=748344
This commit is contained in:
Nicolas Dufresne 2015-06-05 14:28:41 -04:00 committed by Nicolas Dufresne
parent dc7b254805
commit ae20ba7ad4

View file

@ -1093,6 +1093,34 @@ gst_ximagesink_getcaps (GstBaseSink * bsink, GstCaps * filter)
return caps; return caps;
} }
static GstBufferPool *
gst_ximagesink_create_pool (GstXImageSink * ximagesink, GstCaps * caps,
gsize size, gint min)
{
static GstAllocationParams params = { 0, 15, 0, 0, };
GstBufferPool *pool;
GstStructure *config;
/* create a new pool for the new configuration */
pool = gst_ximage_buffer_pool_new (ximagesink);
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_set_params (config, caps, size, min, 0);
gst_buffer_pool_config_set_allocator (config, NULL, &params);
if (!gst_buffer_pool_set_config (pool, config))
goto config_failed;
return pool;
config_failed:
{
GST_WARNING_OBJECT (ximagesink, "failed setting config");
gst_object_unref (pool);
return NULL;
}
}
static gboolean static gboolean
gst_ximagesink_setcaps (GstBaseSink * bsink, GstCaps * caps) gst_ximagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
{ {
@ -1101,8 +1129,6 @@ gst_ximagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
GstVideoInfo info; GstVideoInfo info;
GstBufferPool *newpool, *oldpool; GstBufferPool *newpool, *oldpool;
const GValue *par; const GValue *par;
gint size;
static GstAllocationParams params = { 0, 15, 0, 0, };
ximagesink = GST_XIMAGESINK (bsink); ximagesink = GST_XIMAGESINK (bsink);
@ -1120,8 +1146,6 @@ gst_ximagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
if (!gst_video_info_from_caps (&info, caps)) if (!gst_video_info_from_caps (&info, caps))
goto invalid_format; goto invalid_format;
size = info.size;
structure = gst_caps_get_structure (caps, 0); structure = gst_caps_get_structure (caps, 0);
/* if the caps contain pixel-aspect-ratio, they have to match ours, /* if the caps contain pixel-aspect-ratio, they have to match ours,
* otherwise linking should fail */ * otherwise linking should fail */
@ -1168,26 +1192,17 @@ gst_ximagesink_setcaps (GstBaseSink * bsink, GstCaps * caps)
/* Remember to draw borders for next frame */ /* Remember to draw borders for next frame */
ximagesink->draw_border = TRUE; ximagesink->draw_border = TRUE;
/* create a new pool for the new configuration */ /* create a new internal pool for the new configuration */
newpool = gst_ximage_buffer_pool_new (ximagesink); newpool = gst_ximagesink_create_pool (ximagesink, caps, info.size, 2);
structure = gst_buffer_pool_get_config (newpool);
gst_buffer_pool_config_set_params (structure, caps, size, 2, 0);
gst_buffer_pool_config_set_allocator (structure, NULL, &params);
if (!gst_buffer_pool_set_config (newpool, structure))
goto config_failed;
/* we don't activate the internal pool yet as it may not be needed */
oldpool = ximagesink->pool; oldpool = ximagesink->pool;
/* we don't activate the pool yet, this will be done by downstream after it
* has configured the pool. If downstream does not want our pool we will
* activate it when we render into it */
ximagesink->pool = newpool; ximagesink->pool = newpool;
g_mutex_unlock (&ximagesink->flow_lock); g_mutex_unlock (&ximagesink->flow_lock);
/* unref the old sink */ /* deactivate and unref the old internal pool */
if (oldpool) { if (oldpool) {
/* we don't deactivate, some elements might still be using it, it will be gst_buffer_pool_set_active (oldpool, FALSE);
* deactivated when the last ref is gone */
gst_object_unref (oldpool); gst_object_unref (oldpool);
} }
@ -1215,12 +1230,6 @@ invalid_size:
("Invalid image size.")); ("Invalid image size."));
return FALSE; return FALSE;
} }
config_failed:
{
GST_ERROR_OBJECT (ximagesink, "failed to set config.");
g_mutex_unlock (&ximagesink->flow_lock);
return FALSE;
}
} }
static GstStateChangeReturn static GstStateChangeReturn
@ -1342,8 +1351,8 @@ gst_ximagesink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
/* if we have one... */ /* if we have one... */
GST_LOG_OBJECT (ximagesink, "buffer not from our pool, copying"); GST_LOG_OBJECT (ximagesink, "buffer not from our pool, copying");
/* we should have a pool, configured in setcaps */ /* an internal pool should have been created in setcaps */
if (ximagesink->pool == NULL) if (G_UNLIKELY (ximagesink->pool == NULL))
goto no_pool; goto no_pool;
if (!gst_buffer_pool_set_active (ximagesink->pool, TRUE)) if (!gst_buffer_pool_set_active (ximagesink->pool, TRUE))
@ -1451,8 +1460,7 @@ static gboolean
gst_ximagesink_propose_allocation (GstBaseSink * bsink, GstQuery * query) gst_ximagesink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
{ {
GstXImageSink *ximagesink = GST_XIMAGESINK (bsink); GstXImageSink *ximagesink = GST_XIMAGESINK (bsink);
GstBufferPool *pool; GstBufferPool *pool = NULL;
GstStructure *config;
GstCaps *caps; GstCaps *caps;
guint size; guint size;
gboolean need_pool; gboolean need_pool;
@ -1462,45 +1470,21 @@ gst_ximagesink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
if (caps == NULL) if (caps == NULL)
goto no_caps; goto no_caps;
g_mutex_lock (&ximagesink->flow_lock); if (need_pool) {
if ((pool = ximagesink->pool))
gst_object_ref (pool);
g_mutex_unlock (&ximagesink->flow_lock);
if (pool != NULL) {
GstCaps *pcaps;
/* we had a pool, check caps */
config = gst_buffer_pool_get_config (pool);
gst_buffer_pool_config_get_params (config, &pcaps, &size, NULL, NULL);
GST_DEBUG_OBJECT (ximagesink,
"we had a pool with caps %" GST_PTR_FORMAT, pcaps);
if (!gst_caps_is_equal (caps, pcaps)) {
/* different caps, we can't use this pool */
GST_DEBUG_OBJECT (ximagesink, "pool has different caps");
gst_object_unref (pool);
pool = NULL;
}
gst_structure_free (config);
}
if (pool == NULL && need_pool) {
GstVideoInfo info; GstVideoInfo info;
if (!gst_video_info_from_caps (&info, caps)) if (!gst_video_info_from_caps (&info, caps))
goto invalid_caps; goto invalid_caps;
GST_DEBUG_OBJECT (ximagesink, "create new pool"); pool = gst_ximagesink_create_pool (ximagesink, caps, info.size, 0);
pool = gst_ximage_buffer_pool_new (ximagesink);
/* the normal size of a frame */ /* the normal size of a frame */
size = info.size; size = info.size;
config = gst_buffer_pool_get_config (pool); if (pool == NULL)
gst_buffer_pool_config_set_params (config, caps, size, 0, 0); goto no_pool;
if (!gst_buffer_pool_set_config (pool, config))
goto config_failed;
} }
if (pool) { if (pool) {
/* we need at least 2 buffer because we hold on to the last one */ /* we need at least 2 buffer because we hold on to the last one */
gst_query_add_allocation_pool (query, pool, size, 2, 0); gst_query_add_allocation_pool (query, pool, size, 2, 0);
@ -1524,10 +1508,9 @@ invalid_caps:
GST_DEBUG_OBJECT (bsink, "invalid caps specified"); GST_DEBUG_OBJECT (bsink, "invalid caps specified");
return FALSE; return FALSE;
} }
config_failed: no_pool:
{ {
GST_DEBUG_OBJECT (bsink, "failed setting config"); /* Already warned in create_pool */
gst_object_unref (pool);
return FALSE; return FALSE;
} }
} }