From 5bac5e98bfdce2159b4bbf0f5227239bf655b62d Mon Sep 17 00:00:00 2001 From: Stephan Sundermann Date: Sun, 22 Dec 2013 22:26:10 +0100 Subject: [PATCH] gstreamer-sharp: Query duration async --- Vocaluxe/Base/CBackgroundMusic.cs | 2 +- Vocaluxe/Lib/Sound/CGstreamerSharpAudio.cs | 78 ++++++++++++++----- Vocaluxe/Screens/CScreenSing.cs | 7 ++ .../Menu/SongMenu/CSongMenuTileBoard.cs | 32 ++++++-- 4 files changed, 89 insertions(+), 30 deletions(-) diff --git a/Vocaluxe/Base/CBackgroundMusic.cs b/Vocaluxe/Base/CBackgroundMusic.cs index d92118d3..48c173a0 100644 --- a/Vocaluxe/Base/CBackgroundMusic.cs +++ b/Vocaluxe/Base/CBackgroundMusic.cs @@ -198,7 +198,7 @@ namespace Vocaluxe.Base timeToPlay = _CurrentPlaylistElement.Finish - CSound.GetPosition(_CurrentMusicStream); bool finished = CSound.IsFinished(_CurrentMusicStream); - if (IsPlaying && (timeToPlay <= CSettings.BackgroundMusicFadeTime || finished)) + if (IsPlaying && ((timeToPlay <= CSettings.BackgroundMusicFadeTime && timeToPlay > 0) || finished)) { if (_RepeatSong) { diff --git a/Vocaluxe/Lib/Sound/CGstreamerSharpAudio.cs b/Vocaluxe/Lib/Sound/CGstreamerSharpAudio.cs index 6142e6f4..56ab9a53 100644 --- a/Vocaluxe/Lib/Sound/CGstreamerSharpAudio.cs +++ b/Vocaluxe/Lib/Sound/CGstreamerSharpAudio.cs @@ -169,7 +169,7 @@ namespace Vocaluxe.Lib.Sound public bool IsPaused(int stream) { if (!Streams.ContainsKey(stream)) - return true; + return false; return Streams[stream].Paused; } @@ -206,8 +206,8 @@ namespace Vocaluxe.Lib.Sound { private Element _Element; private bool _Loop; - public bool _Closed; - public bool _Finished; + public volatile bool _Closed; + public volatile bool _Finished; private bool _Fading; private bool _CloseStreamAfterFade; @@ -219,6 +219,10 @@ namespace Vocaluxe.Lib.Sound private float _Volume = 100f; public float _MaxVolume = 100f; + + private volatile float _Duration = -1f; + private volatile float _Position = 0f; + private volatile bool _QueryingDuration = false; public CGstreamerSharpAudioStream(string media, bool prescan) { var convert = ElementFactory.Make("audioconvert", "convert"); @@ -250,8 +254,14 @@ namespace Vocaluxe.Lib.Sound _Element["uri"] = new Uri(media).AbsoluteUri; _Element.SetState(State.Paused); - if (prescan) - _Element.Bus.TimedPopFiltered(0xffffffffffffffff, MessageType.AsyncDone); + // 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 + /*if (prescan) + { + var msg = _Element.Bus.TimedPopFiltered(100L * Constants.MSECOND, MessageType.DurationChanged); + if(msg.Handle != IntPtr.Zero) + UpdateDuration(); + }*/ } private void OnMessage(Message msg) @@ -269,7 +279,10 @@ namespace Vocaluxe.Lib.Sound string debug; msg.ParseError(out error, out debug); CLog.LogError("Gstreamer error: message" + error.Message + ", code" + error.Code + " ,debug information" + debug); - break; + break; + case MessageType.DurationChanged: + UpdateDuration(); + break; } msg.Unref(); } @@ -277,12 +290,21 @@ namespace Vocaluxe.Lib.Sound public void Close() { - if (_Element != null) + if (!_Closed) { - _Element.SetState(State.Null); + + _Closed = true; + _Finished = true; + var t = new System.Threading.Thread(TerminateStream); + t.Start(); } - _Closed = true; - _Finished = true; + + } + + private void TerminateStream() + { + if (_Element != null) + ; //_Element.SetState(State.Null); } public void Play(bool loop = false) @@ -356,11 +378,12 @@ namespace Vocaluxe.Lib.Sound { get { - long duration = 0; - if (_Element != null) - if (!_Element.QueryDuration(Format.Time, out duration)) - CLog.LogError("Could not query duration"); - return duration > 0 ? (duration / (long)Constants.SECOND) : 100; + if (_Duration < 0 && !_QueryingDuration) + { + var t = new System.Threading.Thread(UpdateDuration); + t.Start(); + } + return _Duration > 0 ? _Duration : -1; } } @@ -369,10 +392,11 @@ namespace Vocaluxe.Lib.Sound get { long position = 0; - if (_Element != null) - if (!_Element.QueryPosition(Format.Time, out position)) - CLog.LogError("Could not query position"); - return (float)(position / (double)Constants.SECOND); + if (!_Element.QueryPosition(Format.Time, out position)) + CLog.LogError("Could not query position"); + else + _Position = (float)(position / (double)Constants.SECOND); + return _Position; } set { @@ -385,7 +409,7 @@ namespace Vocaluxe.Lib.Sound { get { - return _Element != null ? _Element.CurrentState == State.Paused : true; + return _Element != null ? _Element.TargetState == State.Paused : true; } set { @@ -398,7 +422,7 @@ namespace Vocaluxe.Lib.Sound { get { - return _Element != null ? _Element.CurrentState == State.Playing : false; + return _Element != null ? _Element.TargetState == State.Playing : false; } set { @@ -436,6 +460,18 @@ namespace Vocaluxe.Lib.Sound } } } + + private void UpdateDuration() + { + _QueryingDuration = true; + long duration = -1; + while (duration < 0 && !_Closed && !_Finished && _Element != null) + { + if (_Element.QueryDuration(Format.Time, out duration)) + _Duration = (float)(duration / (long)Constants.SECOND); + } + _QueryingDuration = false; + } } } } diff --git a/Vocaluxe/Screens/CScreenSing.cs b/Vocaluxe/Screens/CScreenSing.cs index 16ba478a..d7c2e618 100644 --- a/Vocaluxe/Screens/CScreenSing.cs +++ b/Vocaluxe/Screens/CScreenSing.cs @@ -94,6 +94,7 @@ namespace Vocaluxe.Screens private int _CurrentBeat; private int _CurrentStream = -1; + private float _Length = -1f; //private int _NextStream = -1; private const float _Volume = 100f; private int _CurrentVideo = -1; @@ -417,6 +418,7 @@ namespace Vocaluxe.Screens _CurrentBeat = -100; _CurrentTime = 0f; _FinishTime = 0f; + _Length = -1f; _TimeToFirstNote = 0f; _TimeToFirstNoteDuet = 0f; _Pause = false; @@ -1302,6 +1304,9 @@ namespace Vocaluxe.Screens totalTime -= song.Start; float currentTime = _CurrentTime - song.Start; + if (_Length < 0 && totalTime > 0) + _PrepareTimeLine(); + if (totalTime <= 0f) return; @@ -1382,6 +1387,8 @@ namespace Vocaluxe.Screens if (totalTime <= 0f) return; + _Length = totalTime; + foreach (CVoice voice in song.Notes.Voices) { foreach (CSongLine line in voice.Lines.Where(line => line.VisibleInTimeLine)) diff --git a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs index faea98cc..cc8d6575 100644 --- a/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs +++ b/VocaluxeLib/Menu/SongMenu/CSongMenuTileBoard.cs @@ -58,6 +58,8 @@ namespace VocaluxeLib.Menu.SongMenu private bool _SmallView; + private float _Length = -1f; + public CSongMenuTileBoard(int partyModeID) : base(partyModeID) {} @@ -132,6 +134,9 @@ namespace VocaluxeLib.Menu.SongMenu for (int i = 0; i < _Tiles.Count; i++) _Tiles[i].Selected = _Locked == i + _Offset; } + + if (_Length < 0 && CBase.Sound.GetLength(_PreviewSongStream) > 0 && CBase.Songs.IsInCategory()) + UpdateLength(CBase.Songs.GetVisibleSong(_PreviewId)); } private void _UpdatePreview() @@ -145,6 +150,7 @@ namespace VocaluxeLib.Menu.SongMenu _VideoIcon.Visible = false; _MedleyCalcIcon.Visible = false; _MedleyTagIcon.Visible = false; + _Length = -1f; //Check if nothing is selected (for preview) if (_PreviewId < 0) @@ -173,14 +179,7 @@ namespace VocaluxeLib.Menu.SongMenu _MedleyCalcIcon.Visible = song.Medley.Source == EDataSource.Calculated; _MedleyTagIcon.Visible = song.Medley.Source == EDataSource.Tag; - float time = CBase.Sound.GetLength(_PreviewSongStream); - if (Math.Abs(song.Finish) > 0.001) - time = song.Finish; - - time -= song.Start; - var min = (int)Math.Floor(time / 60f); - var sec = (int)(time - min * 60f); - _SongLength.Text = min.ToString("00") + ":" + sec.ToString("00"); + UpdateLength(song); } else { @@ -197,6 +196,23 @@ namespace VocaluxeLib.Menu.SongMenu } } + private void UpdateLength(CSong song) + { + float time = CBase.Sound.GetLength(_PreviewSongStream); + if (Math.Abs(song.Finish) > 0.001) + time = song.Finish; + + // The audiobackend is not yet ready to return the length + if (time > 0) + { + time -= song.Start; + var min = (int)Math.Floor(time / 60f); + var sec = (int)(time - min * 60f); + _SongLength.Text = min.ToString("00") + ":" + sec.ToString("00"); + _Length = time; + } + } + public override void OnShow() { if (CBase.Songs.GetTabs() == EOffOn.TR_CONFIG_OFF && CBase.Songs.GetNumCategories() > 0 && !CBase.Songs.IsInCategory())