From 8414313e662c64eb50723973a0d78314ae07d152 Mon Sep 17 00:00:00 2001
From: Thibaut <thibaut.monseigne@inria.fr>
Date: Fri, 13 Mar 2020 14:26:35 +0100
Subject: [PATCH] :sparkles: Force non simultaneous stream search

---
 Demos/LSLTransformDemoOutlet.cs              |   2 +-
 LSL.cs                                       |   2 +-
 Scripts/Examples/DemoInletForFloatSamples.cs |   2 +-
 Scripts/Examples/ScaleMapping.cs             |   2 +-
 Scripts/Examples/TransformMapping.cs         |   2 +-
 Scripts/LSLTimeSync.cs                       |   2 +-
 Scripts/OV/OVInlets.cs                       | 125 ++++++++++---------
 Scripts/OV/Template/DoubleInlet.cs           |   6 +-
 Scripts/OV/Template/FloatInlet.cs            |   6 +-
 Scripts/OV/Template/FloatInlet.cs.meta       |   2 +-
 Scripts/OV/Template/MatrixInlet.cs           |   8 +-
 Scripts/OV/Template/StimulationInlet.cs      |  10 +-
 Scripts/OV/Template/StimulationInlet.cs.meta |   2 +-
 13 files changed, 86 insertions(+), 85 deletions(-)

diff --git a/Demos/LSLTransformDemoOutlet.cs b/Demos/LSLTransformDemoOutlet.cs
index cdb26e1..82883fe 100644
--- a/Demos/LSLTransformDemoOutlet.cs
+++ b/Demos/LSLTransformDemoOutlet.cs
@@ -1,4 +1,4 @@
-using LSL4Unity.Scripts;
+using LSL4Unity.Scripts;
 using UnityEngine;
 
 namespace LSL4Unity.Demos
