gstreamer-sharp: code cleanup

This commit is contained in:
Stephan Sundermann authored and Florian Küpper committed 2013-12-26 13:05:33 +01:00
1 parent 161e255367
commit eb54ccf5a8
1 file changed
+74 -77
+74 -77
View File
@@ -1,7 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gst; using Gst;
using GLib; using GLib;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@@ -13,20 +11,19 @@ namespace Vocaluxe.Lib.Sound
{ {
public class CGstreamerSharpAudio : IPlayback public class CGstreamerSharpAudio : IPlayback
{ {
private Dictionary<int, CGstreamerSharpAudioStream> Streams = new Dictionary<int, CGstreamerSharpAudioStream>(); private readonly Dictionary<int, CGstreamerSharpAudioStream> _Streams = new Dictionary<int, CGstreamerSharpAudioStream>();
private static int idCount; private static int _IDCount;
[DllImport("kernel32.dll", CharSet = CharSet.Auto)] [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern void SetDllDirectory(string lpPathName); private static extern void SetDllDirectory(string lpPathName);
public bool Init() public bool Init()
{ {
string path;
#if ARCH_X86 #if ARCH_X86
path = ".\\x86\\gstreamer"; string path = ".\\x86\\gstreamer";
#endif #endif
#if ARCH_X64 #if ARCH_X64
path = ".\\x64\\gstreamer"; string path = ".\\x64\\gstreamer";
#endif #endif
SetDllDirectory(path); SetDllDirectory(path);
Application.Init(); Application.Init();
@@ -38,7 +35,7 @@ namespace Vocaluxe.Lib.Sound
public void SetGlobalVolume(float volume) public void SetGlobalVolume(float volume)
{ {
foreach (CGstreamerSharpAudioStream stream in Streams.Values) foreach (CGstreamerSharpAudioStream stream in _Streams.Values)
{ {
stream.Volume = volume; stream.Volume = volume;
} }
@@ -46,12 +43,12 @@ namespace Vocaluxe.Lib.Sound
public int GetStreamCount() public int GetStreamCount()
{ {
return Streams.Count; return _Streams.Count;
} }
public void CloseAll() public void CloseAll()
{ {
foreach (CGstreamerSharpAudioStream stream in Streams.Values) foreach (CGstreamerSharpAudioStream stream in _Streams.Values)
{ {
stream.Close(); stream.Close();
} }
@@ -65,172 +62,172 @@ namespace Vocaluxe.Lib.Sound
public int Load(string media, bool prescan) public int Load(string media, bool prescan)
{ {
var stream = new CGstreamerSharpAudioStream (media, prescan); var stream = new CGstreamerSharpAudioStream (media, prescan);
Streams[idCount] = stream; _Streams[_IDCount] = stream;
return idCount++; return _IDCount++;
} }
public void Close(int stream) public void Close(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].Close(); _Streams[stream].Close();
} }
public void Play(int stream) public void Play(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].Playing = true; _Streams[stream].Playing = true;
} }
public void Play(int stream, bool loop) public void Play(int stream, bool loop)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Play(stream); Play(stream, loop);
} }
public void Pause(int stream) public void Pause(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].Paused = true; _Streams[stream].Paused = true;
} }
public void Stop(int stream) public void Stop(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].Stop(); _Streams[stream].Stop();
} }
public void Fade(int stream, float targetVolume, float seconds) public void Fade(int stream, float targetVolume, float seconds)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].Fade(targetVolume, seconds); _Streams[stream].Fade(targetVolume, seconds);
} }
public void FadeAndPause(int stream, float targetVolume, float seconds) public void FadeAndPause(int stream, float targetVolume, float seconds)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].FadeAndPause(targetVolume, seconds); _Streams[stream].FadeAndPause(targetVolume, seconds);
} }
public void FadeAndClose(int stream, float targetVolume, float seconds) public void FadeAndClose(int stream, float targetVolume, float seconds)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].FadeAndClose(targetVolume, seconds); _Streams[stream].FadeAndClose(targetVolume, seconds);
} }
public void FadeAndStop(int stream, float targetVolume, float seconds) public void FadeAndStop(int stream, float targetVolume, float seconds)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].FadeAndStop(targetVolume, seconds); _Streams[stream].FadeAndStop(targetVolume, seconds);
} }
public void SetStreamVolume(int stream, float volume) public void SetStreamVolume(int stream, float volume)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].Volume = volume; _Streams[stream].Volume = volume;
} }
public void SetStreamVolumeMax(int stream, float volume) public void SetStreamVolumeMax(int stream, float volume)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].MaxVolume = volume; _Streams[stream].MaxVolume = volume;
} }
public float GetLength(int stream) public float GetLength(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return -1; return -1;
return Streams[stream].Length; return _Streams[stream].Length;
} }
public float GetPosition(int stream) public float GetPosition(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return -1f; return -1f;
return Streams[stream].Position; return _Streams[stream].Position;
} }
public bool IsPlaying(int stream) public bool IsPlaying(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return false; return false;
return Streams[stream].Playing; return _Streams[stream].Playing;
} }
public bool IsPaused(int stream) public bool IsPaused(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return false; return false;
return Streams[stream].Paused; return _Streams[stream].Paused;
} }
public bool IsFinished(int stream) public bool IsFinished(int stream)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return true; return true;
return Streams[stream]._Finished; return _Streams[stream].Finished;
} }
public void SetPosition(int stream, float position) public void SetPosition(int stream, float position)
{ {
if (!Streams.ContainsKey(stream)) if (!_Streams.ContainsKey(stream))
return; return;
Streams[stream].Position = position; _Streams[stream].Position = position;
} }
public void Update() public void Update()
{ {
var streamsToDelete = new List<int>(); var streamsToDelete = new List<int>();
foreach (KeyValuePair<int,CGstreamerSharpAudioStream> stream in Streams) foreach (KeyValuePair<int,CGstreamerSharpAudioStream> stream in _Streams)
{ {
if (stream.Value._Closed) if (stream.Value.Closed)
streamsToDelete.Add(stream.Key); streamsToDelete.Add(stream.Key);
stream.Value.Update(); stream.Value.Update();
} }
foreach (int key in streamsToDelete) foreach (int key in streamsToDelete)
{ {
Streams.Remove(key); _Streams.Remove(key);
} }
} }
private class CGstreamerSharpAudioStream private class CGstreamerSharpAudioStream
{ {
private Element _Element; private readonly Element _Element;
private bool _Loop; private bool _Loop;
public volatile bool _Closed; public volatile bool Closed;
public volatile bool _Finished; public volatile bool Finished;
private bool _Fading; private bool _Fading;
private bool _CloseStreamAfterFade; private bool _CloseStreamAfterFade;
private bool _PauseStreamAfterFade; private bool _PauseStreamAfterFade;
private bool _StopStreamAfterFade; private bool _StopStreamAfterFade;
private Stopwatch _FadeTimer = new Stopwatch(); private readonly Stopwatch _FadeTimer = new Stopwatch();
private float _FadeTime; private float _FadeTime;
private float _FadeVolume; private float _FadeVolume;
private float _Volume = 100f; private float _Volume = 100f;
public float _MaxVolume = 100f; private float _MaxVolume = 100f;
private volatile float _Duration = -1f; private volatile float _Duration = -1f;
private volatile float _Position = 0f; private volatile float _Position;
private volatile bool _QueryingDuration = false; private volatile bool _QueryingDuration;
public CGstreamerSharpAudioStream(string media, bool prescan) public CGstreamerSharpAudioStream(string media, bool prescan)
{ {
var convert = ElementFactory.Make("audioconvert", "convert"); var convert = ElementFactory.Make("audioconvert", "convert");
var audiosink = ElementFactory.Make("directsoundsink", "audiosink"); var audiosink = ElementFactory.Make("directsoundsink", "audiosink");
var audioSinkBin = new Bin("Audiosink"); var audioSinkBin = new Bin("Audiosink");
if (convert == null || audiosink == null || audioSinkBin == null) if (convert == null || audiosink == null)
CLog.LogError("Could not create pipeline"); CLog.LogError("Could not create pipeline");
audioSinkBin.Add(convert); audioSinkBin.Add(convert);
@@ -239,7 +236,7 @@ namespace Vocaluxe.Lib.Sound
Pad pad = convert.GetStaticPad("sink"); Pad pad = convert.GetStaticPad("sink");
GhostPad ghostpad = new GhostPad("sink", pad); GhostPad ghostpad = new GhostPad("sink", pad);
if (pad == null || ghostpad == null) if (pad == null)
CLog.LogError("Could not create pads"); CLog.LogError("Could not create pads");
if (!ghostpad.SetActive(true)) if (!ghostpad.SetActive(true))
@@ -262,11 +259,11 @@ namespace Vocaluxe.Lib.Sound
{ {
var msg = _Element.Bus.TimedPopFiltered(500 * Constants.MSECOND, MessageType.AsyncDone); var msg = _Element.Bus.TimedPopFiltered(500 * Constants.MSECOND, MessageType.AsyncDone);
if(msg.Handle != IntPtr.Zero) if(msg.Handle != IntPtr.Zero)
UpdateDuration(); _UpdateDuration();
} }
} }
private void OnMessage(Message msg) private void _OnMessage(Message msg)
{ {
if (msg.Handle == IntPtr.Zero) if (msg.Handle == IntPtr.Zero)
return; return;
@@ -276,7 +273,7 @@ namespace Vocaluxe.Lib.Sound
if (_Loop) if (_Loop)
Position = 0; Position = 0;
else else
_Finished = true; Finished = true;
Close(); Close();
break; break;
case MessageType.Error: case MessageType.Error:
@@ -286,7 +283,7 @@ namespace Vocaluxe.Lib.Sound
CLog.LogError("Gstreamer error: message" + error.Message + ", code" + error.Code + " ,debug information" + debug); CLog.LogError("Gstreamer error: message" + error.Message + ", code" + error.Code + " ,debug information" + debug);
break; break;
case MessageType.DurationChanged: case MessageType.DurationChanged:
UpdateDuration(); _UpdateDuration();
break; break;
} }
msg.Unref(); msg.Unref();
@@ -295,18 +292,18 @@ namespace Vocaluxe.Lib.Sound
public void Close() public void Close()
{ {
if (!_Closed) if (!Closed)
{ {
_Closed = true; Closed = true;
_Finished = true; Finished = true;
var t = new Thread(TerminateStream); var t = new Thread(_TerminateStream);
t.Start(); t.Start();
} }
} }
private void TerminateStream() private void _TerminateStream()
{ {
if (_Element != null) if (_Element != null)
{ {
@@ -319,7 +316,7 @@ namespace Vocaluxe.Lib.Sound
{ {
if (_Element != null) if (_Element != null)
_Element.SetState(State.Playing); _Element.SetState(State.Playing);
this._Loop = loop; _Loop = loop;
} }
public void Stop() public void Stop()
@@ -365,7 +362,7 @@ namespace Vocaluxe.Lib.Sound
{ {
_Volume = value; _Volume = value;
if (_Element != null) if (_Element != null)
_Element["volume"] = (double) ((value / 100d) * (_MaxVolume / 100d)); _Element["volume"] = ((value / 100d) * (_MaxVolume / 100d));
} }
} }
@@ -388,7 +385,7 @@ namespace Vocaluxe.Lib.Sound
{ {
if (_Duration < 0 && !_QueryingDuration) if (_Duration < 0 && !_QueryingDuration)
{ {
var t = new Thread(UpdateDuration); var t = new Thread(_UpdateDuration);
t.Start(); t.Start();
} }
return _Duration > 0 ? _Duration : -1; return _Duration > 0 ? _Duration : -1;
@@ -399,7 +396,7 @@ namespace Vocaluxe.Lib.Sound
{ {
get get
{ {
long position = 0; long position;
if (!_Element.QueryPosition(Format.Time, out position)) if (!_Element.QueryPosition(Format.Time, out position))
CLog.LogError("Could not query position"); CLog.LogError("Could not query position");
else else
@@ -417,7 +414,7 @@ namespace Vocaluxe.Lib.Sound
{ {
get get
{ {
return _Element != null ? _Element.TargetState == State.Paused : true; return _Element == null || _Element.TargetState == State.Paused;
} }
set set
{ {
@@ -430,7 +427,7 @@ namespace Vocaluxe.Lib.Sound
{ {
get get
{ {
return _Element != null ? _Element.TargetState == State.Playing && !_Finished : false; return _Element != null && (_Element.TargetState == State.Playing && !Finished);
} }
set set
{ {
@@ -443,7 +440,7 @@ namespace Vocaluxe.Lib.Sound
{ {
while (_Element != null && _Element.Bus != null && _Element.Bus.HavePending()) while (_Element != null && _Element.Bus != null && _Element.Bus.HavePending())
{ {
OnMessage(_Element.Bus.Pop()); _OnMessage(_Element.Bus.Pop());
} }
if (_Fading) if (_Fading)
@@ -469,15 +466,15 @@ namespace Vocaluxe.Lib.Sound
} }
} }
private void UpdateDuration() private void _UpdateDuration()
{ {
_QueryingDuration = true; _QueryingDuration = true;
long duration = -1; long duration = -1;
while (duration < 0 && !_Closed && !_Finished && _Element != null) while (duration < 0 && !Closed && !Finished && _Element != null)
{ {
if (_Element.QueryDuration(Format.Time, out duration)) if (_Element.QueryDuration(Format.Time, out duration))
{ {
_Duration = (float)(duration / (long)Constants.SECOND); _Duration = duration / (float)Constants.SECOND;
_QueryingDuration = false; _QueryingDuration = false;
} }
} }