Merge pull request #325 from lukeIam/PACloaseStreamHandling

PortAudio close-stream-handling
This commit is contained in:
Florian Küpper authored and GitHub committed 2017-10-30 18:28:43 +01:00
commit c87016c4f8
1 file changed
+21 -3
+21 -3
View File
@@ -74,13 +74,24 @@ namespace Vocaluxe.Lib.Sound
} }
lock (_Mutex) lock (_Mutex)
{ {
if (_Disposed)
return;
Debug.Assert(_RefCount > 0); Debug.Assert(_RefCount > 0);
_RefCount--; _RefCount--;
if (_RefCount == 0) if (_RefCount == 0)
{
try
{
PortAudio.Pa_Terminate(); PortAudio.Pa_Terminate();
} }
catch (Exception ex)
{
CLog.LogError(errorText: $"Error disposing PortAudio: {ex.Message}", e: ex);
}
}
_Disposed = true; _Disposed = true;
} }
}
public void Dispose() public void Dispose()
{ {
@@ -99,12 +110,12 @@ namespace Vocaluxe.Lib.Sound
public PortAudio.PaError OpenStream(out IntPtr stream, ref PortAudio.PaStreamParameters? inputParameters, ref PortAudio.PaStreamParameters? outputParameters, public PortAudio.PaError OpenStream(out IntPtr stream, ref PortAudio.PaStreamParameters? inputParameters, ref PortAudio.PaStreamParameters? outputParameters,
double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags, double sampleRate, uint framesPerBuffer, PortAudio.PaStreamFlags streamFlags,
PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData) PortAudio.PaStreamCallbackDelegate streamCallback, IntPtr userData)
{
lock (_Mutex)
{ {
if (_Disposed) if (_Disposed)
throw new ObjectDisposedException("PortAudioHandle already disposed"); throw new ObjectDisposedException("PortAudioHandle already disposed");
lock (_Mutex)
{
PortAudio.PaError res = PortAudio.Pa_OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback, PortAudio.PaError res = PortAudio.Pa_OpenStream(out stream, ref inputParameters, ref outputParameters, sampleRate, framesPerBuffer, streamFlags, streamCallback,
userData); userData);
if (res == PortAudio.PaError.paNoError) if (res == PortAudio.PaError.paNoError)
@@ -156,13 +167,20 @@ namespace Vocaluxe.Lib.Sound
} }
public void CloseStream(IntPtr stream) public void CloseStream(IntPtr stream)
{
lock (_Mutex)
{ {
if (_Disposed) if (_Disposed)
throw new ObjectDisposedException("PortAudioHandle already disposed"); throw new ObjectDisposedException("PortAudioHandle already disposed");
lock (_Mutex) try
{ {
PortAudio.Pa_CloseStream(stream); PortAudio.Pa_CloseStream(stream);
}
catch (Exception ex)
{
CLog.LogError(errorText:$"Error closing stream: {ex.Message}", e:ex);
}
_Streams.Remove(stream); _Streams.Remove(stream);
} }
} }