SessionsService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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", Summary = "Gets a list of sessions")]
  17. public class GetSessions : IReturn<List<SessionInfoDto>>
  18. {
  19. /// <summary>
  20. /// Gets or sets a value indicating whether [supports remote control].
  21. /// </summary>
  22. /// <value><c>null</c> if [supports remote control] contains no value, <c>true</c> if [supports remote control]; otherwise, <c>false</c>.</value>
  23. [ApiMember(Name = "SupportsRemoteControl", Description = "Optional. Filter by sessions that can be remote controlled.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
  24. public bool? SupportsRemoteControl { get; set; }
  25. [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")]
  26. public Guid? ControllableByUserId { get; set; }
  27. [ApiMember(Name = "DeviceId", Description = "Optional. Filter by device id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  28. public string DeviceId { get; set; }
  29. }
  30. /// <summary>
  31. /// Class BrowseTo
  32. /// </summary>
  33. [Route("/Sessions/{Id}/Viewing", "POST", Summary = "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", Summary = "Instructs a session to play an item")]
  70. public class Play : IReturnVoid
  71. {
  72. /// <summary>
  73. /// Gets or sets the id.
  74. /// </summary>
  75. /// <value>The id.</value>
  76. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  77. public Guid Id { get; set; }
  78. /// <summary>
  79. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  80. /// </summary>
  81. /// <value>The type of the item.</value>
  82. [ApiMember(Name = "ItemIds", Description = "The ids of the items to play, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  83. public string ItemIds { get; set; }
  84. /// <summary>
  85. /// Gets or sets the start position ticks that the first item should be played at
  86. /// </summary>
  87. /// <value>The start position ticks.</value>
  88. [ApiMember(Name = "StartPositionTicks", Description = "The starting position of the first item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  89. public long? StartPositionTicks { get; set; }
  90. /// <summary>
  91. /// Gets or sets the play command.
  92. /// </summary>
  93. /// <value>The play command.</value>
  94. [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")]
  95. public PlayCommand PlayCommand { get; set; }
  96. }
  97. [Route("/Sessions/{Id}/Playing/{Command}", "POST", Summary = "Issues a playstate command to a client")]
  98. public class SendPlaystateCommand : IReturnVoid
  99. {
  100. /// <summary>
  101. /// Gets or sets the id.
  102. /// </summary>
  103. /// <value>The id.</value>
  104. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  105. public Guid Id { get; set; }
  106. /// <summary>
  107. /// Gets or sets the position to seek to
  108. /// </summary>
  109. [ApiMember(Name = "SeekPositionTicks", Description = "The position to seek to.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  110. public long? SeekPositionTicks { get; set; }
  111. /// <summary>
  112. /// Gets or sets the play command.
  113. /// </summary>
  114. /// <value>The play command.</value>
  115. [ApiMember(Name = "Command", Description = "The command to send - stop, pause, unpause, nexttrack, previoustrack, seek, fullscreen.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  116. public PlaystateCommand Command { get; set; }
  117. }
  118. [Route("/Sessions/{Id}/System/{Command}", "POST", Summary = "Issues a system command to a client")]
  119. public class SendSystemCommand : IReturnVoid
  120. {
  121. /// <summary>
  122. /// Gets or sets the id.
  123. /// </summary>
  124. /// <value>The id.</value>
  125. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  126. public Guid Id { get; set; }
  127. /// <summary>
  128. /// Gets or sets the command.
  129. /// </summary>
  130. /// <value>The play command.</value>
  131. [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  132. public string Command { get; set; }
  133. }
  134. [Route("/Sessions/{Id}/Command/{Command}", "POST", Summary = "Issues a system command to a client")]
  135. public class SendGeneralCommand : IReturnVoid
  136. {
  137. /// <summary>
  138. /// Gets or sets the id.
  139. /// </summary>
  140. /// <value>The id.</value>
  141. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  142. public Guid Id { get; set; }
  143. /// <summary>
  144. /// Gets or sets the command.
  145. /// </summary>
  146. /// <value>The play command.</value>
  147. [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  148. public string Command { get; set; }
  149. }
  150. [Route("/Sessions/{Id}/Command", "POST", Summary = "Issues a system command to a client")]
  151. public class SendFullGeneralCommand : GeneralCommand, IReturnVoid
  152. {
  153. /// <summary>
  154. /// Gets or sets the id.
  155. /// </summary>
  156. /// <value>The id.</value>
  157. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  158. public Guid Id { get; set; }
  159. }
  160. [Route("/Sessions/{Id}/Message", "POST", Summary = "Issues a command to a client to display a message to the user")]
  161. public class SendMessageCommand : IReturnVoid
  162. {
  163. /// <summary>
  164. /// Gets or sets the id.
  165. /// </summary>
  166. /// <value>The id.</value>
  167. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  168. public Guid Id { get; set; }
  169. [ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  170. public string Text { get; set; }
  171. [ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  172. public string Header { get; set; }
  173. [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")]
  174. public long? TimeoutMs { get; set; }
  175. }
  176. [Route("/Sessions/{Id}/Users/{UserId}", "POST", Summary = "Adds an additional user to a session")]
  177. public class AddUserToSession : IReturnVoid
  178. {
  179. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  180. public Guid Id { get; set; }
  181. [ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  182. public Guid UserId { get; set; }
  183. }
  184. [Route("/Sessions/{Id}/Users/{UserId}", "DELETE", Summary = "Removes an additional user from a session")]
  185. public class RemoveUserFromSession : IReturnVoid
  186. {
  187. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  188. public Guid Id { get; set; }
  189. [ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  190. public Guid UserId { get; set; }
  191. }
  192. [Route("/Sessions/{Id}/Capabilities", "POST", Summary = "Updates capabilities for a device")]
  193. public class PostCapabilities : IReturnVoid
  194. {
  195. /// <summary>
  196. /// Gets or sets the id.
  197. /// </summary>
  198. /// <value>The id.</value>
  199. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  200. public Guid Id { get; set; }
  201. [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")]
  202. public string PlayableMediaTypes { get; set; }
  203. [ApiMember(Name = "SupportedCommands", Description = "A list of supported remote control commands, comma delimited", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  204. public string SupportedCommands { get; set; }
  205. }
  206. /// <summary>
  207. /// Class SessionsService
  208. /// </summary>
  209. public class SessionsService : BaseApiService
  210. {
  211. /// <summary>
  212. /// The _session manager
  213. /// </summary>
  214. private readonly ISessionManager _sessionManager;
  215. private readonly IUserManager _userManager;
  216. /// <summary>
  217. /// Initializes a new instance of the <see cref="SessionsService" /> class.
  218. /// </summary>
  219. /// <param name="sessionManager">The session manager.</param>
  220. /// <param name="userManager">The user manager.</param>
  221. public SessionsService(ISessionManager sessionManager, IUserManager userManager)
  222. {
  223. _sessionManager = sessionManager;
  224. _userManager = userManager;
  225. }
  226. /// <summary>
  227. /// Gets the specified request.
  228. /// </summary>
  229. /// <param name="request">The request.</param>
  230. /// <returns>System.Object.</returns>
  231. public object Get(GetSessions request)
  232. {
  233. var result = _sessionManager.Sessions.Where(i => i.IsActive);
  234. if (request.SupportsRemoteControl.HasValue)
  235. {
  236. result = result.Where(i => i.SupportsRemoteControl == request.SupportsRemoteControl.Value);
  237. }
  238. if (!string.IsNullOrEmpty(request.DeviceId))
  239. {
  240. result = result.Where(i => string.Equals(i.DeviceId, request.DeviceId, StringComparison.OrdinalIgnoreCase));
  241. }
  242. if (request.ControllableByUserId.HasValue)
  243. {
  244. var user = _userManager.GetUserById(request.ControllableByUserId.Value);
  245. if (!user.Configuration.EnableRemoteControlOfOtherUsers)
  246. {
  247. result = result.Where(i => !i.UserId.HasValue || i.ContainsUser(request.ControllableByUserId.Value));
  248. }
  249. }
  250. return ToOptimizedResult(result.Select(_sessionManager.GetSessionInfoDto).ToList());
  251. }
  252. public void Post(SendPlaystateCommand request)
  253. {
  254. var command = new PlaystateRequest
  255. {
  256. Command = request.Command,
  257. SeekPositionTicks = request.SeekPositionTicks
  258. };
  259. var task = _sessionManager.SendPlaystateCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  260. Task.WaitAll(task);
  261. }
  262. /// <summary>
  263. /// Posts the specified request.
  264. /// </summary>
  265. /// <param name="request">The request.</param>
  266. public void Post(BrowseTo request)
  267. {
  268. var command = new BrowseRequest
  269. {
  270. Context = request.Context,
  271. ItemId = request.ItemId,
  272. ItemName = request.ItemName,
  273. ItemType = request.ItemType
  274. };
  275. var task = _sessionManager.SendBrowseCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  276. Task.WaitAll(task);
  277. }
  278. /// <summary>
  279. /// Posts the specified request.
  280. /// </summary>
  281. /// <param name="request">The request.</param>
  282. public void Post(SendSystemCommand request)
  283. {
  284. GeneralCommandType commandType;
  285. if (Enum.TryParse(request.Command, true, out commandType))
  286. {
  287. var currentSession = GetSession();
  288. var command = new GeneralCommand
  289. {
  290. Name = commandType.ToString(),
  291. ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null
  292. };
  293. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, command, CancellationToken.None);
  294. Task.WaitAll(task);
  295. }
  296. }
  297. /// <summary>
  298. /// Posts the specified request.
  299. /// </summary>
  300. /// <param name="request">The request.</param>
  301. public void Post(SendMessageCommand request)
  302. {
  303. var command = new MessageCommand
  304. {
  305. Header = string.IsNullOrEmpty(request.Header) ? "Message from Server" : request.Header,
  306. TimeoutMs = request.TimeoutMs,
  307. Text = request.Text
  308. };
  309. var task = _sessionManager.SendMessageCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  310. Task.WaitAll(task);
  311. }
  312. /// <summary>
  313. /// Posts the specified request.
  314. /// </summary>
  315. /// <param name="request">The request.</param>
  316. public void Post(Play request)
  317. {
  318. var command = new PlayRequest
  319. {
  320. ItemIds = request.ItemIds.Split(',').ToArray(),
  321. PlayCommand = request.PlayCommand,
  322. StartPositionTicks = request.StartPositionTicks
  323. };
  324. var task = _sessionManager.SendPlayCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  325. Task.WaitAll(task);
  326. }
  327. public void Post(SendGeneralCommand request)
  328. {
  329. var currentSession = GetSession();
  330. var command = new GeneralCommand
  331. {
  332. Name = request.Command,
  333. ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null
  334. };
  335. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, command, CancellationToken.None);
  336. Task.WaitAll(task);
  337. }
  338. public void Post(SendFullGeneralCommand request)
  339. {
  340. var currentSession = GetSession();
  341. request.ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null;
  342. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, request, CancellationToken.None);
  343. Task.WaitAll(task);
  344. }
  345. public void Post(AddUserToSession request)
  346. {
  347. _sessionManager.AddAdditionalUser(request.Id, request.UserId);
  348. }
  349. public void Delete(RemoveUserFromSession request)
  350. {
  351. _sessionManager.RemoveAdditionalUser(request.Id, request.UserId);
  352. }
  353. public void Post(PostCapabilities request)
  354. {
  355. _sessionManager.ReportCapabilities(request.Id, new SessionCapabilities
  356. {
  357. PlayableMediaTypes = request.PlayableMediaTypes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(),
  358. SupportedCommands = request.SupportedCommands.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()
  359. });
  360. }
  361. private SessionInfo GetSession()
  362. {
  363. var auth = AuthorizationRequestFilterAttribute.GetAuthorization(Request);
  364. return _sessionManager.Sessions.First(i => string.Equals(i.DeviceId, auth.DeviceId) &&
  365. string.Equals(i.Client, auth.Client) &&
  366. string.Equals(i.ApplicationVersion, auth.Version));
  367. }
  368. }
  369. }