opusenc: Use aux vars to minimize critical region

This avoid dead lock between gst_audio_encoder_finish_frame() and
gst_opus_enc_get_property().
Also, now bytes var is set into protected section.

https://bugzilla.gnome.org/show_bug.cgi?id=729882
This commit is contained in:
Miguel París Díaz 2014-05-10 18:32:28 +02:00 committed by Sebastian Dröge
parent ab7f9654f4
commit 93ba600ba9

View file

@ -786,15 +786,24 @@ gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buf)
{ {
guint8 *bdata = NULL, *data, *mdata = NULL; guint8 *bdata = NULL, *data, *mdata = NULL;
gsize bsize, size; gsize bsize, size;
gsize bytes = enc->frame_samples * enc->n_channels * 2; gsize bytes;
gint ret = GST_FLOW_OK; gint ret = GST_FLOW_OK;
GstMapInfo map; GstMapInfo map;
GstMapInfo omap; GstMapInfo omap;
gint outsize; gint outsize;
GstBuffer *outbuf; GstBuffer *outbuf;
guint max_payload_size;
gint frame_samples;
g_mutex_lock (&enc->property_lock); g_mutex_lock (&enc->property_lock);
bytes = enc->frame_samples * enc->n_channels * 2;
max_payload_size = enc->max_payload_size;
frame_samples = enc->frame_samples;
g_mutex_unlock (&enc->property_lock);
if (G_LIKELY (buf)) { if (G_LIKELY (buf)) {
gst_buffer_map (buf, &map, GST_MAP_READ); gst_buffer_map (buf, &map, GST_MAP_READ);
bdata = map.data; bdata = map.data;
@ -818,21 +827,21 @@ gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buf)
g_assert (size == bytes); g_assert (size == bytes);
outbuf = gst_buffer_new_and_alloc (enc->max_payload_size * enc->n_channels); outbuf = gst_buffer_new_and_alloc (max_payload_size * enc->n_channels);
if (!outbuf) if (!outbuf)
goto done; goto done;
GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)",
enc->frame_samples, (int) bytes); frame_samples, (int) bytes);
gst_buffer_map (outbuf, &omap, GST_MAP_WRITE); gst_buffer_map (outbuf, &omap, GST_MAP_WRITE);
GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)", GST_DEBUG_OBJECT (enc, "encoding %d samples (%d bytes)",
enc->frame_samples, (int) bytes); frame_samples, (int) bytes);
outsize = outsize =
opus_multistream_encode (enc->state, (const gint16 *) data, opus_multistream_encode (enc->state, (const gint16 *) data,
enc->frame_samples, omap.data, enc->max_payload_size * enc->n_channels); frame_samples, omap.data, max_payload_size * enc->n_channels);
gst_buffer_unmap (outbuf, &omap); gst_buffer_unmap (outbuf, &omap);
@ -840,10 +849,10 @@ gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buf)
GST_ERROR_OBJECT (enc, "Encoding failed: %d", outsize); GST_ERROR_OBJECT (enc, "Encoding failed: %d", outsize);
ret = GST_FLOW_ERROR; ret = GST_FLOW_ERROR;
goto done; goto done;
} else if (outsize > enc->max_payload_size) { } else if (outsize > max_payload_size) {
GST_WARNING_OBJECT (enc, GST_WARNING_OBJECT (enc,
"Encoded size %d is higher than max payload size (%d bytes)", "Encoded size %d is higher than max payload size (%d bytes)",
outsize, enc->max_payload_size); outsize, max_payload_size);
ret = GST_FLOW_ERROR; ret = GST_FLOW_ERROR;
goto done; goto done;
} }
@ -853,13 +862,12 @@ gst_opus_enc_encode (GstOpusEnc * enc, GstBuffer * buf)
ret = ret =
gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), outbuf, gst_audio_encoder_finish_frame (GST_AUDIO_ENCODER (enc), outbuf,
enc->frame_samples); frame_samples);
done: done:
if (bdata) if (bdata)
gst_buffer_unmap (buf, &map); gst_buffer_unmap (buf, &map);
g_mutex_unlock (&enc->property_lock);
if (mdata) if (mdata)
g_free (mdata); g_free (mdata);