Fix Duration Update

This commit is contained in:
Flamefire committed 2014-05-07 20:19:55 +02:00
1 parent dd6503c911
commit 235ce266e5
1 file changed
+6 -27
@@ -30,10 +30,7 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
private volatile bool _IsFinished; private volatile bool _IsFinished;
private volatile float _Position; private volatile float _Position;
private volatile bool _QueryingDuration;
private System.Threading.Thread _QueryDurationThread;
private readonly object _DurationLock = new object(); //For atomic check-and-update of _QueryingDuration
private readonly object _ElementLock = new object(); //Hold this if you set _Element=null or when accessing _Element from outside the main thread private readonly object _ElementLock = new object(); //Hold this if you set _Element=null or when accessing _Element from outside the main thread
public override float Volume public override float Volume
@@ -60,12 +57,9 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
{ {
get get
{ {
if (base.Length < 0 && _QueryDurationThread == null && _Element != null) //TODO: Is it ok, to remove this? Gst should post a message if duration was changed so this should not be required
{ //if (base.Length < 0 && _Element != null)
// _QueryDurationThread does not get reset so it is created only once! // _UpdateDuration();
_QueryDurationThread = new System.Threading.Thread(_UpdateDuration) {Name = "GSt Update Duration"};
_QueryDurationThread.Start();
}
return base.Length >= 0f ? base.Length : 0f; return base.Length >= 0f ? base.Length : 0f;
} }
} }
@@ -180,12 +174,9 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
// Passing CLOCK_TIME_NONE here causes the pipeline to block for a long time so with // Passing CLOCK_TIME_NONE here causes the pipeline to block for a long time so with
// prescan enabled the pipeline will wait 500ms for stream to initialize and then continue // prescan enabled the pipeline will wait 500ms for stream to initialize and then continue
// if it takes more than 500ms, duration queries will be performed asynchronously // if it takes more than 500ms, duration queries will be performed asynchronously
if (prescan) Message msg = _Element.Bus.TimedPopFiltered(prescan ? ulong.MaxValue : 0L, MessageType.AsyncDone);
{
Message msg = _Element.Bus.TimedPopFiltered(0xffffffffffffffff, MessageType.AsyncDone);
if (msg.Handle != IntPtr.Zero) if (msg.Handle != IntPtr.Zero)
_UpdateDuration(); _UpdateDuration();
}
_FileOpened = true; _FileOpened = true;
return true; return true;
} }
@@ -273,27 +264,15 @@ namespace Vocaluxe.Lib.Sound.Playback.GstreamerSharp
} }
private void _UpdateDuration() private void _UpdateDuration()
{
if (_QueryingDuration)
return;
lock (_DurationLock)
{
if (_QueryingDuration)
return;
_QueryingDuration = true;
}
long duration = -1;
while (duration < 0 && !IsFinished)
{ {
lock (_ElementLock) lock (_ElementLock)
{ {
if (_Element == null) if (_Element == null)
break; return;
long duration;
if (_Element.QueryDuration(Format.Time, out duration)) if (_Element.QueryDuration(Format.Time, out duration))
Length = duration / (float)Constants.SECOND; Length = duration / (float)Constants.SECOND;
} }
} }
_QueryingDuration = false;
}
} }
} }