+static gboolean
+gst_base_src_set_allocation (GstBaseSrc * basesrc, GstBufferPool * pool,
+ const GstAllocator * allocator, guint prefix, guint alignment)
+{
+ GstBufferPool *oldpool;
+ GstBaseSrcPrivate *priv = basesrc->priv;
+
+ if (pool) {
+ if (!gst_buffer_pool_set_active (pool, TRUE))
+ goto activate_failed;
+ }
+
+ GST_OBJECT_LOCK (basesrc);
+ oldpool = priv->pool;
+ priv->pool = pool;
+
+ priv->allocator = allocator;
+
+ priv->prefix = prefix;
+ priv->alignment = alignment;
+ GST_OBJECT_UNLOCK (basesrc);
+
+ if (oldpool) {
+ gst_buffer_pool_set_active (oldpool, FALSE);
+ gst_object_unref (oldpool);
+ }
+ return TRUE;
+
+ /* ERRORS */
+activate_failed:
+ {
+ GST_ERROR_OBJECT (basesrc, "failed to activate bufferpool.");
+ return FALSE;
+ }
+}
+
+static gboolean
+gst_base_src_activate_pool (GstBaseSrc * basesrc, gboolean active)
+{
+ GstBaseSrcPrivate *priv = basesrc->priv;
+ GstBufferPool *pool;
+ gboolean res = TRUE;
+
+ GST_OBJECT_LOCK (basesrc);
+ if ((pool = priv->pool))
+ pool = gst_object_ref (pool);
+ GST_OBJECT_UNLOCK (basesrc);
+
+ if (pool) {
+ res = gst_buffer_pool_set_active (pool, active);
+ gst_object_unref (pool);
+ }
+ return res;
+}
+
+static gboolean
+gst_base_src_prepare_allocation (GstBaseSrc * basesrc, GstCaps * caps)
+{
+ GstBaseSrcClass *bclass;
+ gboolean result = TRUE;
+ GstQuery *query;
+ GstBufferPool *pool = NULL;
+ const GstAllocator *allocator = NULL;
+ guint size, min, max, prefix, alignment;
+
+ bclass = GST_BASE_SRC_GET_CLASS (basesrc);
+
+ /* make query and let peer pad answer, we don't really care if it worked or
+ * not, if it failed, the allocation query would contain defaults and the
+ * subclass would then set better values if needed */
+ query = gst_query_new_allocation (caps, TRUE);
+ if (!gst_pad_peer_query (basesrc->srcpad, query)) {
+ /* not a problem, just debug a little */
+ GST_DEBUG_OBJECT (basesrc, "peer ALLOCATION query failed");
+ }
+
+ if (G_LIKELY (bclass->setup_allocation))
+ result = bclass->setup_allocation (basesrc, query);
+
+ GST_DEBUG_OBJECT (basesrc, "ALLOCATION params: %" GST_PTR_FORMAT, query);
+ gst_query_parse_allocation_params (query, &size, &min, &max, &prefix,
+ &alignment, &pool);
+
+ if (size == 0) {
+ const gchar *mem = NULL;
+
+ /* no size, we have variable size buffers */
+ if (gst_query_get_n_allocation_memories (query) > 0) {
+ mem = gst_query_parse_nth_allocation_memory (query, 0);
+ }
+ allocator = gst_allocator_find (mem);
+ } else if (pool == NULL) {
+ /* fixed size, we can use a bufferpool */
+ GstStructure *config;
+
+ /* we did not get a pool, make one ourselves then */
+ pool = gst_buffer_pool_new ();
+
+ config = gst_buffer_pool_get_config (pool);
+ gst_buffer_pool_config_set (config, caps, size, min, max, prefix,
+ alignment);
+ gst_buffer_pool_set_config (pool, config);
+ }
+
+ gst_query_unref (query);
+
+ result =
+ gst_base_src_set_allocation (basesrc, pool, allocator, prefix, alignment);
+
+ return result;
+
+}
+