From de30a0e10bdc05a1ac44ec84e4a2be154a647236 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 29 Jan 2016 14:52:39 -0500 Subject: [PATCH] remove volume timer from device --- MediaBrowser.Dlna/PlayTo/Device.cs | 79 ++++++++++++++++-------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/MediaBrowser.Dlna/PlayTo/Device.cs b/MediaBrowser.Dlna/PlayTo/Device.cs index 222a52736..1ec7a4ce0 100644 --- a/MediaBrowser.Dlna/PlayTo/Device.cs +++ b/MediaBrowser.Dlna/PlayTo/Device.cs @@ -22,14 +22,26 @@ namespace MediaBrowser.Dlna.PlayTo #region Fields & Properties private Timer _timer; - private Timer _volumeTimer; public DeviceInfo Properties { get; set; } private int _muteVol; public bool IsMuted { get; set; } - public int Volume { get; set; } + private int _volume; + + public int Volume + { + get + { + RefreshVolumeIfNeeded(); + return _volume; + } + set + { + _volume = value; + } + } public TimeSpan? Duration { get; set; } @@ -93,11 +105,6 @@ namespace MediaBrowser.Dlna.PlayTo return 1000; } - private int GetVolumeTimerIntervalMs() - { - return 5000; - } - private int GetInactiveTimerIntervalMs() { return 20000; @@ -107,11 +114,37 @@ namespace MediaBrowser.Dlna.PlayTo { _timer = new Timer(TimerCallback, null, GetPlaybackTimerIntervalMs(), GetInactiveTimerIntervalMs()); - _volumeTimer = new Timer(VolumeTimerCallback, null, Timeout.Infinite, Timeout.Infinite); - _timerActive = false; } + private DateTime _lastVolumeRefresh; + private void RefreshVolumeIfNeeded() + { + if (!_timerActive) + { + return; + } + + if (DateTime.UtcNow >= _lastVolumeRefresh.AddSeconds(5)) + { + _lastVolumeRefresh = DateTime.UtcNow; + RefreshVolume(); + } + } + + private async void RefreshVolume() + { + try + { + await GetVolume().ConfigureAwait(false); + await GetMute().ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.ErrorException("Error updating device volume info for {0}", ex, Properties.Name); + } + } + private readonly object _timerLock = new object(); private bool _timerActive; private void RestartTimer() @@ -124,7 +157,6 @@ namespace MediaBrowser.Dlna.PlayTo { _logger.Debug("RestartTimer"); _timer.Change(10, GetPlaybackTimerIntervalMs()); - _volumeTimer.Change(100, GetVolumeTimerIntervalMs()); } _timerActive = true; @@ -150,10 +182,6 @@ namespace MediaBrowser.Dlna.PlayTo { _timer.Change(interval, interval); } - if (_volumeTimer != null) - { - _volumeTimer.Change(Timeout.Infinite, Timeout.Infinite); - } } _timerActive = false; @@ -440,19 +468,6 @@ namespace MediaBrowser.Dlna.PlayTo } } - private async void VolumeTimerCallback(object sender) - { - try - { - await GetVolume().ConfigureAwait(false); - await GetMute().ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error updating device volume info for {0}", ex, Properties.Name); - } - } - private async Task GetVolume() { var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume"); @@ -1012,7 +1027,6 @@ namespace MediaBrowser.Dlna.PlayTo _disposed = true; DisposeTimer(); - DisposeVolumeTimer(); } } @@ -1025,15 +1039,6 @@ namespace MediaBrowser.Dlna.PlayTo } } - private void DisposeVolumeTimer() - { - if (_volumeTimer != null) - { - _volumeTimer.Dispose(); - _volumeTimer = null; - } - } - #endregion public override string ToString()