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: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/6320>
This commit is contained in:
Piotr Brzeziński 2024-03-11 13:14:51 +01:00 committed by GStreamer Marge Bot
parent 0f1dfc2db0
commit 15e0affc98

View file

@ -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: