SessionsService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using MediaBrowser.Controller.Library;
  2. using MediaBrowser.Controller.Net;
  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. [Authenticated]
  18. public class GetSessions : IReturn<List<SessionInfoDto>>
  19. {
  20. [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")]
  21. public Guid? ControllableByUserId { get; set; }
  22. [ApiMember(Name = "DeviceId", Description = "Optional. Filter by device id.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  23. public string DeviceId { get; set; }
  24. }
  25. /// <summary>
  26. /// Class DisplayContent
  27. /// </summary>
  28. [Route("/Sessions/{Id}/Viewing", "POST", Summary = "Instructs a session to browse to an item or view")]
  29. [Authenticated]
  30. public class DisplayContent : IReturnVoid
  31. {
  32. /// <summary>
  33. /// Gets or sets the id.
  34. /// </summary>
  35. /// <value>The id.</value>
  36. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  37. public string Id { get; set; }
  38. /// <summary>
  39. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  40. /// </summary>
  41. /// <value>The type of the item.</value>
  42. [ApiMember(Name = "ItemType", Description = "The type of item to browse to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  43. public string ItemType { get; set; }
  44. /// <summary>
  45. /// Artist name, genre name, item Id, etc
  46. /// </summary>
  47. /// <value>The item identifier.</value>
  48. [ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  49. public string ItemId { get; set; }
  50. /// <summary>
  51. /// Gets or sets the name of the item.
  52. /// </summary>
  53. /// <value>The name of the item.</value>
  54. [ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  55. public string ItemName { get; set; }
  56. }
  57. [Route("/Sessions/{Id}/Playing", "POST", Summary = "Instructs a session to play an item")]
  58. [Authenticated]
  59. public class Play : IReturnVoid
  60. {
  61. /// <summary>
  62. /// Gets or sets the id.
  63. /// </summary>
  64. /// <value>The id.</value>
  65. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  66. public string Id { get; set; }
  67. /// <summary>
  68. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  69. /// </summary>
  70. /// <value>The type of the item.</value>
  71. [ApiMember(Name = "ItemIds", Description = "The ids of the items to play, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  72. public string ItemIds { get; set; }
  73. /// <summary>
  74. /// Gets or sets the start position ticks that the first item should be played at
  75. /// </summary>
  76. /// <value>The start position ticks.</value>
  77. [ApiMember(Name = "StartPositionTicks", Description = "The starting position of the first item.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  78. public long? StartPositionTicks { get; set; }
  79. /// <summary>
  80. /// Gets or sets the play command.
  81. /// </summary>
  82. /// <value>The play command.</value>
  83. [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")]
  84. public PlayCommand PlayCommand { get; set; }
  85. }
  86. [Route("/Sessions/{Id}/Playing/{Command}", "POST", Summary = "Issues a playstate command to a client")]
  87. [Authenticated]
  88. public class SendPlaystateCommand : IReturnVoid
  89. {
  90. /// <summary>
  91. /// Gets or sets the id.
  92. /// </summary>
  93. /// <value>The id.</value>
  94. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  95. public string Id { get; set; }
  96. /// <summary>
  97. /// Gets or sets the position to seek to
  98. /// </summary>
  99. [ApiMember(Name = "SeekPositionTicks", Description = "The position to seek to.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  100. public long? SeekPositionTicks { get; set; }
  101. /// <summary>
  102. /// Gets or sets the play command.
  103. /// </summary>
  104. /// <value>The play command.</value>
  105. [ApiMember(Name = "Command", Description = "The command to send - stop, pause, unpause, nexttrack, previoustrack, seek, fullscreen.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  106. public PlaystateCommand Command { get; set; }
  107. }
  108. [Route("/Sessions/{Id}/System/{Command}", "POST", Summary = "Issues a system command to a client")]
  109. [Authenticated]
  110. public class SendSystemCommand : IReturnVoid
  111. {
  112. /// <summary>
  113. /// Gets or sets the id.
  114. /// </summary>
  115. /// <value>The id.</value>
  116. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  117. public string Id { get; set; }
  118. /// <summary>
  119. /// Gets or sets the command.
  120. /// </summary>
  121. /// <value>The play command.</value>
  122. [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  123. public string Command { get; set; }
  124. }
  125. [Route("/Sessions/{Id}/Command/{Command}", "POST", Summary = "Issues a system command to a client")]
  126. [Authenticated]
  127. public class SendGeneralCommand : IReturnVoid
  128. {
  129. /// <summary>
  130. /// Gets or sets the id.
  131. /// </summary>
  132. /// <value>The id.</value>
  133. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  134. public string Id { get; set; }
  135. /// <summary>
  136. /// Gets or sets the command.
  137. /// </summary>
  138. /// <value>The play command.</value>
  139. [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  140. public string Command { get; set; }
  141. }
  142. [Route("/Sessions/{Id}/Command", "POST", Summary = "Issues a system command to a client")]
  143. [Authenticated]
  144. public class SendFullGeneralCommand : GeneralCommand, IReturnVoid
  145. {
  146. /// <summary>
  147. /// Gets or sets the id.
  148. /// </summary>
  149. /// <value>The id.</value>
  150. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  151. public string Id { get; set; }
  152. }
  153. [Route("/Sessions/{Id}/Message", "POST", Summary = "Issues a command to a client to display a message to the user")]
  154. [Authenticated]
  155. public class SendMessageCommand : IReturnVoid
  156. {
  157. /// <summary>
  158. /// Gets or sets the id.
  159. /// </summary>
  160. /// <value>The id.</value>
  161. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  162. public string Id { get; set; }
  163. [ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  164. public string Text { get; set; }
  165. [ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  166. public string Header { get; set; }
  167. [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")]
  168. public long? TimeoutMs { get; set; }
  169. }
  170. [Route("/Sessions/{Id}/Users/{UserId}", "POST", Summary = "Adds an additional user to a session")]
  171. [Authenticated]
  172. public class AddUserToSession : IReturnVoid
  173. {
  174. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  175. public string Id { get; set; }
  176. [ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  177. public Guid UserId { get; set; }
  178. }
  179. [Route("/Sessions/{Id}/Users/{UserId}", "DELETE", Summary = "Removes an additional user from a session")]
  180. [Authenticated]
  181. public class RemoveUserFromSession : IReturnVoid
  182. {
  183. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  184. public string Id { get; set; }
  185. [ApiMember(Name = "UserId", Description = "UserId Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  186. public Guid UserId { get; set; }
  187. }
  188. [Route("/Sessions/Capabilities", "POST", Summary = "Updates capabilities for a device")]
  189. public class PostCapabilities : IReturnVoid
  190. {
  191. /// <summary>
  192. /// Gets or sets the id.
  193. /// </summary>
  194. /// <value>The id.</value>
  195. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  196. public string Id { get; set; }
  197. [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")]
  198. public string PlayableMediaTypes { get; set; }
  199. [ApiMember(Name = "SupportedCommands", Description = "A list of supported remote control commands, comma delimited", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  200. public string SupportedCommands { get; set; }
  201. [ApiMember(Name = "MessageCallbackUrl", Description = "A url to post messages to, including remote control commands.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  202. public string MessageCallbackUrl { get; set; }
  203. [ApiMember(Name = "SupportsMediaControl", Description = "Determines whether media can be played remotely.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
  204. public bool SupportsMediaControl { 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 (!string.IsNullOrEmpty(request.DeviceId))
  235. {
  236. result = result.Where(i => string.Equals(i.DeviceId, request.DeviceId, StringComparison.OrdinalIgnoreCase));
  237. }
  238. if (request.ControllableByUserId.HasValue)
  239. {
  240. result = result.Where(i => i.SupportsMediaControl);
  241. var user = _userManager.GetUserById(request.ControllableByUserId.Value);
  242. if (!user.Configuration.EnableRemoteControlOfOtherUsers)
  243. {
  244. result = result.Where(i => !i.UserId.HasValue || i.ContainsUser(request.ControllableByUserId.Value));
  245. }
  246. }
  247. return ToOptimizedResult(result.Select(_sessionManager.GetSessionInfoDto).ToList());
  248. }
  249. public void Post(SendPlaystateCommand request)
  250. {
  251. var command = new PlaystateRequest
  252. {
  253. Command = request.Command,
  254. SeekPositionTicks = request.SeekPositionTicks
  255. };
  256. var task = _sessionManager.SendPlaystateCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  257. Task.WaitAll(task);
  258. }
  259. /// <summary>
  260. /// Posts the specified request.
  261. /// </summary>
  262. /// <param name="request">The request.</param>
  263. public void Post(DisplayContent request)
  264. {
  265. var command = new BrowseRequest
  266. {
  267. ItemId = request.ItemId,
  268. ItemName = request.ItemName,
  269. ItemType = request.ItemType
  270. };
  271. var task = _sessionManager.SendBrowseCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  272. Task.WaitAll(task);
  273. }
  274. /// <summary>
  275. /// Posts the specified request.
  276. /// </summary>
  277. /// <param name="request">The request.</param>
  278. public void Post(SendSystemCommand request)
  279. {
  280. GeneralCommandType commandType;
  281. if (Enum.TryParse(request.Command, true, out commandType))
  282. {
  283. var currentSession = GetSession();
  284. var command = new GeneralCommand
  285. {
  286. Name = commandType.ToString(),
  287. ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null
  288. };
  289. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, command, CancellationToken.None);
  290. Task.WaitAll(task);
  291. }
  292. }
  293. /// <summary>
  294. /// Posts the specified request.
  295. /// </summary>
  296. /// <param name="request">The request.</param>
  297. public void Post(SendMessageCommand request)
  298. {
  299. var command = new MessageCommand
  300. {
  301. Header = string.IsNullOrEmpty(request.Header) ? "Message from Server" : request.Header,
  302. TimeoutMs = request.TimeoutMs,
  303. Text = request.Text
  304. };
  305. var task = _sessionManager.SendMessageCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  306. Task.WaitAll(task);
  307. }
  308. /// <summary>
  309. /// Posts the specified request.
  310. /// </summary>
  311. /// <param name="request">The request.</param>
  312. public void Post(Play request)
  313. {
  314. var command = new PlayRequest
  315. {
  316. ItemIds = request.ItemIds.Split(',').ToArray(),
  317. PlayCommand = request.PlayCommand,
  318. StartPositionTicks = request.StartPositionTicks
  319. };
  320. var task = _sessionManager.SendPlayCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  321. Task.WaitAll(task);
  322. }
  323. public void Post(SendGeneralCommand request)
  324. {
  325. var currentSession = GetSession();
  326. var command = new GeneralCommand
  327. {
  328. Name = request.Command,
  329. ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null
  330. };
  331. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, command, CancellationToken.None);
  332. Task.WaitAll(task);
  333. }
  334. public void Post(SendFullGeneralCommand request)
  335. {
  336. var currentSession = GetSession();
  337. request.ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null;
  338. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, request, CancellationToken.None);
  339. Task.WaitAll(task);
  340. }
  341. public void Post(AddUserToSession request)
  342. {
  343. _sessionManager.AddAdditionalUser(request.Id, request.UserId);
  344. }
  345. public void Delete(RemoveUserFromSession request)
  346. {
  347. _sessionManager.RemoveAdditionalUser(request.Id, request.UserId);
  348. }
  349. public void Post(PostCapabilities request)
  350. {
  351. if (string.IsNullOrWhiteSpace(request.Id))
  352. {
  353. request.Id = GetSession().Id;
  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. SupportsMediaControl = request.SupportsMediaControl,
  360. MessageCallbackUrl = request.MessageCallbackUrl
  361. });
  362. }
  363. }
  364. }