Browse Source

Make seek and tell thread-safe (bug #245)

Bart van Strien 14 years ago
parent
commit
404a3d529c

+ 1 - 1
src/modules/audio/Source.h

@@ -73,7 +73,7 @@ namespace audio
 		virtual float getVolume() const = 0;
 		
 		virtual void seek(float offset, Unit unit) = 0;
-		virtual float tell(Unit unit) const = 0;
+		virtual float tell(Unit unit) = 0;
 
 		// all float * v must be of size 3
 		virtual void setPosition(float * v) = 0;

+ 1 - 1
src/modules/audio/null/Source.cpp

@@ -105,7 +105,7 @@ namespace null
 	{
 	}
 	
-	float Source::tell(Source::Unit) const
+	float Source::tell(Source::Unit)
 	{
 		return 0.0f;
 	}

+ 1 - 1
src/modules/audio/null/Source.h

@@ -58,7 +58,7 @@ namespace null
 		virtual void setVolume(float volume);
 		virtual float getVolume() const;
 		virtual void seek(float offset, Unit unit);
-		virtual float tell(Unit unit) const;
+		virtual float tell(Unit unit);
 		virtual void setPosition(float * v);
 		virtual void getPosition(float * v) const;
 		virtual void setVelocity(float * v);

+ 12 - 0
src/modules/audio/openal/Pool.cpp

@@ -237,6 +237,18 @@ namespace openal
 		}
 	}
 
+	void Pool::seek(Source * source, float offset, void * unit)
+	{
+		thread::Lock lock(mutex);
+		return source->seekAtomic(offset, unit);
+	}
+
+	float Pool::tell(Source * source, void * unit)
+	{
+		thread::Lock lock(mutex);
+		return source->tellAtomic(unit);
+	}
+
 	ALuint Pool::findi(const Source * source) const
 	{
 		std::map<Source *, ALuint>::const_iterator i = playing.find((Source *)source);

+ 2 - 0
src/modules/audio/openal/Pool.h

@@ -103,6 +103,8 @@ namespace openal
 		void resume(Source * source);
 		void rewind();
 		void rewind(Source * source);
+		void seek(Source * source, float offset, void * unit);
+		float tell(Source * source, void * unit);
 
 	private:
 

+ 14 - 4
src/modules/audio/openal/Source.cpp

@@ -221,11 +221,11 @@ namespace openal
 		return volume;
 	}
 	
-	void Source::seek(float offset, Source::Unit unit)
+	void Source::seekAtomic(float offset, void * unit)
 	{
 		if (valid)
 		{
-			switch (unit) {
+			switch (*((Source::Unit*) unit)) {
 				case Source::UNIT_SAMPLES:
 					if (type == TYPE_STREAM) {
 						ALint buffer;
@@ -249,13 +249,18 @@ namespace openal
 			}
 		}
 	}
+
+	void Source::seek(float offset, Source::Unit unit)
+	{
+		return pool->seek(this, offset, &unit);
+	}
 	
-	float Source::tell(Source::Unit unit) const
+	float Source::tellAtomic(void * unit) const
 	{
 		if (valid)
 		{
 			float offset;
-			switch (unit) {
+			switch (*((Source::Unit*) unit)) {
 				case Source::UNIT_SAMPLES:
 					alGetSourcef(source, AL_SAMPLE_OFFSET, &offset);
 					if (type == TYPE_STREAM) offset += offsetSamples;
@@ -276,6 +281,11 @@ namespace openal
 		return 0.0f;
 	}
 
+	float Source::tell(Source::Unit unit)
+	{
+		return pool->tell(this, &unit);
+	}
+
 	void Source::setPosition(float * v)
 	{
 		if(valid)

+ 3 - 1
src/modules/audio/openal/Source.h

@@ -87,8 +87,10 @@ namespace openal
 		virtual float getPitch() const;
 		virtual void setVolume(float volume);
 		virtual float getVolume() const;
+		virtual void seekAtomic(float offset, void * unit);
 		virtual void seek(float offset, Unit unit);
-		virtual float tell(Unit unit) const;
+		virtual float tellAtomic(void * unit) const;
+		virtual float tell(Unit unit);
 		virtual void setPosition(float * v);
 		virtual void getPosition(float * v) const;
 		virtual void setVelocity(float * v);