diff options
| author | HurricaneHernandez <carlos@techbyte.ca> | 2014-10-24 16:12:24 -0600 |
|---|---|---|
| committer | HurricaneHernandez <carlos@techbyte.ca> | 2014-10-24 16:12:24 -0600 |
| commit | e01f932f4bd6764850155616e733590a74d905be (patch) | |
| tree | 894a2da0c7b9d96d5108b6f2ed57e0f10dfee96a /MediaBrowser.Server.Implementations/Session/SessionManager.cs | |
| parent | 3be4aa8dc729f5899658790f43a1d1c182e7a243 (diff) | |
| parent | c8a735bcb1ba71e9501d18b3044aa30793ff97ee (diff) | |
Merge remote-tracking branch 'upstream/master' into docker
Diffstat (limited to 'MediaBrowser.Server.Implementations/Session/SessionManager.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/Session/SessionManager.cs | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/MediaBrowser.Server.Implementations/Session/SessionManager.cs b/MediaBrowser.Server.Implementations/Session/SessionManager.cs index cf204a5ba..ac8068744 100644 --- a/MediaBrowser.Server.Implementations/Session/SessionManager.cs +++ b/MediaBrowser.Server.Implementations/Session/SessionManager.cs @@ -359,6 +359,7 @@ namespace MediaBrowser.Server.Implementations.Session session.NowPlayingItem = info.Item; session.LastActivityDate = DateTime.UtcNow; + session.LastPlaybackCheckIn = DateTime.UtcNow; session.PlayState.IsPaused = info.IsPaused; session.PlayState.PositionTicks = info.PositionTicks; @@ -498,6 +499,65 @@ namespace MediaBrowser.Server.Implementations.Session return users; } + private Timer _idleTimer; + + private void StartIdleCheckTimer() + { + if (_idleTimer == null) + { + _idleTimer = new Timer(CheckForIdlePlayback, null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5)); + } + } + private void StopIdleCheckTimer() + { + if (_idleTimer != null) + { + _idleTimer.Dispose(); + _idleTimer = null; + } + } + + private async void CheckForIdlePlayback(object state) + { + var playingSessions = Sessions.Where(i => i.NowPlayingItem != null) + .ToList(); + + if (playingSessions.Count > 0) + { + var idle = playingSessions + .Where(i => (DateTime.UtcNow - i.LastPlaybackCheckIn).TotalMinutes > 5) + .ToList(); + + foreach (var session in idle) + { + _logger.Debug("Session {0} has gone idle while playing", session.Id); + + try + { + await OnPlaybackStopped(new PlaybackStopInfo + { + Item = session.NowPlayingItem, + ItemId = (session.NowPlayingItem == null ? null : session.NowPlayingItem.Id), + SessionId = session.Id, + MediaSourceId = (session.PlayState == null ? null : session.PlayState.MediaSourceId) + }); + } + catch (Exception ex) + { + _logger.Debug("Error calling OnPlaybackStopped", ex); + } + } + + playingSessions = Sessions.Where(i => i.NowPlayingItem != null) + .ToList(); + } + + if (playingSessions.Count == 0) + { + StopIdleCheckTimer(); + } + } + /// <summary> /// Used to report that playback has started for an item /// </summary> @@ -553,6 +613,8 @@ namespace MediaBrowser.Server.Implementations.Session }, _logger); await SendPlaybackStartNotification(session, CancellationToken.None).ConfigureAwait(false); + + StartIdleCheckTimer(); } /// <summary> @@ -623,6 +685,8 @@ namespace MediaBrowser.Server.Implementations.Session DeviceId = session.DeviceId }, _logger); + + StartIdleCheckTimer(); } private async Task OnPlaybackProgress(Guid userId, string userDataKey, BaseItem item, long? positionTicks) @@ -1546,11 +1610,7 @@ namespace MediaBrowser.Server.Implementations.Session if (musicVideo != null) { info.Album = musicVideo.Album; - - if (!string.IsNullOrWhiteSpace(musicVideo.Artist)) - { - info.Artists.Add(musicVideo.Artist); - } + info.Artists = musicVideo.Artists.ToList(); } var backropItem = item.HasImage(ImageType.Backdrop) ? item : null; |
