baseaudioencoder: Fix thread safety issues if both pads have different streaming...
authorSebastian Dröge <sebastian.droege@collabora.co.uk>
Wed, 17 Aug 2011 09:34:04 +0000 (11:34 +0200)
committerSebastian Dröge <sebastian.droege@collabora.co.uk>
Wed, 17 Aug 2011 09:34:04 +0000 (11:34 +0200)
omx/gstbaseaudioencoder.c
omx/gstbaseaudioencoder.h

index 1e6040d..8c4f6df 100644 (file)
@@ -354,6 +354,8 @@ gst_base_audio_encoder_init (GstBaseAudioEncoder * enc,
   enc->priv->adapter = gst_adapter_new ();
   enc->ctx = &enc->priv->ctx;
 
+  g_static_rec_mutex_init (&enc->stream_lock);
+
   /* property default */
   enc->perfect_ts = DEFAULT_PERFECT_TS;
   enc->hard_resync = DEFAULT_HARD_RESYNC;
@@ -367,6 +369,8 @@ gst_base_audio_encoder_init (GstBaseAudioEncoder * enc,
 static void
 gst_base_audio_encoder_reset (GstBaseAudioEncoder * enc, gboolean full)
 {
+  GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
+
   if (full) {
     enc->priv->active = FALSE;
     enc->priv->samples_in = 0;
@@ -389,6 +393,8 @@ gst_base_audio_encoder_reset (GstBaseAudioEncoder * enc, gboolean full)
   enc->priv->base_gp = -1;
   enc->priv->samples = 0;
   enc->priv->discont = FALSE;
+
+  GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
 }
 
 static void
@@ -398,6 +404,8 @@ gst_base_audio_encoder_finalize (GObject * object)
 
   g_object_unref (enc->priv->adapter);
 
+  g_static_rec_mutex_free (&enc->stream_lock);
+
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -443,6 +451,8 @@ gst_base_audio_encoder_finish_frame (GstBaseAudioEncoder * enc, GstBuffer * buf,
   g_return_val_if_fail (buf == NULL || GST_BUFFER_SIZE (buf) > 0,
       GST_FLOW_ERROR);
 
+  GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
+
   GST_LOG_OBJECT (enc, "accepting %d bytes encoded data as %d samples",
       buf ? GST_BUFFER_SIZE (buf) : -1, samples);
 
@@ -451,10 +461,9 @@ gst_base_audio_encoder_finish_frame (GstBaseAudioEncoder * enc, GstBuffer * buf,
 
   if (priv->pending_events) {
     GList *pending_events, *l;
-    GST_OBJECT_LOCK (enc);
+
     pending_events = priv->pending_events;
     priv->pending_events = NULL;
-    GST_OBJECT_UNLOCK (enc);
 
     GST_DEBUG_OBJECT (enc, "Pushing pending events");
     for (l = priv->pending_events; l; l = l->next)
@@ -602,6 +611,8 @@ gst_base_audio_encoder_finish_frame (GstBaseAudioEncoder * enc, GstBuffer * buf,
   }
 
 exit:
+  GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
+
   return ret;
 
   /* ERRORS */
@@ -612,7 +623,8 @@ overflow:
             samples, priv->offset / ctx->state.bpf), (NULL));
     if (buf)
       gst_buffer_unref (buf);
-    return GST_FLOW_ERROR;
+    ret = GST_FLOW_ERROR;
+    goto exit;
   }
 }
 
@@ -752,6 +764,8 @@ gst_base_audio_encoder_chain (GstPad * pad, GstBuffer * buffer)
   priv = enc->priv;
   ctx = enc->ctx;
 
+  GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
+
   /* should know what is coming by now */
   if (!ctx->state.bpf)
     goto not_negotiated;
@@ -882,6 +896,9 @@ gst_base_audio_encoder_chain (GstPad * pad, GstBuffer * buffer)
 
 done:
   GST_LOG_OBJECT (enc, "chain leaving");
+
+  GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
+
   return ret;
 
   /* ERRORS */
@@ -890,7 +907,8 @@ not_negotiated:
     GST_ELEMENT_ERROR (enc, CORE, NEGOTIATION, (NULL),
         ("encoder not initialized"));
     gst_buffer_unref (buffer);
-    return GST_FLOW_NOT_NEGOTIATED;
+    ret = GST_FLOW_NOT_NEGOTIATED;
+    goto done;
   }
 wrong_buffer:
   {
@@ -898,7 +916,8 @@ wrong_buffer:
         ("buffer size %d not a multiple of %d", GST_BUFFER_SIZE (buffer),
             ctx->state.bpf));
     gst_buffer_unref (buffer);
-    return GST_FLOW_ERROR;
+    ret = GST_FLOW_ERROR;
+    goto done;
   }
 }
 
