2020-09-24 21:04:21 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading ;
using System.Threading.Tasks ;
using Jellyfin.Data.Entities ;
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.Session ;
using MediaBrowser.Controller.SyncPlay ;
2020-11-14 22:40:01 +00:00
using MediaBrowser.Controller.SyncPlay.GroupStates ;
using MediaBrowser.Controller.SyncPlay.Queue ;
2020-11-28 15:03:02 +00:00
using MediaBrowser.Controller.SyncPlay.Requests ;
2020-09-24 21:04:21 +00:00
using MediaBrowser.Model.SyncPlay ;
using Microsoft.Extensions.Logging ;
namespace Emby.Server.Implementations.SyncPlay
{
/// <summary>
2020-12-03 20:01:18 +00:00
/// Class Group.
2020-09-24 21:04:21 +00:00
/// </summary>
/// <remarks>
/// Class is not thread-safe, external locking is required when accessing methods.
/// </remarks>
2020-12-03 20:01:18 +00:00
public class Group : IGroupStateContext
2020-09-24 21:04:21 +00:00
{
/// <summary>
/// The logger.
/// </summary>
2020-12-03 20:01:18 +00:00
private readonly ILogger < Group > _logger ;
2020-11-16 16:40:19 +00:00
/// <summary>
/// The logger factory.
/// </summary>
private readonly ILoggerFactory _loggerFactory ;
2020-09-24 21:04:21 +00:00
/// <summary>
/// The user manager.
/// </summary>
private readonly IUserManager _userManager ;
/// <summary>
/// The session manager.
/// </summary>
private readonly ISessionManager _sessionManager ;
/// <summary>
/// The library manager.
/// </summary>
private readonly ILibraryManager _libraryManager ;
2020-11-16 16:40:19 +00:00
/// <summary>
/// The participants, or members of the group.
/// </summary>
private readonly Dictionary < string , GroupMember > _participants =
new Dictionary < string , GroupMember > ( StringComparer . OrdinalIgnoreCase ) ;
2020-09-24 21:04:21 +00:00
/// <summary>
2020-11-18 12:28:35 +00:00
/// The internal group state.
2020-09-24 21:04:21 +00:00
/// </summary>
2020-11-13 14:13:32 +00:00
private IGroupState _state ;
/// <summary>
2020-12-03 20:01:18 +00:00
/// Initializes a new instance of the <see cref="Group" /> class.
2020-11-13 14:13:32 +00:00
/// </summary>
2020-11-16 16:40:19 +00:00
/// <param name="loggerFactory">The logger factory.</param>
2020-11-13 14:13:32 +00:00
/// <param name="userManager">The user manager.</param>
/// <param name="sessionManager">The session manager.</param>
/// <param name="libraryManager">The library manager.</param>
2020-12-03 20:01:18 +00:00
public Group (
2020-11-16 16:40:19 +00:00
ILoggerFactory loggerFactory ,
2020-11-13 14:13:32 +00:00
IUserManager userManager ,
ISessionManager sessionManager ,
2020-11-15 16:03:27 +00:00
ILibraryManager libraryManager )
2020-11-13 14:13:32 +00:00
{
2020-11-16 16:40:19 +00:00
_loggerFactory = loggerFactory ;
2020-11-13 14:13:32 +00:00
_userManager = userManager ;
_sessionManager = sessionManager ;
_libraryManager = libraryManager ;
2020-12-03 20:01:18 +00:00
_logger = loggerFactory . CreateLogger < Group > ( ) ;
2020-11-13 14:13:32 +00:00
2020-11-16 16:40:19 +00:00
_state = new IdleGroupState ( loggerFactory ) ;
2020-11-13 14:13:32 +00:00
}
/// <summary>
/// Gets the default ping value used for sessions.
/// </summary>
2020-11-16 16:40:19 +00:00
/// <value>The default ping.</value>
2020-11-13 14:13:32 +00:00
public long DefaultPing { get ; } = 500 ;
/// <summary>
/// Gets the maximum time offset error accepted for dates reported by clients, in milliseconds.
/// </summary>
2020-11-16 16:40:19 +00:00
/// <value>The maximum time offset error.</value>
2020-11-13 14:13:32 +00:00
public long TimeSyncOffset { get ; } = 2000 ;
/// <summary>
/// Gets the maximum offset error accepted for position reported by clients, in milliseconds.
/// </summary>
2020-11-16 16:40:19 +00:00
/// <value>The maximum offset error.</value>
2020-11-13 14:13:32 +00:00
public long MaxPlaybackOffset { get ; } = 500 ;
2020-09-24 21:04:21 +00:00
/// <summary>
/// Gets the group identifier.
/// </summary>
/// <value>The group identifier.</value>
public Guid GroupId { get ; } = Guid . NewGuid ( ) ;
/// <summary>
/// Gets the group name.
/// </summary>
/// <value>The group name.</value>
public string GroupName { get ; private set ; }
/// <summary>
/// Gets the group identifier.
/// </summary>
/// <value>The group identifier.</value>
public PlayQueueManager PlayQueue { get ; } = new PlayQueueManager ( ) ;
/// <summary>
2020-11-13 14:13:32 +00:00
/// Gets the runtime ticks of current playing item.
2020-09-24 21:04:21 +00:00
/// </summary>
/// <value>The runtime ticks of current playing item.</value>
public long RunTimeTicks { get ; private set ; }
/// <summary>
/// Gets or sets the position ticks.
/// </summary>
/// <value>The position ticks.</value>
public long PositionTicks { get ; set ; }
/// <summary>
/// Gets or sets the last activity.
/// </summary>
/// <value>The last activity.</value>
public DateTime LastActivity { get ; set ; }
/// <summary>
/// Adds the session to the group.
/// </summary>
/// <param name="session">The session.</param>
private void AddSession ( SessionInfo session )
{
2020-11-16 16:40:19 +00:00
_participants . TryAdd (
2020-09-24 21:04:21 +00:00
session . Id ,
2020-11-16 16:40:19 +00:00
new GroupMember ( session )
2020-09-24 21:04:21 +00:00
{
Ping = DefaultPing ,
IsBuffering = false
} ) ;
}
/// <summary>
/// Removes the session from the group.
/// </summary>
/// <param name="session">The session.</param>
private void RemoveSession ( SessionInfo session )
{
2020-11-16 16:40:19 +00:00
_participants . Remove ( session . Id ) ;
2020-09-24 21:04:21 +00:00
}
/// <summary>
/// Filters sessions of this group.
/// </summary>
/// <param name="from">The current session.</param>
/// <param name="type">The filtering type.</param>
2020-11-15 16:03:27 +00:00
/// <returns>The list of sessions matching the filter.</returns>
2020-11-13 14:13:32 +00:00
private IEnumerable < SessionInfo > FilterSessions ( SessionInfo from , SyncPlayBroadcastType type )
{
return type switch
{
SyncPlayBroadcastType . CurrentSession = > new SessionInfo [ ] { from } ,
2020-11-16 16:40:19 +00:00
SyncPlayBroadcastType . AllGroup = > _participants
2020-11-13 14:13:32 +00:00
. Values
. Select ( session = > session . Session ) ,
2020-11-16 16:40:19 +00:00
SyncPlayBroadcastType . AllExceptCurrentSession = > _participants
2020-11-13 14:13:32 +00:00
. Values
. Select ( session = > session . Session )
. Where ( session = > ! session . Id . Equals ( from . Id , StringComparison . OrdinalIgnoreCase ) ) ,
2020-11-16 16:40:19 +00:00
SyncPlayBroadcastType . AllReady = > _participants
2020-11-13 14:13:32 +00:00
. Values
. Where ( session = > ! session . IsBuffering )
. Select ( session = > session . Session ) ,
_ = > Enumerable . Empty < SessionInfo > ( )
} ;
2020-09-24 21:04:21 +00:00
}
2020-11-13 14:13:32 +00:00
/// <summary>
/// Checks if a given user can access all items of a given queue, that is,
/// the user has the required minimum parental access and has access to all required folders.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="queue">The queue.</param>
/// <returns><c>true</c> if the user can access all the items in the queue, <c>false</c> otherwise.</returns>
2020-11-15 16:03:27 +00:00
private bool HasAccessToQueue ( User user , IReadOnlyList < Guid > queue )
2020-09-24 21:04:21 +00:00
{
2020-11-13 14:13:32 +00:00
// Check if queue is empty.
2020-11-16 16:40:19 +00:00
if ( queue = = null | | queue . Count = = 0 )
2020-09-24 21:04:21 +00:00
{
return true ;
}
2020-11-13 14:13:32 +00:00
foreach ( var itemId in queue )
2020-09-24 21:04:21 +00:00
{
2020-11-13 14:13:32 +00:00
var item = _libraryManager . GetItemById ( itemId ) ;
2020-11-30 09:03:42 +00:00
if ( ! item . IsVisibleStandalone ( user ) )
2020-11-13 14:13:32 +00:00
{
return false ;
}
2020-09-24 21:04:21 +00:00
}
2020-11-13 14:13:32 +00:00
return true ;
2020-09-24 21:04:21 +00:00
}
2020-11-15 16:03:27 +00:00
private bool AllUsersHaveAccessToQueue ( IReadOnlyList < Guid > queue )
2020-09-24 21:04:21 +00:00
{
2020-11-13 14:13:32 +00:00
// Check if queue is empty.
2020-11-16 16:40:19 +00:00
if ( queue = = null | | queue . Count = = 0 )
2020-09-24 21:04:21 +00:00
{
return true ;
}
2020-10-21 13:46:50 +00:00
// Get list of users.
2020-11-16 16:40:19 +00:00
var users = _participants
2020-10-22 13:51:58 +00:00
. Values
2020-09-24 21:04:21 +00:00
. Select ( participant = > _userManager . GetUserById ( participant . Session . UserId ) ) ;
2020-10-21 13:46:50 +00:00
// Find problematic users.
2020-09-24 21:04:21 +00:00
var usersWithNoAccess = users . Where ( user = > ! HasAccessToQueue ( user , queue ) ) ;
2020-10-21 13:46:50 +00:00
// All users must be able to access the queue.
2020-09-24 21:04:21 +00:00
return ! usersWithNoAccess . Any ( ) ;
}
2020-12-03 20:01:18 +00:00
/// <summary>
/// Checks if the group is empty.
/// </summary>
/// <returns><c>true</c> if the group is empty, <c>false</c> otherwise.</returns>
2020-11-16 16:40:19 +00:00
public bool IsGroupEmpty ( ) = > _participants . Count = = 0 ;
2020-09-24 21:04:21 +00:00
2020-12-03 20:01:18 +00:00
/// <summary>
/// Initializes the group with the session's info.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2020-11-28 15:03:02 +00:00
public void CreateGroup ( SessionInfo session , NewGroupRequest request , CancellationToken cancellationToken )
2020-09-24 21:04:21 +00:00
{
GroupName = request . GroupName ;
AddSession ( session ) ;
var sessionIsPlayingAnItem = session . FullNowPlayingItem ! = null ;
RestartCurrentItem ( ) ;
if ( sessionIsPlayingAnItem )
{
2020-11-15 16:03:27 +00:00
var playlist = session . NowPlayingQueue . Select ( item = > item . Id ) . ToList ( ) ;
2020-10-22 13:49:37 +00:00
PlayQueue . Reset ( ) ;
2020-09-24 21:04:21 +00:00
PlayQueue . SetPlaylist ( playlist ) ;
PlayQueue . SetPlayingItemById ( session . FullNowPlayingItem . Id ) ;
RunTimeTicks = session . FullNowPlayingItem . RunTimeTicks ? ? 0 ;
PositionTicks = session . PlayState . PositionTicks ? ? 0 ;
2020-11-16 16:40:19 +00:00
// Maintain playstate.
var waitingState = new WaitingGroupState ( _loggerFactory )
2020-11-13 14:13:32 +00:00
{
ResumePlaying = ! session . PlayState . IsPaused
} ;
2020-09-24 21:04:21 +00:00
SetState ( waitingState ) ;
}
var updateSession = NewSyncPlayGroupUpdate ( GroupUpdateType . GroupJoined , GetInfo ( ) ) ;
SendGroupUpdate ( session , SyncPlayBroadcastType . CurrentSession , updateSession , cancellationToken ) ;
2020-11-13 14:13:32 +00:00
_state . SessionJoined ( this , _state . Type , session , cancellationToken ) ;
2020-09-24 21:04:21 +00:00
2020-11-16 16:40:19 +00:00
_logger . LogInformation ( "Session {SessionId} created group {GroupId}." , session . Id , GroupId . ToString ( ) ) ;
2020-09-24 21:04:21 +00:00
}
2020-12-03 20:01:18 +00:00
/// <summary>
/// Adds the session to the group.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2020-11-28 15:03:02 +00:00
public void SessionJoin ( SessionInfo session , JoinGroupRequest request , CancellationToken cancellationToken )
2020-09-24 21:04:21 +00:00
{
AddSession ( session ) ;
var updateSession = NewSyncPlayGroupUpdate ( GroupUpdateType . GroupJoined , GetInfo ( ) ) ;
SendGroupUpdate ( session , SyncPlayBroadcastType . CurrentSession , updateSession , cancellationToken ) ;
var updateOthers = NewSyncPlayGroupUpdate ( GroupUpdateType . UserJoined , session . UserName ) ;
SendGroupUpdate ( session , SyncPlayBroadcastType . AllExceptCurrentSession , updateOthers , cancellationToken ) ;
2020-11-13 14:13:32 +00:00
_state . SessionJoined ( this , _state . Type , session , cancellationToken ) ;
2020-09-24 21:04:21 +00:00
2020-11-16 16:40:19 +00:00
_logger . LogInformation ( "Session {SessionId} joined group {GroupId}." , session . Id , GroupId . ToString ( ) ) ;
2020-09-24 21:04:21 +00:00
}
2020-12-03 20:01:18 +00:00
/// <summary>
/// Removes the session from the group.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2020-11-28 15:03:02 +00:00
public void SessionLeave ( SessionInfo session , LeaveGroupRequest request , CancellationToken cancellationToken )
2020-09-24 21:04:21 +00:00
{
2020-11-13 14:13:32 +00:00
_state . SessionLeaving ( this , _state . Type , session , cancellationToken ) ;
2020-09-24 21:04:21 +00:00
RemoveSession ( session ) ;
var updateSession = NewSyncPlayGroupUpdate ( GroupUpdateType . GroupLeft , GroupId . ToString ( ) ) ;
SendGroupUpdate ( session , SyncPlayBroadcastType . CurrentSession , updateSession , cancellationToken ) ;
var updateOthers = NewSyncPlayGroupUpdate ( GroupUpdateType . UserLeft , session . UserName ) ;
SendGroupUpdate ( session , SyncPlayBroadcastType . AllExceptCurrentSession , updateOthers , cancellationToken ) ;
2020-11-16 16:40:19 +00:00
_logger . LogInformation ( "Session {SessionId} left group {GroupId}." , session . Id , GroupId . ToString ( ) ) ;
2020-09-24 21:04:21 +00:00
}
2020-12-03 20:01:18 +00:00
/// <summary>
/// Handles the requested action by the session.
/// </summary>
/// <param name="session">The session.</param>
/// <param name="request">The requested action.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2020-11-13 14:13:32 +00:00
public void HandleRequest ( SessionInfo session , IGroupPlaybackRequest request , CancellationToken cancellationToken )
2020-09-24 21:04:21 +00:00
{
// The server's job is to maintain a consistent state for clients to reference
// and notify clients of state changes. The actual syncing of media playback
// happens client side. Clients are aware of the server's time and use it to sync.
2020-11-28 15:03:02 +00:00
_logger . LogInformation ( "Session {SessionId} requested {RequestType} in group {GroupId} that is {StateType}." , session . Id , request . Action , GroupId . ToString ( ) , _state . Type ) ;
2020-11-13 14:13:32 +00:00
request . Apply ( this , _state , session , cancellationToken ) ;
2020-09-24 21:04:21 +00:00
}
2020-12-03 20:01:18 +00:00
/// <summary>
/// Gets the info about the group for the clients.
/// </summary>
/// <returns>The group info for the clients.</returns>
2020-09-24 21:04:21 +00:00
public GroupInfoDto GetInfo ( )
{
2020-11-16 16:40:19 +00:00
var participants = _participants . Values . Select ( session = > session . Session . UserName ) . Distinct ( ) . ToList ( ) ;
2020-11-15 16:03:27 +00:00
return new GroupInfoDto ( GroupId , GroupName , _state . Type , participants , DateTime . UtcNow ) ;
2020-09-24 21:04:21 +00:00
}
2020-12-03 20:01:18 +00:00
/// <summary>
/// Checks if a user has access to all content in the play queue.
/// </summary>
/// <param name="user">The user.</param>
/// <returns><c>true</c> if the user can access the play queue; <c>false</c> otherwise.</returns>
2020-09-24 21:04:21 +00:00
public bool HasAccessToPlayQueue ( User user )
{
2020-11-15 16:03:27 +00:00
var items = PlayQueue . GetPlaylist ( ) . Select ( item = > item . ItemId ) . ToList ( ) ;
2020-09-24 21:04:21 +00:00
return HasAccessToQueue ( user , items ) ;
}
/// <inheritdoc />
public void SetIgnoreGroupWait ( SessionInfo session , bool ignoreGroupWait )
{
2020-11-16 16:40:19 +00:00
if ( _participants . TryGetValue ( session . Id , out GroupMember value ) )
2020-09-24 21:04:21 +00:00
{
2020-11-14 17:09:25 +00:00
value . IgnoreGroupWait = ignoreGroupWait ;
2020-09-24 21:04:21 +00:00
}
}
/// <inheritdoc />
2020-11-13 14:13:32 +00:00
public void SetState ( IGroupState state )
2020-09-24 21:04:21 +00:00
{
2020-11-16 16:40:19 +00:00
_logger . LogInformation ( "Group {GroupId} switching from {FromStateType} to {ToStateType}." , GroupId . ToString ( ) , _state . Type , state . Type ) ;
2020-11-13 14:13:32 +00:00
this . _state = state ;
2020-09-24 21:04:21 +00:00
}
/// <inheritdoc />
public Task SendGroupUpdate < T > ( SessionInfo from , SyncPlayBroadcastType type , GroupUpdate < T > message , CancellationToken cancellationToken )
{
IEnumerable < Task > GetTasks ( )
{
foreach ( var session in FilterSessions ( from , type ) )
{
yield return _sessionManager . SendSyncPlayGroupUpdate ( session , message , cancellationToken ) ;
}
}
return Task . WhenAll ( GetTasks ( ) ) ;
}
/// <inheritdoc />
public Task SendCommand ( SessionInfo from , SyncPlayBroadcastType type , SendCommand message , CancellationToken cancellationToken )
{
IEnumerable < Task > GetTasks ( )
{
foreach ( var session in FilterSessions ( from , type ) )
{
yield return _sessionManager . SendSyncPlayCommand ( session , message , cancellationToken ) ;
}
}
return Task . WhenAll ( GetTasks ( ) ) ;
}
/// <inheritdoc />
public SendCommand NewSyncPlayCommand ( SendCommandType type )
{
2020-11-15 16:03:27 +00:00
return new SendCommand (
GroupId ,
PlayQueue . GetPlayingItemPlaylistId ( ) ,
LastActivity ,
type ,
PositionTicks ,
DateTime . UtcNow ) ;
2020-09-24 21:04:21 +00:00
}
/// <inheritdoc />
public GroupUpdate < T > NewSyncPlayGroupUpdate < T > ( GroupUpdateType type , T data )
{
2020-11-15 16:03:27 +00:00
return new GroupUpdate < T > ( GroupId , type , data ) ;
2020-09-24 21:04:21 +00:00
}
/// <inheritdoc />
public long SanitizePositionTicks ( long? positionTicks )
{
var ticks = positionTicks ? ? 0 ;
2020-11-13 14:13:32 +00:00
return Math . Clamp ( ticks , 0 , RunTimeTicks ) ;
2020-09-24 21:04:21 +00:00
}
/// <inheritdoc />
public void UpdatePing ( SessionInfo session , long ping )
{
2020-11-16 16:40:19 +00:00
if ( _participants . TryGetValue ( session . Id , out GroupMember value ) )
2020-09-24 21:04:21 +00:00
{
value . Ping = ping ;
}
}
/// <inheritdoc />
public long GetHighestPing ( )
{
long max = long . MinValue ;
2020-11-16 16:40:19 +00:00
foreach ( var session in _participants . Values )
2020-09-24 21:04:21 +00:00
{
max = Math . Max ( max , session . Ping ) ;
}
return max ;
}
/// <inheritdoc />
public void SetBuffering ( SessionInfo session , bool isBuffering )
{
2020-11-16 16:40:19 +00:00
if ( _participants . TryGetValue ( session . Id , out GroupMember value ) )
2020-09-24 21:04:21 +00:00
{
value . IsBuffering = isBuffering ;
}
}
/// <inheritdoc />
public void SetAllBuffering ( bool isBuffering )
{
2020-11-16 16:40:19 +00:00
foreach ( var session in _participants . Values )
2020-09-24 21:04:21 +00:00
{
session . IsBuffering = isBuffering ;
}
}
/// <inheritdoc />
public bool IsBuffering ( )
{
2020-11-16 16:40:19 +00:00
foreach ( var session in _participants . Values )
2020-09-24 21:04:21 +00:00
{
if ( session . IsBuffering & & ! session . IgnoreGroupWait )
{
return true ;
}
}
return false ;
}
/// <inheritdoc />
2020-11-15 16:03:27 +00:00
public bool SetPlayQueue ( IReadOnlyList < Guid > playQueue , int playingItemPosition , long startPositionTicks )
2020-09-24 21:04:21 +00:00
{
2020-10-21 13:46:50 +00:00
// Ignore on empty queue or invalid item position.
2020-11-15 16:03:27 +00:00
if ( playQueue . Count = = 0 | | playingItemPosition > = playQueue . Count | | playingItemPosition < 0 )
2020-09-24 21:04:21 +00:00
{
return false ;
}
2020-10-21 14:42:57 +00:00
// Check if participants can access the new playing queue.
2020-09-24 21:04:21 +00:00
if ( ! AllUsersHaveAccessToQueue ( playQueue ) )
{
return false ;
}
2020-10-22 13:49:37 +00:00
PlayQueue . Reset ( ) ;
2020-09-24 21:04:21 +00:00
PlayQueue . SetPlaylist ( playQueue ) ;
PlayQueue . SetPlayingItemByIndex ( playingItemPosition ) ;
var item = _libraryManager . GetItemById ( PlayQueue . GetPlayingItemId ( ) ) ;
RunTimeTicks = item . RunTimeTicks ? ? 0 ;
PositionTicks = startPositionTicks ;
LastActivity = DateTime . UtcNow ;
return true ;
}
/// <inheritdoc />
public bool SetPlayingItem ( string playlistItemId )
{
var itemFound = PlayQueue . SetPlayingItemByPlaylistId ( playlistItemId ) ;
if ( itemFound )
{
var item = _libraryManager . GetItemById ( PlayQueue . GetPlayingItemId ( ) ) ;
RunTimeTicks = item . RunTimeTicks ? ? 0 ;
}
else
{
RunTimeTicks = 0 ;
}
RestartCurrentItem ( ) ;
return itemFound ;
}
/// <inheritdoc />
2020-11-15 16:03:27 +00:00
public bool RemoveFromPlayQueue ( IReadOnlyList < string > playlistItemIds )
2020-09-24 21:04:21 +00:00
{
var playingItemRemoved = PlayQueue . RemoveFromPlaylist ( playlistItemIds ) ;
if ( playingItemRemoved )
{
var itemId = PlayQueue . GetPlayingItemId ( ) ;
if ( ! itemId . Equals ( Guid . Empty ) )
{
var item = _libraryManager . GetItemById ( itemId ) ;
RunTimeTicks = item . RunTimeTicks ? ? 0 ;
}
else
{
RunTimeTicks = 0 ;
}
RestartCurrentItem ( ) ;
}
return playingItemRemoved ;
}
/// <inheritdoc />
public bool MoveItemInPlayQueue ( string playlistItemId , int newIndex )
{
return PlayQueue . MovePlaylistItem ( playlistItemId , newIndex ) ;
}
/// <inheritdoc />
2020-11-15 16:03:27 +00:00
public bool AddToPlayQueue ( IReadOnlyList < Guid > newItems , GroupQueueMode mode )
2020-09-24 21:04:21 +00:00
{
2020-10-21 13:46:50 +00:00
// Ignore on empty list.
2020-11-15 16:03:27 +00:00
if ( newItems . Count = = 0 )
2020-09-24 21:04:21 +00:00
{
return false ;
}
2020-10-21 14:42:57 +00:00
// Check if participants can access the new playing queue.
2020-09-24 21:04:21 +00:00
if ( ! AllUsersHaveAccessToQueue ( newItems ) )
{
return false ;
}
2020-11-14 22:40:01 +00:00
if ( mode . Equals ( GroupQueueMode . QueueNext ) )
2020-09-24 21:04:21 +00:00
{
PlayQueue . QueueNext ( newItems ) ;
}
else
{
PlayQueue . Queue ( newItems ) ;
}
return true ;
}
/// <inheritdoc />
public void RestartCurrentItem ( )
{
PositionTicks = 0 ;
LastActivity = DateTime . UtcNow ;
}
/// <inheritdoc />
public bool NextItemInQueue ( )
{
var update = PlayQueue . Next ( ) ;
if ( update )
{
var item = _libraryManager . GetItemById ( PlayQueue . GetPlayingItemId ( ) ) ;
RunTimeTicks = item . RunTimeTicks ? ? 0 ;
RestartCurrentItem ( ) ;
return true ;
}
else
{
return false ;
}
}
/// <inheritdoc />
public bool PreviousItemInQueue ( )
{
var update = PlayQueue . Previous ( ) ;
if ( update )
{
var item = _libraryManager . GetItemById ( PlayQueue . GetPlayingItemId ( ) ) ;
RunTimeTicks = item . RunTimeTicks ? ? 0 ;
RestartCurrentItem ( ) ;
return true ;
}
else
{
return false ;
}
}
/// <inheritdoc />
2020-11-14 22:40:01 +00:00
public void SetRepeatMode ( GroupRepeatMode mode )
2020-11-13 14:13:32 +00:00
{
2020-11-14 22:40:01 +00:00
PlayQueue . SetRepeatMode ( mode ) ;
2020-09-24 21:04:21 +00:00
}
/// <inheritdoc />
2020-11-14 22:40:01 +00:00
public void SetShuffleMode ( GroupShuffleMode mode )
2020-11-13 14:13:32 +00:00
{
2020-11-14 22:40:01 +00:00
PlayQueue . SetShuffleMode ( mode ) ;
2020-09-24 21:04:21 +00:00
}
/// <inheritdoc />
public PlayQueueUpdate GetPlayQueueUpdate ( PlayQueueUpdateReason reason )
{
var startPositionTicks = PositionTicks ;
2020-11-13 14:13:32 +00:00
if ( _state . Type . Equals ( GroupStateType . Playing ) )
2020-09-24 21:04:21 +00:00
{
var currentTime = DateTime . UtcNow ;
var elapsedTime = currentTime - LastActivity ;
2020-10-22 13:51:58 +00:00
// Elapsed time is negative if event happens
// during the delay added to account for latency.
// In this phase clients haven't started the playback yet.
// In other words, LastActivity is in the future,
// when playback unpause is supposed to happen.
// Adjust ticks only if playback actually started.
startPositionTicks + = Math . Max ( elapsedTime . Ticks , 0 ) ;
2020-09-24 21:04:21 +00:00
}
2020-11-15 16:03:27 +00:00
return new PlayQueueUpdate (
reason ,
PlayQueue . LastChange ,
PlayQueue . GetPlaylist ( ) ,
PlayQueue . PlayingItemIndex ,
startPositionTicks ,
PlayQueue . ShuffleMode ,
PlayQueue . RepeatMode ) ;
2020-09-24 21:04:21 +00:00
}
}
}