Add Close() function to IPlayback to fully close the driver (and block till done)
This commit is contained in:
1 parent
af941e2ecc
commit
6a51e63be4
9 files changed
+93
-20
No files matched your search
@@ -83,7 +83,8 @@ namespace Vocaluxe.Base
|
|||||||
|
|
||||||
public static void Close()
|
public static void Close()
|
||||||
{
|
{
|
||||||
CloseAllStreams();
|
if (_Playback != null)
|
||||||
|
_Playback.Close();
|
||||||
_Playback = null;
|
_Playback = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,11 @@ namespace Vocaluxe.Lib.Sound.Playback.Gstreamer
|
|||||||
return CGstreamerAudioWrapper.Init();
|
return CGstreamerAudioWrapper.Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
CloseAll();
|
||||||
|
}
|
||||||
|
|
||||||
public void SetGlobalVolume(float volume)
|
public void SetGlobalVolume(float volume)
|
||||||
{
|
{
|
||||||
CGstreamerAudioWrapper.SetGlobalVolume(volume);
|
CGstreamerAudioWrapper.SetGlobalVolume(volume);
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
|
|||||||
|
|
||||||
public bool Init()
|
public bool Init()
|
||||||
{
|
{
|
||||||
|
if (Application.IsInitialized)
|
||||||
|
return false;
|
||||||
#if ARCH_X86
|
#if ARCH_X86
|
||||||
const string path = ".\\x86\\gstreamer";
|
const string path = ".\\x86\\gstreamer";
|
||||||
#endif
|
#endif
|
||||||
@@ -41,6 +43,14 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
|
|||||||
return Application.IsInitialized;
|
return Application.IsInitialized;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
if (!Application.IsInitialized)
|
||||||
|
return;
|
||||||
|
CloseAll();
|
||||||
|
Application.Deinit();
|
||||||
|
}
|
||||||
|
|
||||||
public void SetGlobalVolume(float volume)
|
public void SetGlobalVolume(float volume)
|
||||||
{
|
{
|
||||||
foreach (CGstreamerSharpAudioStream stream in _Streams.Values)
|
foreach (CGstreamerSharpAudioStream stream in _Streams.Values)
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ namespace Vocaluxe.Lib.Sound.Playback
|
|||||||
interface IPlayback
|
interface IPlayback
|
||||||
{
|
{
|
||||||
bool Init();
|
bool Init();
|
||||||
|
void Close();
|
||||||
void SetGlobalVolume(float volume);
|
void SetGlobalVolume(float volume);
|
||||||
int GetStreamCount();
|
int GetStreamCount();
|
||||||
void CloseAll();
|
void CloseAll();
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using OpenTK.Audio;
|
using OpenTK.Audio;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -34,10 +35,15 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
|||||||
|
|
||||||
private List<SAudioStreams> _Streams;
|
private List<SAudioStreams> _Streams;
|
||||||
|
|
||||||
|
~COpenALPlay()
|
||||||
|
{
|
||||||
|
_Dispose(false);
|
||||||
|
}
|
||||||
|
|
||||||
public bool Init()
|
public bool Init()
|
||||||
{
|
{
|
||||||
if (_Initialized)
|
if (_Initialized)
|
||||||
CloseAll();
|
return false;
|
||||||
|
|
||||||
_Context = new AudioContext();
|
_Context = new AudioContext();
|
||||||
|
|
||||||
@@ -51,6 +57,11 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
public void CloseAll()
|
public void CloseAll()
|
||||||
{
|
{
|
||||||
lock (_MutexDecoder)
|
lock (_MutexDecoder)
|
||||||
@@ -321,8 +332,8 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
|||||||
|
|
||||||
private void _CloseProc(int streamID)
|
private void _CloseProc(int streamID)
|
||||||
{
|
{
|
||||||
if (_Initialized)
|
if (!_Initialized)
|
||||||
{
|
return;
|
||||||
lock (_MutexDecoder)
|
lock (_MutexDecoder)
|
||||||
{
|
{
|
||||||
if (_AlreadyAdded(streamID))
|
if (_AlreadyAdded(streamID))
|
||||||
@@ -333,13 +344,26 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_Dispose(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void _Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
CloseAll();
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
while (_Decoder.Count > 0)
|
||||||
|
Thread.Sleep(5);
|
||||||
|
}
|
||||||
|
if (_Context != null)
|
||||||
{
|
{
|
||||||
_Context.Dispose();
|
_Context.Dispose();
|
||||||
_Context = null;
|
_Context = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void FadeAndStop(int stream, float targetVolume, float seconds)
|
public void FadeAndStop(int stream, float targetVolume, float seconds)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ using VocaluxeLib;
|
|||||||
|
|
||||||
namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
||||||
{
|
{
|
||||||
class COpenAlStream : IDisposable
|
class COpenAlStream
|
||||||
{
|
{
|
||||||
private const int _BufferSize = 2048;
|
private const int _BufferSize = 2048;
|
||||||
private const int _BufferCount = 5;
|
private const int _BufferCount = 5;
|
||||||
@@ -70,6 +70,7 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
|||||||
_StreamID = streamID;
|
_StreamID = streamID;
|
||||||
_Terminated = true;
|
_Terminated = true;
|
||||||
_CloseMutex = closeMutex;
|
_CloseMutex = closeMutex;
|
||||||
|
_EventDecode.Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Length
|
public float Length
|
||||||
@@ -372,6 +373,11 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
|||||||
}
|
}
|
||||||
|
|
||||||
_Closeproc(_StreamID);
|
_Closeproc(_StreamID);
|
||||||
|
if (_EventDecode != null)
|
||||||
|
{
|
||||||
|
_EventDecode.Dispose();
|
||||||
|
_EventDecode = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endregion Threading
|
#endregion Threading
|
||||||
|
|
||||||
@@ -464,11 +470,5 @@ namespace Vocaluxe.Lib.Sound.Playback.OpenAL
|
|||||||
_CurrentTime = _TimeCode - _Data.BytesNotRead / _BytesPerSecond - 0.1f;
|
_CurrentTime = _TimeCode - _Data.BytesNotRead / _BytesPerSecond - 0.1f;
|
||||||
_Timer.Restart();
|
_Timer.Restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
|
||||||
{
|
|
||||||
_EventDecode.Close();
|
|
||||||
_EventDecode = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,29 +21,43 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
|
|||||||
{
|
{
|
||||||
class CPortAudioPlay : IPlayback
|
class CPortAudioPlay : IPlayback
|
||||||
{
|
{
|
||||||
private bool _Initialized;
|
private volatile bool _Initialized;
|
||||||
private readonly List<CPortAudioStream> _Streams = new List<CPortAudioStream>();
|
private readonly List<CPortAudioStream> _Streams = new List<CPortAudioStream>();
|
||||||
private int _NextID;
|
private int _NextID;
|
||||||
|
|
||||||
public bool Init()
|
public bool Init()
|
||||||
{
|
{
|
||||||
if (_Initialized)
|
if (_Initialized)
|
||||||
CloseAll();
|
return false;
|
||||||
|
|
||||||
_Initialized = true;
|
_Initialized = true;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Close()
|
||||||
|
{
|
||||||
|
_Initialized = false; //Set this first, so threads don't call closeproc anymore
|
||||||
|
List<CPortAudioStream> streams = new List<CPortAudioStream>();
|
||||||
|
//Get all streams but do not modify list without holding the lock
|
||||||
|
//Calling dispose from within the lock may deadlock in closeproc (if a thread is already there)
|
||||||
|
lock (_Streams)
|
||||||
|
{
|
||||||
|
streams.AddRange(_Streams);
|
||||||
|
_Streams.Clear();
|
||||||
|
}
|
||||||
|
foreach (CPortAudioStream stream in streams)
|
||||||
|
stream.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
public void CloseAll()
|
public void CloseAll()
|
||||||
{
|
{
|
||||||
lock (_Streams)
|
lock (_Streams)
|
||||||
{
|
{
|
||||||
foreach (CPortAudioStream stream in _Streams)
|
foreach (CPortAudioStream stream in _Streams)
|
||||||
stream.Free();
|
stream.Close();
|
||||||
_Streams.Clear();
|
_Streams.Clear();
|
||||||
}
|
}
|
||||||
_Initialized = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetGlobalVolume(float volume)
|
public void SetGlobalVolume(float volume)
|
||||||
@@ -99,7 +113,7 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
|
|||||||
int index = _GetStreamIndex(stream);
|
int index = _GetStreamIndex(stream);
|
||||||
if (index >= 0)
|
if (index >= 0)
|
||||||
{
|
{
|
||||||
_Streams[index].Free();
|
_Streams[index].Close();
|
||||||
_Streams.RemoveAt(index);
|
_Streams.RemoveAt(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,12 +68,15 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
|
|||||||
|
|
||||||
~CPortAudioStream()
|
~CPortAudioStream()
|
||||||
{
|
{
|
||||||
Dispose();
|
_Dispose(false);
|
||||||
|
if (_DecoderThread != null)
|
||||||
|
_DecoderThread.Join();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Free()
|
public void Close()
|
||||||
{
|
{
|
||||||
_Terminated = true;
|
_Terminated = true;
|
||||||
|
_EventDecode.Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Length
|
public float Length
|
||||||
@@ -512,11 +515,25 @@ namespace Vocaluxe.Lib.Sound.Playback.PortAudio
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
_Dispose(true);
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void _Dispose(bool disposing)
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
if (disposing)
|
||||||
|
{
|
||||||
|
if (_DecoderThread != null)
|
||||||
|
{
|
||||||
|
_DecoderThread.Join();
|
||||||
|
_DecoderThread = null;
|
||||||
|
}
|
||||||
if (_EventDecode != null)
|
if (_EventDecode != null)
|
||||||
{
|
{
|
||||||
_EventDecode.Close();
|
_EventDecode.Close();
|
||||||
_EventDecode = null;
|
_EventDecode = null;
|
||||||
GC.SuppressFinalize(this);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -268,6 +268,7 @@ namespace Vocaluxe
|
|||||||
CDraw.Unload();
|
CDraw.Unload();
|
||||||
CDataBase.Close();
|
CDataBase.Close();
|
||||||
CWebcam.Close();
|
CWebcam.Close();
|
||||||
|
GC.Collect(); // Do a GC run here before we close logs to have finalizers run
|
||||||
CLog.CloseAll();
|
CLog.CloseAll();
|
||||||
}
|
}
|
||||||
catch (Exception) {}
|
catch (Exception) {}
|
||||||
|
|||||||
Reference in new issue
Block a user