SessionsService.cs 15 KB

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