From f2ad031effcad74fd6aa56f8fe9bfa7465bb5a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Brzezi=C5=84ski?= Date: Mon, 11 Mar 2024 13:14:51 +0100 Subject: [PATCH] audioencoder: Avoid wrapping temporarily mapped memory with a GstBuffer and passing that to subclass Memory from gst_adapter_map() could live shorter than the GstMemory that the GstBuffer wraps around it, which in lucky cases 'just' caused a re-use of the same memory for multiple (potentially still in use!) input buffers, but could easily end up pointing to an already-freed memory. Manifested when an AudioToolbox encoder kept getting silence inserted in seemingly random circumstances, turned out to be the memory being re-used by GStreamer at the same time that the AT API was processing it... Part-of: --- .../gst-libs/gst/audio/gstaudioencoder.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/subprojects/gst-plugins-base/gst-libs/gst/audio/gstaudioencoder.c b/subprojects/gst-plugins-base/gst-libs/gst/audio/gstaudioencoder.c index f77216709b..5265993652 100644 --- a/subprojects/gst-plugins-base/gst-libs/gst/audio/gstaudioencoder.c +++ b/subprojects/gst-plugins-base/gst-libs/gst/audio/gstaudioencoder.c @@ -1150,12 +1150,9 @@ gst_audio_encoder_push_buffers (GstAudioEncoder * enc, gboolean force) priv->got_data = FALSE; if (G_LIKELY (need)) { - const guint8 *data; - - data = gst_adapter_map (priv->adapter, priv->offset + need); - buf = - gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, - (gpointer) data, priv->offset + need, priv->offset, need, NULL, NULL); + buf = gst_adapter_get_buffer (priv->adapter, priv->offset + need); + buf = gst_buffer_make_writable (buf); + gst_buffer_resize (buf, priv->offset, -1); } else if (!priv->drainable) { GST_DEBUG_OBJECT (enc, "non-drainable and no more data"); goto finish; @@ -1182,7 +1179,6 @@ gst_audio_encoder_push_buffers (GstAudioEncoder * enc, gboolean force) if (G_LIKELY (buf)) { gst_buffer_unref (buf); - gst_adapter_unmap (priv->adapter); } finish: