SessionsService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using MediaBrowser.Controller.Dto;
  2. using MediaBrowser.Controller.Session;
  3. using MediaBrowser.Model.Session;
  4. using ServiceStack.ServiceHost;
  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")]
  16. [Api(("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. }
  28. /// <summary>
  29. /// Class BrowseTo
  30. /// </summary>
  31. [Route("/Sessions/{Id}/Viewing", "POST")]
  32. [Api(("Instructs a session to browse to an item or view"))]
  33. public class BrowseTo : IReturnVoid
  34. {
  35. /// <summary>
  36. /// Gets or sets the id.
  37. /// </summary>
  38. /// <value>The id.</value>
  39. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  40. public Guid Id { get; set; }
  41. /// <summary>
  42. /// Artist, Genre, Studio, Person, or any kind of BaseItem
  43. /// </summary>
  44. /// <value>The type of the item.</value>
  45. [ApiMember(Name = "ItemType", Description = "The type of item to browse to.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  46. public string ItemType { get; set; }
  47. /// <summary>
  48. /// Artist name, genre name, item Id, etc
  49. /// </summary>
  50. /// <value>The item identifier.</value>
  51. [ApiMember(Name = "ItemId", Description = "The Id of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  52. public string ItemId { get; set; }
  53. /// <summary>
  54. /// Gets or sets the name of the item.
  55. /// </summary>
  56. /// <value>The name of the item.</value>
  57. [ApiMember(Name = "ItemName", Description = "The name of the item.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  58. public string ItemName { get; set; }
  59. /// <summary>
  60. /// Gets or sets the context (Movies, Music, TvShows, etc)
  61. /// Applicable to genres, studios and persons only because the context of items and artists can be inferred.
  62. /// This is optional to supply and clients are free to ignore it.
  63. /// </summary>
  64. /// <value>The context.</value>
  65. [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")]
  66. public string Context { get; set; }
  67. }
  68. [Route("/Sessions/{Id}/Playing", "POST")]
  69. [Api(("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")]
  98. [Api(("Issues a playstate command to a client"))]
  99. public class SendPlaystateCommand : IReturnVoid
  100. {
  101. /// <summary>
  102. /// Gets or sets the id.
  103. /// </summary>
  104. /// <value>The id.</value>
  105. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  106. public Guid Id { get; set; }
  107. /// <summary>
  108. /// Gets or sets the position to seek to
  109. /// </summary>
  110. [ApiMember(Name = "SeekPositionTicks", Description = "The position to seek to.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  111. public long? SeekPositionTicks { get; set; }
  112. /// <summary>
  113. /// Gets or sets the play command.
  114. /// </summary>
  115. /// <value>The play command.</value>
  116. [ApiMember(Name = "Command", Description = "The command to send - stop, pause, unpause, nexttrack, previoustrack, seek.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  117. public PlaystateCommand Command { get; set; }
  118. }
  119. [Route("/Sessions/{Id}/System/{Command}", "POST")]
  120. [Api(("Issues a system command to a client"))]
  121. public class SendSystemCommand : IReturnVoid
  122. {
  123. /// <summary>
  124. /// Gets or sets the id.
  125. /// </summary>
  126. /// <value>The id.</value>
  127. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  128. public Guid Id { get; set; }
  129. /// <summary>
  130. /// Gets or sets the command.
  131. /// </summary>
  132. /// <value>The play command.</value>
  133. [ApiMember(Name = "Command", Description = "The command to send.", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  134. public SystemCommand Command { get; set; }
  135. }
  136. [Route("/Sessions/{Id}/Message", "POST")]
  137. [Api(("Issues a command to a client to display a message to the user"))]
  138. public class SendMessageCommand : IReturnVoid
  139. {
  140. /// <summary>
  141. /// Gets or sets the id.
  142. /// </summary>
  143. /// <value>The id.</value>
  144. [ApiMember(Name = "Id", Description = "Session Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  145. public Guid Id { get; set; }
  146. [ApiMember(Name = "Text", Description = "The message text.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  147. public string Text { get; set; }
  148. [ApiMember(Name = "Header", Description = "The message header.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  149. public string Header { get; set; }
  150. [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")]
  151. public long? TimeoutMs { get; set; }
  152. }
  153. /// <summary>
  154. /// Class SessionsService
  155. /// </summary>
  156. public class SessionsService : BaseApiService
  157. {
  158. /// <summary>
  159. /// The _session manager
  160. /// </summary>
  161. private readonly ISessionManager _sessionManager;
  162. private readonly IDtoService _dtoService;
  163. /// <summary>
  164. /// Initializes a new instance of the <see cref="SessionsService" /> class.
  165. /// </summary>
  166. /// <param name="sessionManager">The session manager.</param>
  167. /// <param name="dtoService">The dto service.</param>
  168. public SessionsService(ISessionManager sessionManager, IDtoService dtoService)
  169. {
  170. _sessionManager = sessionManager;
  171. _dtoService = dtoService;
  172. }
  173. /// <summary>
  174. /// Gets the specified request.
  175. /// </summary>
  176. /// <param name="request">The request.</param>
  177. /// <returns>System.Object.</returns>
  178. public object Get(GetSessions request)
  179. {
  180. var result = _sessionManager.Sessions.Where(i => i.IsActive);
  181. if (request.SupportsRemoteControl.HasValue)
  182. {
  183. result = result.Where(i => i.SupportsRemoteControl == request.SupportsRemoteControl.Value);
  184. }
  185. return ToOptimizedResult(result.Select(_dtoService.GetSessionInfoDto).ToList());
  186. }
  187. public void Post(SendPlaystateCommand request)
  188. {
  189. var command = new PlaystateRequest
  190. {
  191. Command = request.Command,
  192. SeekPositionTicks = request.SeekPositionTicks
  193. };
  194. var task = _sessionManager.SendPlaystateCommand(request.Id, command, CancellationToken.None);
  195. Task.WaitAll(task);
  196. }
  197. /// <summary>
  198. /// Posts the specified request.
  199. /// </summary>
  200. /// <param name="request">The request.</param>
  201. public void Post(BrowseTo request)
  202. {
  203. var command = new BrowseRequest
  204. {
  205. Context = request.Context,
  206. ItemId = request.ItemId,
  207. ItemName = request.ItemName,
  208. ItemType = request.ItemType
  209. };
  210. var task = _sessionManager.SendBrowseCommand(request.Id, command, CancellationToken.None);
  211. Task.WaitAll(task);
  212. }
  213. /// <summary>
  214. /// Posts the specified request.
  215. /// </summary>
  216. /// <param name="request">The request.</param>
  217. public void Post(SendSystemCommand request)
  218. {
  219. var task = _sessionManager.SendSystemCommand(request.Id, request.Command, CancellationToken.None);
  220. Task.WaitAll(task);
  221. }
  222. /// <summary>
  223. /// Posts the specified request.
  224. /// </summary>
  225. /// <param name="request">The request.</param>
  226. public void Post(SendMessageCommand request)
  227. {
  228. var command = new MessageCommand
  229. {
  230. Header = string.IsNullOrEmpty(request.Header) ? "Message from Server" : request.Header,
  231. TimeoutMs = request.TimeoutMs,
  232. Text = request.Text
  233. };
  234. var task = _sessionManager.SendMessageCommand(request.Id, command, CancellationToken.None);
  235. Task.WaitAll(task);
  236. }
  237. /// <summary>
  238. /// Posts the specified request.
  239. /// </summary>
  240. /// <param name="request">The request.</param>
  241. public void Post(Play request)
  242. {
  243. var command = new PlayRequest
  244. {
  245. ItemIds = request.ItemIds.Split(',').ToArray(),
  246. PlayCommand = request.PlayCommand,
  247. StartPositionTicks = request.StartPositionTicks
  248. };
  249. var task = _sessionManager.SendPlayCommand(request.Id, command, CancellationToken.None);
  250. Task.WaitAll(task);
  251. }
  252. }
  253. }