remove volume timer from device

This commit is contained in:
Luke Pulverenti 2016-01-29 14:52:39 -05:00
parent a39f263814
commit f3534a56e2

View File

@ -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()