SessionsService.cs 18 KB

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