diff --git a/LSL.cs b/LSL.cs
index 31a0d6e..db07b64 100644
--- a/LSL.cs
+++ b/LSL.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Runtime.InteropServices;
 
 /**
diff --git a/Scripts/Examples/DemoInletForFloatSamples.cs b/Scripts/Examples/DemoInletForFloatSamples.cs
index 68d58a9..4565f83 100644
--- a/Scripts/Examples/DemoInletForFloatSamples.cs
+++ b/Scripts/Examples/DemoInletForFloatSamples.cs
@@ -47,7 +47,7 @@ namespace LSL4Unity.Scripts.Examples
 		/// <param name="time"> The current Time. </param>
 		protected override void Process(float[] sample, double time)
 		{
-			//Assuming that a sample contains at least 3 values for x,y,z
+			//Assuming that a samples contains at least 3 values for x,y,z
 			float x = UseX ? sample[0] : 1;
 			float y = UseY ? sample[1] : 1;
 			float z = UseZ ? sample[2] : 1;
diff --git a/Scripts/Examples/ScaleMapping.cs b/Scripts/Examples/ScaleMapping.cs
index 64d2f6c..89803ba 100644
--- a/Scripts/Examples/ScaleMapping.cs
+++ b/Scripts/Examples/ScaleMapping.cs
@@ -1,4 +1,4 @@
-using UnityEngine;
+using UnityEngine;
 
 namespace LSL4Unity.Scripts.Examples
 {
diff --git a/Scripts/Examples/TransformMapping.cs b/Scripts/Examples/TransformMapping.cs
index 8603d8c..2ae00ea 100644
--- a/Scripts/Examples/TransformMapping.cs
+++ b/Scripts/Examples/TransformMapping.cs
@@ -1,4 +1,4 @@
-using UnityEngine;
+using UnityEngine;
 
 namespace LSL4Unity.Scripts.Examples
 {
diff --git a/Scripts/LSLTimeSync.cs b/Scripts/LSLTimeSync.cs
index d94944e..5260cde 100644
--- a/Scripts/LSLTimeSync.cs
+++ b/Scripts/LSLTimeSync.cs
@@ -1,4 +1,4 @@
-using UnityEngine;
+using UnityEngine;
 
 namespace LSL4Unity.Scripts
 {
diff --git a/Scripts/OV/OVInlets.cs b/Scripts/OV/OVInlets.cs
index e9ebcc7..b79f135 100644
--- a/Scripts/OV/OVInlets.cs
+++ b/Scripts/OV/OVInlets.cs
@@ -1,29 +1,32 @@
-using System;
+using System;
 using System.Collections;
 using UnityEngine;
 
 namespace LSL4Unity.Scripts.OV
 {
-	/// <summary> Base Inlet for OpenViBE Link </summary>
-	/// <seealso cref="UnityEngine.MonoBehaviour" />
-	public abstract class OVInlet : MonoBehaviour
+	/// <summary> Base Inlet for OpenViBE Link. </summary>
+	/// <seealso cref="MonoBehaviour" />
+	public abstract class OVInlet<T> : MonoBehaviour
 	{
-		public enum UpdateMoment { FixedUpdate, Update }
+		private enum UpdateMoment { FixedUpdate, Update }
 
-		public UpdateMoment Moment;
+		[SerializeField] private UpdateMoment moment     = UpdateMoment.Update;
+		[SerializeField] private string       streamName = "ovSignal";
 
-		public string StreamName = "ovSignal";
+		public string StreamName => streamName;
 
-		protected liblsl.StreamInlet        Inlet;
-		private   liblsl.StreamInfo[]       _results;
-		private   liblsl.ContinuousResolver _resolver;
+		protected liblsl.StreamInlet        inlet;
+		private   liblsl.ContinuousResolver resolver;
+
+		private   bool readyToResolve   = true;
+		protected int  expectedChannels = 0;
+		protected T[]  samples;
 
-		protected int ExpectedChannels = 0;
 
 		/// <summary> Start is called before the first frame update. </summary>
 		private void Start()
 		{
-			var hasAName = StreamName.Length != 0;
+			bool hasAName = streamName.Length != 0;
 
 			if (!hasAName)
 			{
@@ -32,8 +35,8 @@ namespace LSL4Unity.Scripts.OV
 				return;
 			}
 
-			Debug.Log("Creating LSL resolver for stream " + StreamName);
-			_resolver = new liblsl.ContinuousResolver("name", StreamName);
+			Debug.Log("Creating LSL resolver for stream " + streamName);
+			resolver = new liblsl.ContinuousResolver("name", streamName);
 
 			StartCoroutine(ResolveExpectedStream());
 		}
@@ -41,132 +44,130 @@ namespace LSL4Unity.Scripts.OV
 		/// <summary> Fixupdate is called once per physics framerate. </summary>
 		private void FixedUpdate()
 		{
-			if (Moment == UpdateMoment.FixedUpdate && Inlet != null) { PullSamples(); }
+			if (moment == UpdateMoment.FixedUpdate && inlet != null) { PullSamples(); }
 		}
 
 		/// <summary> Update is called once per frame. </summary>
 		private void Update()
 		{
-			if (Moment == UpdateMoment.Update && Inlet != null) { PullSamples(); }
+			if (moment == UpdateMoment.Update && inlet != null) { PullSamples(); }
 		}
 
 		/// <summary> Resolves the stream. </summary>
 		/// <returns></returns>
 		private IEnumerator ResolveExpectedStream()
 		{
-			_results = _resolver.Results();
-			yield return new WaitUntil(() => _results.Length > 0);
+			yield return new WaitUntil(() => readyToResolve); // False mutex to wait Found Stream before search an other
+			readyToResolve = false;                           // Avoïd double resolver
+
+			liblsl.StreamInfo[] results = resolver.Results();
+			yield return new WaitUntil(() => results.Length > 0);
 
-			Debug.Log($"Resolving Stream : {StreamName}");
+			Debug.Log($"Resolving Stream : {streamName}");
 
-			Inlet            = new liblsl.StreamInlet(_results[0]);
-			ExpectedChannels = Inlet.Info().ChannelCount();
+			inlet            = new liblsl.StreamInlet(results[0]);
+			expectedChannels = inlet.Info().ChannelCount();
 
+			readyToResolve = true;
 			yield return null;
 		}
 
 		/// <summary> Pull the samples. </summary>
 		protected abstract void PullSamples();
+
+		/// <summary> Override this method in the subclass to specify what should happen when samples are available. </summary>
+		/// <param name="input"> The Incomming Sample. </param>
+		/// <param name="time"> The current Time. </param>
+		protected abstract void Process(T[] input, double time);
 	}
 
-	public abstract class OVFloatInlet : OVInlet
+	/// <summary> Float Inlet for OpenViBE Link. </summary>
+	/// <seealso cref="OVInlet{T}" />
+	public abstract class OVFloatInlet : OVInlet<float>
 	{
-		private float[] _sample;
-
-		/// <inheritdoc cref="OVInlet.PullSamples"/>
+		/// <inheritdoc cref="OVInlet{T}.PullSamples"/>
 		protected override void PullSamples()
 		{
-			_sample = new float[ExpectedChannels];
+			samples = new float[expectedChannels];
 
 			try
 			{
-				double lastTimeStamp = Inlet.PullSample(_sample, 0.0f);
+				double lastTimeStamp = inlet.PullSample(samples, 0.0f);
 
 				if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE)
 				{
 					// do not miss the first one found
-					Process(_sample, lastTimeStamp);
+					Process(samples, lastTimeStamp);
 					// pull as long samples are available
-					while (Math.Abs(lastTimeStamp = Inlet.PullSample(_sample, 0.0f)) > Constants.TOLERANCE) { Process(_sample, lastTimeStamp); }
+					while (Math.Abs(lastTimeStamp = inlet.PullSample(samples, 0.0f)) > Constants.TOLERANCE) { Process(samples, lastTimeStamp); }
 				}
 			}
-			catch (ArgumentException aex)
+			catch (ArgumentException e)
 			{
 				Debug.LogError("An Error on pulling samples deactivating LSL inlet on...", this);
 				enabled = false;
-				Debug.LogException(aex, this);
+				Debug.LogException(e, this);
 			}
 		}
-
-		/// <summary> Override this method in the subclass to specify what should happen when samples are available. </summary>
-		/// <param name="sample"> The Incomming Sample. </param>
-		/// <param name="time"> The current Time. </param>
-		protected abstract void Process(float[] sample, double time);
 	}
 
-	public abstract class OVDoubleInlet : OVInlet
+	/// <summary> Double Inlet for OpenViBE Link. </summary>
+	/// <seealso cref="OVInlet{T}" />
+	public abstract class OVDoubleInlet : OVInlet<double>
 	{
-		private double[] _sample;
-
-		/// <inheritdoc cref="OVInlet.PullSamples"/>
+		/// <inheritdoc cref="OVInlet{T}.PullSamples"/>
 		protected override void PullSamples()
 		{
-			_sample = new double[ExpectedChannels];
+			samples = new double[expectedChannels];
 
 			try
 			{
-				double lastTimeStamp = Inlet.PullSample(_sample, 0.0f);
+				double lastTimeStamp = inlet.PullSample(samples, 0.0f);
 
 				if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE)
 				{
 					// do not miss the first one found
-					Process(_sample, lastTimeStamp);
+					Process(samples, lastTimeStamp);
 					// pull as long samples are available
-					while (Math.Abs(lastTimeStamp = Inlet.PullSample(_sample, 0.0f)) > Constants.TOLERANCE) { Process(_sample, lastTimeStamp); }
+					while (Math.Abs(lastTimeStamp = inlet.PullSample(samples, 0.0f)) > Constants.TOLERANCE) { Process(samples, lastTimeStamp); }
 				}
 			}
-			catch (ArgumentException aex)
+			catch (ArgumentException e)
 			{
 				Debug.LogError("An Error on pulling samples deactivating LSL inlet on...", this);
 				enabled = false;
-				Debug.LogException(aex, this);
+				Debug.LogException(e, this);
 			}
 		}
-
-		/// <inheritdoc cref="OVFloatInlet.Process"/>
-		protected abstract void Process(double[] sample, double time);
 	}
 
-	public abstract class OVIntInlet : OVInlet
+	/// <summary> Int Inlet for OpenViBE Link. </summary>
+	/// <seealso cref="OVInlet{T}" />
+	public abstract class OVIntInlet : OVInlet<int>
 	{
-		private int[] _sample;
-
-		/// <inheritdoc cref="OVInlet.PullSamples"/>
+		/// <inheritdoc cref="OVInlet{T}.PullSamples"/>
 		protected override void PullSamples()
 		{
-			_sample = new int[ExpectedChannels];
+			samples = new int[expectedChannels];
 
 			try
 			{
-				double lastTimeStamp = Inlet.PullSample(_sample, 0.0f);
+				double lastTimeStamp = inlet.PullSample(samples, 0.0f);
 
 				if (Math.Abs(lastTimeStamp) > Constants.TOLERANCE)
 				{
 					// do not miss the first one found
-					Process(_sample, lastTimeStamp);
+					Process(samples, lastTimeStamp);
 					// pull as long samples are available
-					while (Math.Abs(lastTimeStamp = Inlet.PullSample(_sample, 0.0f)) > Constants.TOLERANCE) { Process(_sample, lastTimeStamp); }
+					while (Math.Abs(lastTimeStamp = inlet.PullSample(samples, 0.0f)) > Constants.TOLERANCE) { Process(samples, lastTimeStamp); }
 				}
 			}
-			catch (ArgumentException aex)
+			catch (ArgumentException e)
 			{
 				Debug.LogError("An Error on pulling samples deactivating LSL inlet on...", this);
 				enabled = false;
-				Debug.LogException(aex, this);
+				Debug.LogException(e, this);
 			}
 		}
-
-		/// <inheritdoc cref="OVFloatInlet.Process"/>
-		protected abstract void Process(int[] sample, double time);
 	}
 }
diff --git a/Scripts/OV/Template/DoubleInlet.cs b/Scripts/OV/Template/DoubleInlet.cs
index 50a6dcd..e8290d8 100644
--- a/Scripts/OV/Template/DoubleInlet.cs
+++ b/Scripts/OV/Template/DoubleInlet.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
 using UnityEngine;
 
 namespace LSL4Unity.Scripts.OV.Template
@@ -7,9 +7,9 @@ namespace LSL4Unity.Scripts.OV.Template
 	/// <seealso cref="OVFloatInlet" />
 	public class DoubleInlet : OVDoubleInlet
 	{
-		public double[] LastSample;
+		public double[] LastSample { get; private set; }
 
 		/// <inheritdoc cref="OVDoubleInlet.Process"/>
-		protected override void Process(double[] sample, double time) { LastSample = sample; }
+		protected override void Process(double[] input, double time) { LastSample = input; }
 	}
 }
diff --git a/Scripts/OV/Template/FloatInlet.cs b/Scripts/OV/Template/FloatInlet.cs
index caac0d3..9b58359 100644
--- a/Scripts/OV/Template/FloatInlet.cs
+++ b/Scripts/OV/Template/FloatInlet.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
 using UnityEngine;
 
 namespace LSL4Unity.Scripts.OV.Template
@@ -7,9 +7,9 @@ namespace LSL4Unity.Scripts.OV.Template
 	/// <seealso cref="OVFloatInlet" />
 	public class FloatInlet : OVFloatInlet
 	{
-		public float[] LastSample;
+		public float[] LastSample { get; private set; }
 
 		/// <inheritdoc cref="OVFloatInlet.Process"/>
-		protected override void Process(float[] sample, double time) { LastSample = sample; }
+		protected override void Process(float[] input, double time) { LastSample = input; }
 	}
 }
diff --git a/Scripts/OV/Template/FloatInlet.cs.meta b/Scripts/OV/Template/FloatInlet.cs.meta
index ba234f5..6f3d22e 100644
--- a/Scripts/OV/Template/FloatInlet.cs.meta
+++ b/Scripts/OV/Template/FloatInlet.cs.meta
@@ -1,3 +1,3 @@
 fileFormatVersion: 2
-guid: bb7dc342a4384c66a5915e670a3bcba1
+guid: 678ffdf9fb713f24fb807bd76283a226
 timeCreated: 1583931462
\ No newline at end of file
diff --git a/Scripts/OV/Template/MatrixInlet.cs b/Scripts/OV/Template/MatrixInlet.cs
index 7d79805..af5b5e5 100644
--- a/Scripts/OV/Template/MatrixInlet.cs
+++ b/Scripts/OV/Template/MatrixInlet.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
 
 namespace LSL4Unity.Scripts.OV.Template
 {
@@ -25,12 +25,12 @@ namespace LSL4Unity.Scripts.OV.Template
 			_curSample  = 0;
 		}
 
-		protected override void Process(double[] sample, double time)
+		protected override void Process(double[] input, double time)
 		{
-			if (NChannel == -1) { NChannel = (int) (sample[0]); }
+			if (NChannel == -1) { NChannel = (int) (input[0]); }
 			else if (NSample == -1)
 			{
-				NSample = (int) (sample[0]);
+				NSample = (int) (input[0]);
 				Matrix  = new double[NChannel, NSample];
 				ResetMatrix();
 			}
diff --git a/Scripts/OV/Template/StimulationInlet.cs b/Scripts/OV/Template/StimulationInlet.cs
index d2bda28..ea0f6df 100644
--- a/Scripts/OV/Template/StimulationInlet.cs
+++ b/Scripts/OV/Template/StimulationInlet.cs
@@ -1,4 +1,4 @@
-using UnityEngine;
+using UnityEngine;
 
 namespace LSL4Unity.Scripts.OV.Template
 {
@@ -6,13 +6,13 @@ namespace LSL4Unity.Scripts.OV.Template
 	/// <seealso cref="OVFloatInlet" />
 	public class StimulationInlet : OVIntInlet
 	{
-		public int[] LastSample;
+		public int[] LastSample { get; private set; }
 
 		/// <inheritdoc cref="OVIntInlet.Process"/>
-		protected override void Process(int[] sample, double time)
+		protected override void Process(int[] input, double time)
 		{
-			LastSample = sample;
-			Debug.Log($"Got {sample.Length} ints at {time}");
+			LastSample = input;
+			Debug.Log($"Got {input.Length} ints at {time}");
 		}
 	}
 }
diff --git a/Scripts/OV/Template/StimulationInlet.cs.meta b/Scripts/OV/Template/StimulationInlet.cs.meta
index 4759e7e..eb14ea5 100644
--- a/Scripts/OV/Template/StimulationInlet.cs.meta
+++ b/Scripts/OV/Template/StimulationInlet.cs.meta
@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: f595da02601a05a4c8159e37aa87d213
+guid: 2906273330e2ad043bef98fa5e87a038
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2
-- 
GitLab