HttpSessionController.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Session;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Serialization;
  5. using MediaBrowser.Model.Session;
  6. using MediaBrowser.Model.System;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace Emby.Server.Implementations.Session
  15. {
  16. public class HttpSessionController : ISessionController
  17. {
  18. private readonly IHttpClient _httpClient;
  19. private readonly IJsonSerializer _json;
  20. private readonly ISessionManager _sessionManager;
  21. public SessionInfo Session { get; private set; }
  22. private readonly string _postUrl;
  23. public HttpSessionController(IHttpClient httpClient,
  24. IJsonSerializer json,
  25. SessionInfo session,
  26. string postUrl, ISessionManager sessionManager)
  27. {
  28. _httpClient = httpClient;
  29. _json = json;
  30. Session = session;
  31. _postUrl = postUrl;
  32. _sessionManager = sessionManager;
  33. }
  34. private string PostUrl
  35. {
  36. get
  37. {
  38. return string.Format("http://{0}{1}", Session.RemoteEndPoint, _postUrl);
  39. }
  40. }
  41. public bool IsSessionActive
  42. {
  43. get
  44. {
  45. return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 5;
  46. }
  47. }
  48. public bool SupportsMediaControl
  49. {
  50. get { return true; }
  51. }
  52. private Task SendMessage(string name, string messageId, CancellationToken cancellationToken)
  53. {
  54. return SendMessage(name, messageId, new Dictionary<string, string>(), cancellationToken);
  55. }
  56. private Task SendMessage(string name, string messageId, Dictionary<string, string> args, CancellationToken cancellationToken)
  57. {
  58. args["messageId"] = messageId;
  59. var url = PostUrl + "/" + name + ToQueryString(args);
  60. return SendRequest(new HttpRequestOptions
  61. {
  62. Url = url,
  63. CancellationToken = cancellationToken,
  64. BufferContent = false
  65. });
  66. }
  67. private Task SendPlayCommand(PlayRequest command, string messageId, CancellationToken cancellationToken)
  68. {
  69. var dict = new Dictionary<string, string>();
  70. dict["ItemIds"] = string.Join(",", command.ItemIds.Select(i => i.ToString("N")).ToArray());
  71. if (command.StartPositionTicks.HasValue)
  72. {
  73. dict["StartPositionTicks"] = command.StartPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  74. }
  75. if (command.AudioStreamIndex.HasValue)
  76. {
  77. dict["AudioStreamIndex"] = command.AudioStreamIndex.Value.ToString(CultureInfo.InvariantCulture);
  78. }
  79. if (command.SubtitleStreamIndex.HasValue)
  80. {
  81. dict["SubtitleStreamIndex"] = command.SubtitleStreamIndex.Value.ToString(CultureInfo.InvariantCulture);
  82. }
  83. if (command.StartIndex.HasValue)
  84. {
  85. dict["StartIndex"] = command.StartIndex.Value.ToString(CultureInfo.InvariantCulture);
  86. }
  87. if (!string.IsNullOrEmpty(command.MediaSourceId))
  88. {
  89. dict["MediaSourceId"] = command.MediaSourceId;
  90. }
  91. return SendMessage(command.PlayCommand.ToString(), messageId, dict, cancellationToken);
  92. }
  93. private Task SendPlaystateCommand(PlaystateRequest command, string messageId, CancellationToken cancellationToken)
  94. {
  95. var args = new Dictionary<string, string>();
  96. if (command.Command == PlaystateCommand.Seek)
  97. {
  98. if (!command.SeekPositionTicks.HasValue)
  99. {
  100. throw new ArgumentException("SeekPositionTicks cannot be null");
  101. }
  102. args["SeekPositionTicks"] = command.SeekPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  103. }
  104. return SendMessage(command.Command.ToString(), messageId, args, cancellationToken);
  105. }
  106. private string[] _supportedMessages = new string[] { };
  107. public Task SendMessage<T>(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken)
  108. {
  109. if (!IsSessionActive)
  110. {
  111. return Task.CompletedTask;
  112. }
  113. if (string.Equals(name, "Play", StringComparison.OrdinalIgnoreCase))
  114. {
  115. return SendPlayCommand(data as PlayRequest, messageId, cancellationToken);
  116. }
  117. if (string.Equals(name, "PlayState", StringComparison.OrdinalIgnoreCase))
  118. {
  119. return SendPlaystateCommand(data as PlaystateRequest, messageId, cancellationToken);
  120. }
  121. if (string.Equals(name, "GeneralCommand", StringComparison.OrdinalIgnoreCase))
  122. {
  123. var command = data as GeneralCommand;
  124. return SendMessage(command.Name, messageId, command.Arguments, cancellationToken);
  125. }
  126. if (!_supportedMessages.Contains(name, StringComparer.OrdinalIgnoreCase))
  127. {
  128. return Task.CompletedTask;
  129. }
  130. var url = PostUrl + "/" + name;
  131. url += "?messageId=" + messageId;
  132. var options = new HttpRequestOptions
  133. {
  134. Url = url,
  135. CancellationToken = cancellationToken,
  136. BufferContent = false
  137. };
  138. if (data != null)
  139. {
  140. if (typeof(T) == typeof(string))
  141. {
  142. var str = data as String;
  143. if (!string.IsNullOrEmpty(str))
  144. {
  145. options.RequestContent = str;
  146. options.RequestContentType = "application/json";
  147. }
  148. }
  149. else
  150. {
  151. options.RequestContent = _json.SerializeToString(data);
  152. options.RequestContentType = "application/json";
  153. }
  154. }
  155. return SendRequest(options);
  156. }
  157. private async Task SendRequest(HttpRequestOptions options)
  158. {
  159. using (var response = await _httpClient.Post(options).ConfigureAwait(false))
  160. {
  161. }
  162. }
  163. private string ToQueryString(Dictionary<string, string> nvc)
  164. {
  165. var array = (from item in nvc
  166. select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
  167. .ToArray();
  168. var args = string.Join("&", array);
  169. if (string.IsNullOrEmpty(args))
  170. {
  171. return args;
  172. }
  173. return "?" + args;
  174. }
  175. }
  176. }