@@ -920,6 +939,8 @@ gst_base_audio_encoder_sink_setcaps (GstPad * pad, GstCaps * caps)
   ctx = enc->ctx;
   state = &ctx->state;
 
+  GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
+
   GST_DEBUG_OBJECT (enc, "caps: %" GST_PTR_FORMAT, caps);
 
   if (!gst_caps_is_fixed (caps))
@@ -971,13 +992,17 @@ gst_base_audio_encoder_sink_setcaps (GstPad * pad, GstCaps * caps)
     GST_DEBUG_OBJECT (enc, "new audio format identical to configured format");
   }
 
+exit:
+
+  GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
+
   return res;
 
   /* ERRORS */
 refuse_caps:
   {
     GST_WARNING_OBJECT (enc, "rejected caps %" GST_PTR_FORMAT, caps);
-    return res;
+    goto exit;
   }
 }
 
@@ -1104,6 +1129,7 @@ gst_base_audio_encoder_sink_eventfunc (GstBaseAudioEncoder * enc,
         break;
       }
 
+      GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
       /* finish current segment */
       gst_base_audio_encoder_drain (enc);
       /* reset partially for new segment */
@@ -1111,6 +1137,7 @@ gst_base_audio_encoder_sink_eventfunc (GstBaseAudioEncoder * enc,
       /* and follow along with segment */
       gst_segment_set_newsegment_full (&enc->segment, update, rate, arate,
           format, start, stop, time);
+      GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
       break;
     }
 
@@ -1118,6 +1145,7 @@ gst_base_audio_encoder_sink_eventfunc (GstBaseAudioEncoder * enc,
       break;
 
     case GST_EVENT_FLUSH_STOP:
+      GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
       /* discard any pending stuff */
       /* TODO route through drain ?? */
       if (!enc->priv->drained && klass->flush)
@@ -1125,16 +1153,17 @@ gst_base_audio_encoder_sink_eventfunc (GstBaseAudioEncoder * enc,
       /* and get (re)set for the sequel */
       gst_base_audio_encoder_reset (enc, FALSE);
 
-      GST_OBJECT_LOCK (enc);
       g_list_foreach (enc->priv->pending_events, (GFunc) gst_event_unref, NULL);
       g_list_free (enc->priv->pending_events);
       enc->priv->pending_events = NULL;
-      GST_OBJECT_UNLOCK (enc);
+      GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
 
       break;
 
     case GST_EVENT_EOS:
+      GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
       gst_base_audio_encoder_drain (enc);
+      GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
       break;
 
     default:
@@ -1178,10 +1207,10 @@ gst_base_audio_encoder_sink_event (GstPad * pad, GstEvent * event)
         || GST_EVENT_TYPE (event) == GST_EVENT_FLUSH_STOP) {
       ret = gst_pad_event_default (pad, event);
     } else {
-      GST_OBJECT_LOCK (enc);
+      GST_BASE_AUDIO_ENCODER_STREAM_LOCK (enc);
       enc->priv->pending_events =
           g_list_append (enc->priv->pending_events, event);
-      GST_OBJECT_UNLOCK (enc);
+      GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK (enc);
       ret = TRUE;
     }
   }
index 52d4470..c459ba2 100644 (file)
@@ -83,6 +83,8 @@ G_BEGIN_DECLS
  */
 #define GST_BASE_AUDIO_ENCODER_SEGMENT(obj)     (GST_BASE_AUDIO_ENCODER_CAST (obj)->segment)
 
+#define GST_BASE_AUDIO_ENCODER_STREAM_LOCK(enc) g_static_rec_mutex_lock (&GST_BASE_AUDIO_ENCODER (enc)->stream_lock)
+#define GST_BASE_AUDIO_ENCODER_STREAM_UNLOCK(enc) g_static_rec_mutex_unlock (&GST_BASE_AUDIO_ENCODER (enc)->stream_lock)
 
 typedef struct _GstBaseAudioEncoder GstBaseAudioEncoder;
 typedef struct _GstBaseAudioEncoderClass GstBaseAudioEncoderClass;
@@ -130,6 +132,11 @@ struct _GstBaseAudioEncoder {
   GstPad         *sinkpad;
   GstPad         *srcpad;
 
+  /* protects all data processing, i.e. is locked
+   * in the chain function, finish_frame and when
+   * processing serialized events */
+  GStaticRecMutex stream_lock;
+
   /* MT-protected (with STREAM_LOCK) */
   GstSegment      segment;
   GstBaseAudioEncoderContext *ctx;