From b5ed2e65f8f5572f8a0f1a39f691e37f699bbe12 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 27 Nov 2015 06:01:50 -0800 Subject: [PATCH] Add a method to get the sound stream delay This helps avoid a lock during the movie player's read method, since it needs to sync with the current playback position which would otherwise need to get the movie decoder's current position. --- apps/openmw/mwsound/movieaudiofactory.cpp | 3 ++- apps/openmw/mwsound/openal_output.cpp | 21 +++++++++++++++++++++ apps/openmw/mwsound/sound.hpp | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwsound/movieaudiofactory.cpp b/apps/openmw/mwsound/movieaudiofactory.cpp index 47889051ae..925c966da4 100644 --- a/apps/openmw/mwsound/movieaudiofactory.cpp +++ b/apps/openmw/mwsound/movieaudiofactory.cpp @@ -60,7 +60,8 @@ namespace MWSound virtual double getAudioClock() { - return mAudioTrack->getTimeOffset(); + return (double)getSampleOffset()/(double)mAVStream->codec->sample_rate - + mAudioTrack->getStreamDelay(); } virtual void adjustAudioSettings(AVSampleFormat& sampleFormat, uint64_t& channelLayout, int& sampleRate) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 87667019e1..dd440674f0 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -188,6 +188,7 @@ public: virtual void stop(); virtual bool isPlaying(); virtual double getTimeOffset(); + virtual double getStreamDelay() const; virtual void applyUpdates(); void play(); @@ -395,6 +396,26 @@ double OpenAL_SoundStream::getTimeOffset() return t; } +double OpenAL_SoundStream::getStreamDelay() const +{ + ALint state = AL_STOPPED; + double d = 0.0; + ALint offset; + + alGetSourcei(mSource, AL_SAMPLE_OFFSET, &offset); + alGetSourcei(mSource, AL_SOURCE_STATE, &state); + if(state == AL_PLAYING || state == AL_PAUSED) + { + ALint queued; + alGetSourcei(mSource, AL_BUFFERS_QUEUED, &queued); + ALint inqueue = mBufferSize/mFrameSize*queued - offset; + d = (double)inqueue / (double)mSampleRate; + } + + throwALerror(); + return d; +} + void OpenAL_SoundStream::updateAll(bool local) { alSourcef(mSource, AL_REFERENCE_DISTANCE, mMinDistance); diff --git a/apps/openmw/mwsound/sound.hpp b/apps/openmw/mwsound/sound.hpp index 944fbc0327..f95ff169d0 100644 --- a/apps/openmw/mwsound/sound.hpp +++ b/apps/openmw/mwsound/sound.hpp @@ -25,6 +25,7 @@ namespace MWSound virtual void stop() = 0; virtual bool isPlaying() = 0; virtual double getTimeOffset() = 0; + virtual double getStreamDelay() const { return 0.0; } virtual void applyUpdates() = 0; void setPosition(const osg::Vec3f &pos) { mPos = pos; } void setVolume(float volume) { mVolume = volume; }