remove volume timer from device

This commit is contained in:
Luke Pulverenti 2016-01-29 14:52:39 -05:00
parent 24b925cb56
commit de30a0e10b

View File

@ -22,14 +22,26 @@ namespace MediaBrowser.Dlna.PlayTo
#region Fields & Properties #region Fields & Properties
private Timer _timer; private Timer _timer;
private Timer _volumeTimer;
public DeviceInfo Properties { get; set; } public DeviceInfo Properties { get; set; }
private int _muteVol; private int _muteVol;
public bool IsMuted { get; set; } 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; } public TimeSpan? Duration { get; set; }
@ -93,11 +105,6 @@ namespace MediaBrowser.Dlna.PlayTo
return 1000; return 1000;
} }
private int GetVolumeTimerIntervalMs()
{
return 5000;
}
private int GetInactiveTimerIntervalMs() private int GetInactiveTimerIntervalMs()
{ {
return 20000; return 20000;
@ -107,11 +114,37 @@ namespace MediaBrowser.Dlna.PlayTo
{ {
_timer = new Timer(TimerCallback, null, GetPlaybackTimerIntervalMs(), GetInactiveTimerIntervalMs()); _timer = new Timer(TimerCallback, null, GetPlaybackTimerIntervalMs(), GetInactiveTimerIntervalMs());
_volumeTimer = new Timer(VolumeTimerCallback, null, Timeout.Infinite, Timeout.Infinite);
_timerActive = false; _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 readonly object _timerLock = new object();
private bool _timerActive; private bool _timerActive;
private void RestartTimer() private void RestartTimer()
@ -124,7 +157,6 @@ namespace MediaBrowser.Dlna.PlayTo
{ {
_logger.Debug("RestartTimer"); _logger.Debug("RestartTimer");
_timer.Change(10, GetPlaybackTimerIntervalMs()); _timer.Change(10, GetPlaybackTimerIntervalMs());
_volumeTimer.Change(100, GetVolumeTimerIntervalMs());
} }
_timerActive = true; _timerActive = true;
@ -150,10 +182,6 @@ namespace MediaBrowser.Dlna.PlayTo
{ {
_timer.Change(interval, interval); _timer.Change(interval, interval);
} }
if (_volumeTimer != null)
{
_volumeTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
} }
_timerActive = false; _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() private async Task GetVolume()
{ {
var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume"); var command = RendererCommands.ServiceActions.FirstOrDefault(c => c.Name == "GetVolume");
@ -1012,7 +1027,6 @@ namespace MediaBrowser.Dlna.PlayTo
_disposed = true; _disposed = true;
DisposeTimer(); DisposeTimer();
DisposeVolumeTimer();
} }
} }
@ -1025,15 +1039,6 @@ namespace MediaBrowser.Dlna.PlayTo
} }
} }
private void DisposeVolumeTimer()
{
if (_volumeTimer != null)
{
_volumeTimer.Dispose();
_volumeTimer = null;
}
}
#endregion #endregion
public override string ToString() public override string ToString()