SessionsService.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. [Authenticated]
  190. public class PostCapabilities : IReturnVoid
  191. {
  192. /// <summary>
  193. /// Gets or sets the id.
  194. /// </summary>
  195. /// <value>The id.</value>
  196. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  197. public string Id { get; set; }
  198. [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")]
  199. public string PlayableMediaTypes { get; set; }
  200. [ApiMember(Name = "SupportedCommands", Description = "A list of supported remote control commands, comma delimited", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  201. public string SupportedCommands { get; set; }
  202. [ApiMember(Name = "MessageCallbackUrl", Description = "A url to post messages to, including remote control commands.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  203. public string MessageCallbackUrl { get; set; }
  204. [ApiMember(Name = "SupportsMediaControl", Description = "Determines whether media can be played remotely.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
  205. public bool SupportsMediaControl { get; set; }
  206. }
  207. [Route("/Sessions/Logout", "POST", Summary = "Reports that a session has ended")]
  208. public class ReportSessionEnded : IReturnVoid
  209. {
  210. }
  211. /// <summary>
  212. /// Class SessionsService
  213. /// </summary>
  214. public class SessionsService : BaseApiService
  215. {
  216. /// <summary>
  217. /// The _session manager
  218. /// </summary>
  219. private readonly ISessionManager _sessionManager;
  220. private readonly IUserManager _userManager;
  221. private readonly IAuthorizationContext _authContext;
  222. /// <summary>
  223. /// Initializes a new instance of the <see cref="SessionsService" /> class.
  224. /// </summary>
  225. /// <param name="sessionManager">The session manager.</param>
  226. /// <param name="userManager">The user manager.</param>
  227. public SessionsService(ISessionManager sessionManager, IUserManager userManager, IAuthorizationContext authContext)
  228. {
  229. _sessionManager = sessionManager;
  230. _userManager = userManager;
  231. _authContext = authContext;
  232. }
  233. public void Post(ReportSessionEnded request)
  234. {
  235. var auth = _authContext.GetAuthorizationInfo(Request);
  236. _sessionManager.Logout(auth.Token);
  237. }
  238. /// <summary>
  239. /// Gets the specified request.
  240. /// </summary>
  241. /// <param name="request">The request.</param>
  242. /// <returns>System.Object.</returns>
  243. public object Get(GetSessions request)
  244. {
  245. var result = _sessionManager.Sessions.Where(i => i.IsActive);
  246. if (!string.IsNullOrEmpty(request.DeviceId))
  247. {
  248. result = result.Where(i => string.Equals(i.DeviceId, request.DeviceId, StringComparison.OrdinalIgnoreCase));
  249. }
  250. if (request.ControllableByUserId.HasValue)
  251. {
  252. result = result.Where(i => i.SupportsMediaControl);
  253. var user = _userManager.GetUserById(request.ControllableByUserId.Value);
  254. if (!user.Configuration.EnableRemoteControlOfOtherUsers)
  255. {
  256. result = result.Where(i => !i.UserId.HasValue || i.ContainsUser(request.ControllableByUserId.Value));
  257. }
  258. }
  259. return ToOptimizedResult(result.Select(_sessionManager.GetSessionInfoDto).ToList());
  260. }
  261. public void Post(SendPlaystateCommand request)
  262. {
  263. var command = new PlaystateRequest
  264. {
  265. Command = request.Command,
  266. SeekPositionTicks = request.SeekPositionTicks
  267. };
  268. var task = _sessionManager.SendPlaystateCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  269. Task.WaitAll(task);
  270. }
  271. /// <summary>
  272. /// Posts the specified request.
  273. /// </summary>
  274. /// <param name="request">The request.</param>
  275. public void Post(DisplayContent request)
  276. {
  277. var command = new BrowseRequest
  278. {
  279. ItemId = request.ItemId,
  280. ItemName = request.ItemName,
  281. ItemType = request.ItemType
  282. };
  283. var task = _sessionManager.SendBrowseCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  284. Task.WaitAll(task);
  285. }
  286. /// <summary>
  287. /// Posts the specified request.
  288. /// </summary>
  289. /// <param name="request">The request.</param>
  290. public void Post(SendSystemCommand request)
  291. {
  292. GeneralCommandType commandType;
  293. if (Enum.TryParse(request.Command, true, out commandType))
  294. {
  295. var currentSession = GetSession();
  296. var command = new GeneralCommand
  297. {
  298. Name = commandType.ToString(),
  299. ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null
  300. };
  301. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, command, CancellationToken.None);
  302. Task.WaitAll(task);
  303. }
  304. }
  305. /// <summary>
  306. /// Posts the specified request.
  307. /// </summary>
  308. /// <param name="request">The request.</param>
  309. public void Post(SendMessageCommand request)
  310. {
  311. var command = new MessageCommand
  312. {
  313. Header = string.IsNullOrEmpty(request.Header) ? "Message from Server" : request.Header,
  314. TimeoutMs = request.TimeoutMs,
  315. Text = request.Text
  316. };
  317. var task = _sessionManager.SendMessageCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  318. Task.WaitAll(task);
  319. }
  320. /// <summary>
  321. /// Posts the specified request.
  322. /// </summary>
  323. /// <param name="request">The request.</param>
  324. public void Post(Play request)
  325. {
  326. var command = new PlayRequest
  327. {
  328. ItemIds = request.ItemIds.Split(',').ToArray(),
  329. PlayCommand = request.PlayCommand,
  330. StartPositionTicks = request.StartPositionTicks
  331. };
  332. var task = _sessionManager.SendPlayCommand(GetSession().Id, request.Id, command, CancellationToken.None);
  333. Task.WaitAll(task);
  334. }
  335. public void Post(SendGeneralCommand request)
  336. {
  337. var currentSession = GetSession();
  338. var command = new GeneralCommand
  339. {
  340. Name = request.Command,
  341. ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null
  342. };
  343. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, command, CancellationToken.None);
  344. Task.WaitAll(task);
  345. }
  346. public void Post(SendFullGeneralCommand request)
  347. {
  348. var currentSession = GetSession();
  349. request.ControllingUserId = currentSession.UserId.HasValue ? currentSession.UserId.Value.ToString("N") : null;
  350. var task = _sessionManager.SendGeneralCommand(currentSession.Id, request.Id, request, CancellationToken.None);
  351. Task.WaitAll(task);
  352. }
  353. public void Post(AddUserToSession request)
  354. {
  355. _sessionManager.AddAdditionalUser(request.Id, request.UserId);
  356. }
  357. public void Delete(RemoveUserFromSession request)
  358. {
  359. _sessionManager.RemoveAdditionalUser(request.Id, request.UserId);
  360. }
  361. public void Post(PostCapabilities request)
  362. {
  363. if (string.IsNullOrWhiteSpace(request.Id))
  364. {
  365. request.Id = GetSession().Id;
  366. }
  367. _sessionManager.ReportCapabilities(request.Id, new SessionCapabilities
  368. {
  369. PlayableMediaTypes = request.PlayableMediaTypes.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(),
  370. SupportedCommands = request.SupportedCommands.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(),
  371. SupportsMediaControl = request.SupportsMediaControl,
  372. MessageCallbackUrl = request.MessageCallbackUrl
  373. });
  374. }
  375. }
  376. }