audiodecoder: Protect samples_in/bytes_out and audio info with object lock
authorSebastian Dröge <sebastian@centricular.com>
Mon, 4 Jul 2016 09:12:25 +0000 (11:12 +0200)
committerSebastian Dröge <sebastian@centricular.com>
Mon, 4 Jul 2016 09:12:55 +0000 (11:12 +0200)
It might cause invalid calculations during the CONVERT query otherwise.

gst-libs/gst/audio/gstaudiodecoder.c

index 7840db1..e583e2f 100644 (file)
@@ -499,8 +499,10 @@ gst_audio_decoder_reset (GstAudioDecoder * dec, gboolean full)
 
   if (full) {
     dec->priv->active = FALSE;
+    GST_OBJECT_LOCK (dec);
     dec->priv->bytes_in = 0;
     dec->priv->samples_out = 0;
+    GST_OBJECT_UNLOCK (dec);
     dec->priv->agg = -1;
     dec->priv->error_count = 0;
     gst_audio_decoder_clear_queues (dec);
@@ -527,12 +529,14 @@ gst_audio_decoder_reset (GstAudioDecoder * dec, gboolean full)
     if (dec->priv->ctx.allocator)
       gst_object_unref (dec->priv->ctx.allocator);
 
+    GST_OBJECT_LOCK (dec);
     gst_caps_replace (&dec->priv->ctx.input_caps, NULL);
     gst_caps_replace (&dec->priv->ctx.allocation_caps, NULL);
 
     memset (&dec->priv->ctx, 0, sizeof (dec->priv->ctx));
 
     gst_audio_info_init (&dec->priv->ctx.info);
+    GST_OBJECT_UNLOCK (dec);
     dec->priv->ctx.max_errors = GST_AUDIO_DECODER_MAX_ERRORS;
     dec->priv->ctx.had_output_data = FALSE;
     dec->priv->ctx.had_input_data = FALSE;
@@ -817,7 +821,9 @@ gst_audio_decoder_set_output_format (GstAudioDecoder * dec,
   }
 
   /* copy the GstAudioInfo */
+  GST_OBJECT_LOCK (dec);
   dec->priv->ctx.info = *info;
+  GST_OBJECT_UNLOCK (dec);
   dec->priv->ctx.output_format_changed = TRUE;
 
 done:
@@ -1388,8 +1394,10 @@ gst_audio_decoder_finish_frame (GstAudioDecoder * dec, GstBuffer * buf,
     }
   }
 
+  GST_OBJECT_LOCK (dec);
   priv->samples += samples;
   priv->samples_out += samples;
+  GST_OBJECT_UNLOCK (dec);
 
   /* we got data, so note things are looking up */
   if (G_UNLIKELY (dec->priv->error_count))
@@ -1450,7 +1458,9 @@ gst_audio_decoder_handle_frame (GstAudioDecoder * dec,
         GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
     g_queue_push_tail (&dec->priv->frames, buffer);
     dec->priv->ctx.delay = dec->priv->frames.length;
+    GST_OBJECT_LOCK (dec);
     dec->priv->bytes_in += size;
+    GST_OBJECT_UNLOCK (dec);
   } else {
     GST_LOG_OBJECT (dec, "providing subclass with NULL frame");
   }
@@ -1951,8 +1961,14 @@ not_negotiated:
 static inline gboolean
 gst_audio_decoder_do_byte (GstAudioDecoder * dec)
 {
-  return dec->priv->ctx.do_estimate_rate && dec->priv->ctx.info.bpf &&
+  gboolean ret;
+
+  GST_OBJECT_LOCK (dec);
+  ret = dec->priv->ctx.do_estimate_rate && dec->priv->ctx.info.bpf &&
       dec->priv->ctx.info.rate <= dec->priv->samples_out;
+  GST_OBJECT_UNLOCK (dec);
+
+  return ret;
 }
 
 /* Must be called holding the GST_AUDIO_DECODER_STREAM_LOCK */
@@ -1966,6 +1982,7 @@ gst_audio_decoder_negotiate_default_caps (GstAudioDecoder * dec)
   guint64 channel_mask = 0;
   gint caps_size;
   GstStructure *structure;
+  GstAudioInfo info;
 
   templcaps = gst_pad_get_pad_template_caps (dec->srcpad);
   caps = gst_pad_peer_query_caps (dec->srcpad, templcaps);
@@ -2041,9 +2058,13 @@ gst_audio_decoder_negotiate_default_caps (GstAudioDecoder * dec)
     }
   }
 
-  if (!caps || !gst_audio_info_from_caps (&dec->priv->ctx.info, caps))
+  if (!caps || !gst_audio_info_from_caps (&info, caps))
     goto caps_error;
 
+  GST_OBJECT_LOCK (dec);
+  dec->priv->ctx.info = info;
+  GST_OBJECT_UNLOCK (dec);
+
   GST_INFO_OBJECT (dec,
       "Chose default caps %" GST_PTR_FORMAT " for initial gap", caps);
   gst_caps_unref (caps);
@@ -2598,9 +2619,12 @@ gst_audio_decoder_sink_query_default (GstAudioDecoder * dec, GstQuery * query)
       gint64 src_val, dest_val;
 
       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
-      if (!(res = __gst_audio_encoded_audio_convert (&dec->priv->ctx.info,
-                  dec->priv->bytes_in, dec->priv->samples_out,
-                  src_fmt, src_val, &dest_fmt, &dest_val)))
+      GST_OBJECT_LOCK (dec);
+      res = __gst_audio_encoded_audio_convert (&dec->priv->ctx.info,
+          dec->priv->bytes_in, dec->priv->samples_out,
+          src_fmt, src_val, &dest_fmt, &dest_val);
+      GST_OBJECT_UNLOCK (dec);
+      if (!res)
         goto error;
       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
       break;
@@ -2779,8 +2803,11 @@ gst_audio_decoder_src_query_default (GstAudioDecoder * dec, GstQuery * query)
       gint64 src_val, dest_val;
 
       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
-      if (!(res = gst_audio_info_convert (&dec->priv->ctx.info,
-                  src_fmt, src_val, dest_fmt, &dest_val)))
+      GST_OBJECT_LOCK (dec);
+      res = gst_audio_info_convert (&dec->priv->ctx.info,
+          src_fmt, src_val, dest_fmt, &dest_val);
+      GST_OBJECT_UNLOCK (dec);
+      if (!res)
         break;
       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
       break;