2014-04-13 19:27:13 +02:00
using MediaBrowser.Controller.Library ;
2013-05-10 00:43:11 +02:00
using MediaBrowser.Controller.Session ;
2013-05-09 19:38:02 +02:00
using MediaBrowser.Model.Session ;
2013-12-07 16:52:38 +01:00
using ServiceStack ;
2013-05-10 14:18:07 +02:00
using System ;
2013-05-09 19:38:02 +02:00
using System.Collections.Generic ;
using System.Linq ;
2013-05-10 14:18:07 +02:00
using System.Threading ;
2013-05-28 19:25:10 +02:00
using System.Threading.Tasks ;
2013-05-09 19:38:02 +02:00
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetSessions
/// </summary>
2014-03-23 20:36:25 +01:00
[Route("/Sessions", "GET", Summary = "Gets a list of sessions")]
2013-05-10 00:43:11 +02:00
public class GetSessions : IReturn < List < SessionInfoDto > >
2013-05-09 19:38:02 +02:00
{
2013-05-11 18:45:20 +02:00
/// <summary>
/// Gets or sets a value indicating whether [supports remote control].
/// </summary>
/// <value><c>null</c> if [supports remote control] contains no value, <c>true</c> if [supports remote control]; otherwise, <c>false</c>.</value>
2013-05-10 14:18:07 +02:00
[ApiMember(Name = "SupportsRemoteControl", Description = "Optional. Filter by sessions that can be remote controlled.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? SupportsRemoteControl { get ; set ; }
2013-09-19 22:03:14 +02:00
[ApiMember(Name = "ControllableByUserId", Description = "Optional. Filter by sessions that a given user is allowed to remote control.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid ? ControllableByUserId { get ; set ; }
2014-01-06 17:48:43 +01:00
2014-03-23 21:49:05 +01:00
[ApiMember(Name = "DeviceId", Description = "Optional. Filter by device id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2014-01-06 17:48:43 +01:00
public string DeviceId { get ; set ; }
2013-05-10 14:18:07 +02:00
}
2013-05-11 18:45:20 +02:00
/// <summary>
2014-04-13 19:27:13 +02:00
/// Class DisplayContent
2013-05-11 18:45:20 +02:00
/// </summary>
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/Viewing", "POST", Summary = "Instructs a session to browse to an item or view")]
2014-04-13 19:27:13 +02:00
public class DisplayContent : IReturnVoid
2013-05-10 14:18:07 +02:00
{
2013-05-11 18:45:20 +02:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
2013-05-10 14:18:07 +02:00
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2013-05-10 14:18:07 +02:00
2013-05-09 19:38:02 +02:00
/// <summary>
2013-05-10 14:18:07 +02:00
/// Artist, Genre, Studio, Person, or any kind of BaseItem
2013-05-09 19:38:02 +02:00
/// </summary>
2013-05-10 14:18:07 +02:00
/// <value>The type of the item.</value>
2013-05-28 19:25:10 +02:00
[ApiMember(Name = "ItemType", Description = "The type of item to browse to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-05-10 14:18:07 +02:00
public string ItemType { get ; set ; }
/// <summary>
/// Artist name, genre name, item Id, etc
/// </summary>
/// <value>The item identifier.</value>
2013-05-26 02:53:51 +02:00
[ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemId { get ; set ; }
2013-05-10 14:18:07 +02:00
2013-05-26 02:53:51 +02:00
/// <summary>
/// Gets or sets the name of the item.
/// </summary>
/// <value>The name of the item.</value>
[ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemName { get ; set ; }
2013-05-28 19:25:10 +02:00
2013-05-10 14:18:07 +02:00
/// <summary>
/// Gets or sets the context (Movies, Music, TvShows, etc)
/// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
/// This is optional to supply and clients are free to ignore it.
/// </summary>
/// <value>The context.</value>
2013-05-26 03:53:34 +02:00
[ApiMember(Name = "Context", Description = "The ui context for the client (movies, music, tv, games etc). This is optional to supply and clients are free to ignore it.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-05-10 14:18:07 +02:00
public string Context { get ; set ; }
2013-05-09 19:38:02 +02:00
}
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/Playing", "POST", Summary = "Instructs a session to play an item")]
2013-05-28 19:25:10 +02:00
public class Play : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2013-05-28 19:25:10 +02:00
/// <summary>
/// Artist, Genre, Studio, Person, or any kind of BaseItem
/// </summary>
/// <value>The type of the item.</value>
[ApiMember(Name = "ItemIds", Description = "The ids of the items to play, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string ItemIds { get ; set ; }
/// <summary>
/// Gets or sets the start position ticks that the first item should be played at
/// </summary>
/// <value>The start position ticks.</value>
[ApiMember(Name = "StartPositionTicks", Description = "The starting position of the first item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public long? StartPositionTicks { get ; set ; }
/// <summary>
/// Gets or sets the play command.
/// </summary>
/// <value>The play command.</value>
[ApiMember(Name = "PlayCommand", Description = "The type of play command to issue (PlayNow, PlayNext, PlayLast). Clients who have not yet implemented play next and play last may play now.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public PlayCommand PlayCommand { get ; set ; }
}
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/Playing/{Command}", "POST", Summary = "Issues a playstate command to a client")]
2013-07-09 18:11:16 +02:00
public class SendPlaystateCommand : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2013-07-09 18:11:16 +02:00
/// <summary>
/// Gets or sets the position to seek to
/// </summary>
2013-07-19 20:36:44 +02:00
[ApiMember(Name = "SeekPositionTicks", Description = "The position to seek to.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public long? SeekPositionTicks { get ; set ; }
2013-07-09 18:11:16 +02:00
/// <summary>
/// Gets or sets the play command.
/// </summary>
/// <value>The play command.</value>
2014-03-20 20:12:10 +01:00
[ApiMember(Name = "Command", Description = "The command to send - stop, pause, unpause, nexttrack, previoustrack, seek, fullscreen.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2013-07-09 18:11:16 +02:00
public PlaystateCommand Command { get ; set ; }
}
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/System/{Command}", "POST", Summary = "Issues a system command to a client")]
2013-08-28 06:16:21 +02:00
public class SendSystemCommand : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2013-08-28 06:16:21 +02:00
/// <summary>
/// Gets or sets the command.
/// </summary>
/// <value>The play command.</value>
[ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-03-31 23:04:22 +02:00
public string Command { get ; set ; }
}
[Route("/Sessions/{Id}/Command/{Command}", "POST", Summary = "Issues a system command to a client")]
public class SendGeneralCommand : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2014-03-31 23:04:22 +02:00
/// <summary>
/// Gets or sets the command.
/// </summary>
/// <value>The play command.</value>
[ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Command { get ; set ; }
}
[Route("/Sessions/{Id}/Command", "POST", Summary = "Issues a system command to a client")]
public class SendFullGeneralCommand : GeneralCommand , IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2013-08-28 06:16:21 +02:00
}
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/Message", "POST", Summary = "Issues a command to a client to display a message to the user")]
2013-08-28 06:16:21 +02:00
public class SendMessageCommand : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2013-08-28 06:16:21 +02:00
[ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Text { get ; set ; }
[ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Header { get ; set ; }
2013-08-28 06:31:34 +02:00
[ApiMember(Name = "TimeoutMs", Description = "The message timeout. If omitted the user will have to confirm viewing the message.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-08-28 06:16:21 +02:00
public long? TimeoutMs { get ; set ; }
}
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/Users/{UserId}", "POST", Summary = "Adds an additional user to a session")]
2014-01-04 03:35:41 +01:00
public class AddUserToSession : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2014-01-04 03:35:41 +01:00
[ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get ; set ; }
}
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/Users/{UserId}", "DELETE", Summary = "Removes an additional user from a session")]
2014-01-04 03:35:41 +01:00
public class RemoveUserFromSession : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-16 04:17:48 +02:00
public string Id { get ; set ; }
2014-01-04 03:35:41 +01:00
[ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get ; set ; }
}
2014-04-13 19:27:13 +02:00
[Route("/Sessions/Capabilities", "POST", Summary = "Updates capabilities for a device")]
2014-03-23 20:36:25 +01:00
[Route("/Sessions/{Id}/Capabilities", "POST", Summary = "Updates capabilities for a device")]
2014-01-05 06:34:06 +01:00
public class PostCapabilities : IReturnVoid
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2014-04-13 19:27:13 +02:00
public string Id { get ; set ; }
2014-01-05 06:34:06 +01:00
2014-01-05 07:08:22 +01:00
[ApiMember(Name = "PlayableMediaTypes", Description = "A list of playable media types, comma delimited. Audio, Video, Book, Game, Photo.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
2014-01-05 06:34:06 +01:00
public string PlayableMediaTypes { get ; set ; }
2014-03-21 04:31:40 +01:00
2014-04-02 23:55:19 +02:00
[ApiMember(Name = "SupportedCommands", Description = "A list of supported remote control commands, comma delimited", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string SupportedCommands { get ; set ; }
2014-01-05 06:34:06 +01:00
}
2014-03-13 10:18:32 +01:00
2013-05-09 19:38:02 +02:00
/// <summary>
/// Class SessionsService
/// </summary>
public class SessionsService : BaseApiService
{
/// <summary>
/// The _session manager
/// </summary>
private readonly ISessionManager _sessionManager ;
2013-10-30 16:55:55 +01:00
private readonly IUserManager _userManager ;
2013-09-04 19:02:19 +02:00
2013-05-09 19:38:02 +02:00
/// <summary>
2013-05-11 18:45:20 +02:00
/// Initializes a new instance of the <see cref="SessionsService" /> class.
2013-05-09 19:38:02 +02:00
/// </summary>
/// <param name="sessionManager">The session manager.</param>
2014-04-06 19:53:23 +02:00
/// <param name="userManager">The user manager.</param>
public SessionsService ( ISessionManager sessionManager , IUserManager userManager )
2013-05-09 19:38:02 +02:00
{
_sessionManager = sessionManager ;
2013-10-30 16:55:55 +01:00
_userManager = userManager ;
2013-05-09 19:38:02 +02:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetSessions request )
{
2013-05-10 14:18:07 +02:00
var result = _sessionManager . Sessions . Where ( i = > i . IsActive ) ;
if ( request . SupportsRemoteControl . HasValue )
{
2013-06-12 17:53:29 +02:00
result = result . Where ( i = > i . SupportsRemoteControl = = request . SupportsRemoteControl . Value ) ;
2013-05-10 14:18:07 +02:00
}
2013-05-09 19:38:02 +02:00
2014-01-06 17:48:43 +01:00
if ( ! string . IsNullOrEmpty ( request . DeviceId ) )
{
result = result . Where ( i = > string . Equals ( i . DeviceId , request . DeviceId , StringComparison . OrdinalIgnoreCase ) ) ;
}
2013-10-30 16:55:55 +01:00
if ( request . ControllableByUserId . HasValue )
{
var user = _userManager . GetUserById ( request . ControllableByUserId . Value ) ;
if ( ! user . Configuration . EnableRemoteControlOfOtherUsers )
{
2014-01-04 03:59:20 +01:00
result = result . Where ( i = > ! i . UserId . HasValue | | i . ContainsUser ( request . ControllableByUserId . Value ) ) ;
2013-10-30 16:55:55 +01:00
}
}
2014-04-06 19:53:23 +02:00
return ToOptimizedResult ( result . Select ( _sessionManager . GetSessionInfoDto ) . ToList ( ) ) ;
2013-05-09 19:38:02 +02:00
}
2013-05-10 14:18:07 +02:00
2013-07-09 18:11:16 +02:00
public void Post ( SendPlaystateCommand request )
2014-03-13 10:18:32 +01:00
{
2013-09-24 21:54:42 +02:00
var command = new PlaystateRequest
2013-07-09 18:11:16 +02:00
{
2013-09-24 21:54:42 +02:00
Command = request . Command ,
SeekPositionTicks = request . SeekPositionTicks
} ;
2013-07-09 18:11:16 +02:00
2014-03-16 17:15:10 +01:00
var task = _sessionManager . SendPlaystateCommand ( GetSession ( ) . Id , request . Id , command , CancellationToken . None ) ;
2013-07-09 18:11:16 +02:00
2013-09-24 21:54:42 +02:00
Task . WaitAll ( task ) ;
2013-07-09 18:11:16 +02:00
}
2013-08-28 06:31:34 +02:00
2013-05-11 18:45:20 +02:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2014-04-13 19:27:13 +02:00
public void Post ( DisplayContent request )
2013-05-28 19:25:10 +02:00
{
2013-09-24 21:54:42 +02:00
var command = new BrowseRequest
2013-05-27 03:23:46 +02:00
{
2013-09-24 21:54:42 +02:00
Context = request . Context ,
ItemId = request . ItemId ,
ItemName = request . ItemName ,
ItemType = request . ItemType
} ;
2013-05-27 03:23:46 +02:00
2014-03-16 17:15:10 +01:00
var task = _sessionManager . SendBrowseCommand ( GetSession ( ) . Id , request . Id , command , CancellationToken . None ) ;
2013-05-26 02:53:51 +02:00
2013-09-24 21:54:42 +02:00
Task . WaitAll ( task ) ;
2013-05-10 14:18:07 +02:00
}
2013-05-28 19:25:10 +02:00
2013-08-28 06:16:21 +02:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post ( SendSystemCommand request )
{
2014-03-31 23:04:22 +02:00
GeneralCommandType commandType ;
2013-08-28 06:16:21 +02:00
2014-03-31 23:04:22 +02:00
if ( Enum . TryParse ( request . Command , true , out commandType ) )
{
var currentSession = GetSession ( ) ;
var command = new GeneralCommand
{
Name = commandType . ToString ( ) ,
ControllingUserId = currentSession . UserId . HasValue ? currentSession . UserId . Value . ToString ( "N" ) : null
} ;
var task = _sessionManager . SendGeneralCommand ( currentSession . Id , request . Id , command , CancellationToken . None ) ;
Task . WaitAll ( task ) ;
}
2013-08-28 06:16:21 +02:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post ( SendMessageCommand request )
{
2013-09-24 21:54:42 +02:00
var command = new MessageCommand
2013-08-28 06:16:21 +02:00
{
2013-09-24 21:54:42 +02:00
Header = string . IsNullOrEmpty ( request . Header ) ? "Message from Server" : request . Header ,
TimeoutMs = request . TimeoutMs ,
Text = request . Text
} ;
2013-08-28 06:16:21 +02:00
2014-03-16 17:15:10 +01:00
var task = _sessionManager . SendMessageCommand ( GetSession ( ) . Id , request . Id , command , CancellationToken . None ) ;
2013-08-28 06:16:21 +02:00
2013-09-24 21:54:42 +02:00
Task . WaitAll ( task ) ;
2013-08-28 06:16:21 +02:00
}
2013-05-28 19:25:10 +02:00
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post ( Play request )
{
2013-09-24 21:54:42 +02:00
var command = new PlayRequest
2013-05-28 19:25:10 +02:00
{
2013-09-24 21:54:42 +02:00
ItemIds = request . ItemIds . Split ( ',' ) . ToArray ( ) ,
2014-03-13 10:18:32 +01:00
2013-09-24 21:54:42 +02:00
PlayCommand = request . PlayCommand ,
StartPositionTicks = request . StartPositionTicks
} ;
2013-05-28 19:25:10 +02:00
2014-03-16 17:15:10 +01:00
var task = _sessionManager . SendPlayCommand ( GetSession ( ) . Id , request . Id , command , CancellationToken . None ) ;
2013-05-28 19:25:10 +02:00
2013-09-24 21:54:42 +02:00
Task . WaitAll ( task ) ;
2013-05-28 19:25:10 +02:00
}
2014-01-04 03:35:41 +01:00
2014-03-31 23:04:22 +02:00
public void Post ( SendGeneralCommand request )
{
var currentSession = GetSession ( ) ;
var command = new GeneralCommand
{
Name = request . Command ,
ControllingUserId = currentSession . UserId . HasValue ? currentSession . UserId . Value . ToString ( "N" ) : null
} ;
var task = _sessionManager . SendGeneralCommand ( currentSession . Id , request . Id , command , CancellationToken . None ) ;
Task . WaitAll ( task ) ;
}
public void Post ( SendFullGeneralCommand request )
{
var currentSession = GetSession ( ) ;
request . ControllingUserId = currentSession . UserId . HasValue ? currentSession . UserId . Value . ToString ( "N" ) : null ;
var task = _sessionManager . SendGeneralCommand ( currentSession . Id , request . Id , request , CancellationToken . None ) ;
Task . WaitAll ( task ) ;
}
2014-01-04 03:35:41 +01:00
public void Post ( AddUserToSession request )
{
_sessionManager . AddAdditionalUser ( request . Id , request . UserId ) ;
}
public void Delete ( RemoveUserFromSession request )
{
_sessionManager . RemoveAdditionalUser ( request . Id , request . UserId ) ;
}
2014-01-05 06:34:06 +01:00
public void Post ( PostCapabilities request )
{
2014-04-13 19:27:13 +02:00
if ( string . IsNullOrWhiteSpace ( request . Id ) )
{
2014-04-16 04:17:48 +02:00
request . Id = GetSession ( ) . Id ;
2014-04-13 19:27:13 +02:00
}
2014-04-16 04:17:48 +02:00
_sessionManager . ReportCapabilities ( request . Id , new SessionCapabilities
2014-03-21 04:31:40 +01:00
{
2014-04-02 23:55:19 +02:00
PlayableMediaTypes = request . PlayableMediaTypes . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) . ToList ( ) ,
2014-01-05 06:34:06 +01:00
2014-04-02 23:55:19 +02:00
SupportedCommands = request . SupportedCommands . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) . ToList ( )
2014-03-21 04:31:40 +01:00
} ) ;
2014-01-05 06:34:06 +01:00
}
2014-03-16 17:15:10 +01:00
private SessionInfo GetSession ( )
{
var auth = AuthorizationRequestFilterAttribute . GetAuthorization ( Request ) ;
return _sessionManager . Sessions . First ( i = > string . Equals ( i . DeviceId , auth . DeviceId ) & &
string . Equals ( i . Client , auth . Client ) & &
string . Equals ( i . ApplicationVersion , auth . Version ) ) ;
}
2013-05-09 19:38:02 +02:00
}
2014-03-13 10:18:32 +01:00
}