SessionsService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. using MediaBrowser.Controller.Dto;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Session;
  4. using MediaBrowser.Model.Session;
  5. using ServiceStack;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Api
  12. {
  13. /// <summary>
  14. /// Class GetSessions
  15. /// </summary>
  16. [Route("/Sessions", "GET")]
  17. [Api(("Gets a list of sessions"))]
  18. public class GetSessions : IReturn<List<SessionInfoDto>>
  19. {
  20. /// <summary>
  21. /// Gets or sets a value indicating whether [supports remote control].
  22. /// </summary>
  23. /// <value><c>null</c> if [supports remote control] contains no value, <c>true</c> if [supports remote control]; otherwise, <c>false</c>.</value>
  24. [ApiMember(Name = "SupportsRemoteControl", Description = "Optional. Filter by sessions that can be remote controlled.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  25. public bool? SupportsRemoteControl { get; set; }
  26. [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")]
  27. public Guid? ControllableByUserId { get; set; }
  28. [ApiMember(Name = "DeviceId", Description = "Optional. Filter by device id.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  29. public string DeviceId { get; set; }
  30. }
  31. /// <summary>
  32. /// Class BrowseTo
  33. /// </summary>
  34. [Route("/Sessions/{Id}/Viewing", "POST")]
  35. [Api(("Instructs a session to browse to an item or view"))]
  36. public class BrowseTo : IReturnVoid
  37. {
  38. /// <summary>
  39. /// Gets or sets the id.
  40. /// </summary>
  41. /// <value>The id.</value>
  42. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  43. public Guid Id { get; set; }
  44. /// <summary>
  45. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  46. /// </summary>
  47. /// <value>The type of the item.</value>
  48. [ApiMember(Name = "ItemType", Description = "The type of item to browse to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  49. public string ItemType { get; set; }
  50. /// <summary>
  51. /// Artist name, genre name, item Id, etc
  52. /// </summary>
  53. /// <value>The item identifier.</value>
  54. [ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  55. public string ItemId { get; set; }
  56. /// <summary>
  57. /// Gets or sets the name of the item.
  58. /// </summary>
  59. /// <value>The name of the item.</value>
  60. [ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  61. public string ItemName { get; set; }
  62. /// <summary>
  63. /// Gets or sets the context (Movies, Music, TvShows, etc)
  64. /// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
  65. /// This is optional to supply and clients are free to ignore it.
  66. /// </summary>
  67. /// <value>The context.</value>
  68. [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")]
  69. public string Context { get; set; }
  70. }
  71. [Route("/Sessions/{Id}/Playing", "POST")]
  72. [Api(("Instructs a session to play an item"))]
  73. public class Play : IReturnVoid
  74. {
  75. /// <summary>
  76. /// Gets or sets the id.
  77. /// </summary>
  78. /// <value>The id.</value>
  79. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  80. public Guid Id { get; set; }
  81. /// <summary>
  82. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  83. /// </summary>
  84. /// <value>The type of the item.</value>
  85. [ApiMember(Name = "ItemIds", Description = "The ids of the items to play, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  86. public string ItemIds { get; set; }
  87. /// <summary>
  88. /// Gets or sets the start position ticks that the first item should be played at
  89. /// </summary>
  90. /// <value>The start position ticks.</value>
  91. [ApiMember(Name = "StartPositionTicks", Description = "The starting position of the first item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  92. public long? StartPositionTicks { get; set; }
  93. /// <summary>
  94. /// Gets or sets the play command.
  95. /// </summary>
  96. /// <value>The play command.</value>
  97. [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")]
  98. public PlayCommand PlayCommand { get; set; }
  99. }
  100. [Route("/Sessions/{Id}/Playing/{Command}", "POST")]
  101. [Api(("Issues a playstate command to a client"))]
  102. public class SendPlaystateCommand : IReturnVoid
  103. {
  104. /// <summary>
  105. /// Gets or sets the id.
  106. /// </summary>
  107. /// <value>The id.</value>
  108. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  109. public Guid Id { get; set; }
  110. /// <summary>
  111. /// Gets or sets the position to seek to
  112. /// </summary>
  113. [ApiMember(Name = "SeekPositionTicks", Description = "The position to seek to.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  114. public long? SeekPositionTicks { get; set; }
  115. /// <summary>
  116. /// Gets or sets the play command.
  117. /// </summary>
  118. /// <value>The play command.</value>
  119. [ApiMember(Name = "Command", Description = "The command to send - stop, pause, unpause, nexttrack, previoustrack, seek, fullscreen.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  120. public PlaystateCommand Command { get; set; }
  121. }
  122. [Route("/Sessions/{Id}/System/{Command}", "POST")]
  123. [Api(("Issues a system command to a client"))]
  124. public class SendSystemCommand : IReturnVoid
  125. {
  126. /// <summary>
  127. /// Gets or sets the id.
  128. /// </summary>
  129. /// <value>The id.</value>
  130. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  131. public Guid Id { get; set; }
  132. /// <summary>
  133. /// Gets or sets the command.
  134. /// </summary>
  135. /// <value>The play command.</value>
  136. [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  137. public SystemCommand Command { get; set; }
  138. }
  139. [Route("/Sessions/{Id}/Message", "POST")]
  140. [Api(("Issues a command to a client to display a message to the user"))]
  141. public class SendMessageCommand : IReturnVoid
  142. {
  143. /// <summary>
  144. /// Gets or sets the id.
  145. /// </summary>
  146. /// <value>The id.</value>
  147. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  148. public Guid Id { get; set; }
  149. [ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  150. public string Text { get; set; }
  151. [ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  152. public string Header { get; set; }
  153. [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")]
  154. public long? TimeoutMs { get; set; }
  155. }
  156. [Route("/Sessions/{Id}/Users/{UserId}", "POST")]
  157. [Api(("Adds an additional user to a session"))]
  158. public class AddUserToSession : IReturnVoid
  159. {
  160. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  161. public Guid Id { get; set; }
  162. [ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  163. public Guid UserId { get; set; }
  164. }
  165. [Route("/Sessions/{Id}/Users/{UserId}", "DELETE")]
  166. [Api(("Removes an additional user from a session"))]
  167. public class RemoveUserFromSession : IReturnVoid
  168. {
  169. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  170. public Guid Id { get; set; }
  171. [ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  172. public Guid UserId { get; set; }
  173. }
  174. [Route("/Sessions/{Id}/Capabilities", "POST")]
  175. [Api(("Updates capabilities for a device"))]
  176. public class PostCapabilities : IReturnVoid
  177. {
  178. /// <summary>
  179. /// Gets or sets the id.
  180. /// </summary>
  181. /// <value>The id.</value>
  182. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  183. public Guid Id { get; set; }
  184. [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")]
  185. public string PlayableMediaTypes { get; set; }
  186. [ApiMember(Name = "SupportsFullscreenToggle", Description = "Whether or not the session supports fullscreen toggle", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
  187. public bool SupportsFullscreenToggle { get; set; }
  188. }
  189. /// <summary>
  190. /// Class SessionsService
  191. /// </summary>
  192. public class SessionsService : BaseApiService
  193. {
  194. /// <summary>
  195. /// The _session manager
  196. /// </summary>
  197. private readonly ISessionManager _sessionManager;
  198. private readonly IDtoService _dtoService;
  199. private readonly IUserManager _userManager;
  200. /// <summary>
  201. /// Initializes a new instance of the <see cref="SessionsService" /> class.
  202. /// </summary>
  203. /// <param name="sessionManager">The session manager.</param>
  204. /// <param name="dtoService">The dto service.</param>
  205. public SessionsService(ISessionManager sessionManager, IDtoService dtoService, IUserManager userManager)
  206. {
  207. _sessionManager = sessionManager;
  208. _dtoService = dtoService;
  209. _userManager = userManager;
  210. }
  211. /// <summary>
  212. /// Gets the specified request.
  213. /// </summary>
  214. /// <param name="request">The request.</param>
  215. /// <returns>System.Object.</returns>
  216. public object Get(GetSessions request)
  217. {
  218. var result = _sessionManager.Sessions.Where(i => i.IsActive);
  219. if (request.SupportsRemoteControl.HasValue)
  220. {
  221. result = result.Where(i => i.SupportsRemoteControl == request.SupportsRemoteControl.Value);
  222. }
  223. if (!string.IsNullOrEmpty(request.DeviceId))
  224. {
  225. result = result.Where(i => string.Equals(i.DeviceId, request.DeviceId, StringComparison.OrdinalIgnoreCase));
  226. }
  227. if (request.ControllableByUserId.HasValue)
  228. {
  229. var user = _userManager.GetUserById(request.ControllableByUserId.Value);
  230. if (!user.Configuration.EnableRemoteControlOfOtherUsers)
  231. {
  232. result = result.Where(i => !i.UserId.HasValue || i.ContainsUser(request.ControllableByUserId.Value));
  233. }
  234. }
  235. return ToOptimizedResult(result.Select(_dtoService.GetSessionInfoDto).ToList());
  236. }
  237. public void Post(SendPlaystateCommand request)
  238. {
  239. var command = new PlaystateRequest
  240. {
  241. Command = request.Command,
  242. SeekPositionTicks = request.SeekPositionTicks
  243. };
  244. var task = _sessionManager.SendPlaystateCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  245. Task.WaitAll(task);
  246. }
  247. /// <summary>
  248. /// Posts the specified request.
  249. /// </summary>
  250. /// <param name="request">The request.</param>
  251. public void Post(BrowseTo request)
  252. {
  253. var command = new BrowseRequest
  254. {
  255. Context = request.Context,
  256. ItemId = request.ItemId,
  257. ItemName = request.ItemName,
  258. ItemType = request.ItemType
  259. };
  260. var task = _sessionManager.SendBrowseCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  261. Task.WaitAll(task);
  262. }
  263. /// <summary>
  264. /// Posts the specified request.
  265. /// </summary>
  266. /// <param name="request">The request.</param>
  267. public void Post(SendSystemCommand request)
  268. {
  269. var task = _sessionManager.SendSystemCommand(GetSession().Id, request.Id, request.Command, CancellationToken.None);
  270. Task.WaitAll(task);
  271. }
  272. /// <summary>
  273. /// Posts the specified request.
  274. /// </summary>
  275. /// <param name="request">The request.</param>
  276. public void Post(SendMessageCommand request)
  277. {
  278. var command = new MessageCommand
  279. {
  280. Header = string.IsNullOrEmpty(request.Header) ? "Message from Server" : request.Header,
  281. TimeoutMs = request.TimeoutMs,
  282. Text = request.Text
  283. };
  284. var task = _sessionManager.SendMessageCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  285. Task.WaitAll(task);
  286. }
  287. /// <summary>
  288. /// Posts the specified request.
  289. /// </summary>
  290. /// <param name="request">The request.</param>
  291. public void Post(Play request)
  292. {
  293. var command = new PlayRequest
  294. {
  295. ItemIds = request.ItemIds.Split(',').ToArray(),
  296. PlayCommand = request.PlayCommand,
  297. StartPositionTicks = request.StartPositionTicks
  298. };
  299. var task = _sessionManager.SendPlayCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  300. Task.WaitAll(task);
  301. }
  302. public void Post(AddUserToSession request)
  303. {
  304. _sessionManager.AddAdditionalUser(request.Id, request.UserId);
  305. }
  306. public void Delete(RemoveUserFromSession request)
  307. {
  308. _sessionManager.RemoveAdditionalUser(request.Id, request.UserId);
  309. }
  310. public void Post(PostCapabilities request)
  311. {
  312. _sessionManager.ReportCapabilities(request.Id, new SessionCapabilities
  313. {
  314. PlayableMediaTypes = request.PlayableMediaTypes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries),
  315. SupportsFullscreenToggle = request.SupportsFullscreenToggle
  316. });
  317. }
  318. private SessionInfo GetSession()
  319. {
  320. var auth = AuthorizationRequestFilterAttribute.GetAuthorization(Request);
  321. return _sessionManager.Sessions.First(i => string.Equals(i.DeviceId, auth.DeviceId) &&
  322. string.Equals(i.Client, auth.Client) &&
  323. string.Equals(i.ApplicationVersion, auth.Version));
  324. }
  325. }
  326. }