HttpSessionController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 => string.Format("http://{0}{1}", Session.RemoteEndPoint, _postUrl);
  35. public bool IsSessionActive => (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 5;
  36. public bool SupportsMediaControl => true;
  37. private Task SendMessage(string name, string messageId, CancellationToken cancellationToken)
  38. {
  39. return SendMessage(name, messageId, new Dictionary<string, string>(), cancellationToken);
  40. }
  41. private Task SendMessage(string name, string messageId, Dictionary<string, string> args, CancellationToken cancellationToken)
  42. {
  43. args["messageId"] = messageId;
  44. var url = PostUrl + "/" + name + ToQueryString(args);
  45. return SendRequest(new HttpRequestOptions
  46. {
  47. Url = url,
  48. CancellationToken = cancellationToken,
  49. BufferContent = false
  50. });
  51. }
  52. private Task SendPlayCommand(PlayRequest command, string messageId, CancellationToken cancellationToken)
  53. {
  54. var dict = new Dictionary<string, string>();
  55. dict["ItemIds"] = string.Join(",", command.ItemIds.Select(i => i.ToString("N")).ToArray());
  56. if (command.StartPositionTicks.HasValue)
  57. {
  58. dict["StartPositionTicks"] = command.StartPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  59. }
  60. if (command.AudioStreamIndex.HasValue)
  61. {
  62. dict["AudioStreamIndex"] = command.AudioStreamIndex.Value.ToString(CultureInfo.InvariantCulture);
  63. }
  64. if (command.SubtitleStreamIndex.HasValue)
  65. {
  66. dict["SubtitleStreamIndex"] = command.SubtitleStreamIndex.Value.ToString(CultureInfo.InvariantCulture);
  67. }
  68. if (command.StartIndex.HasValue)
  69. {
  70. dict["StartIndex"] = command.StartIndex.Value.ToString(CultureInfo.InvariantCulture);
  71. }
  72. if (!string.IsNullOrEmpty(command.MediaSourceId))
  73. {
  74. dict["MediaSourceId"] = command.MediaSourceId;
  75. }
  76. return SendMessage(command.PlayCommand.ToString(), messageId, dict, cancellationToken);
  77. }
  78. private Task SendPlaystateCommand(PlaystateRequest command, string messageId, CancellationToken cancellationToken)
  79. {
  80. var args = new Dictionary<string, string>();
  81. if (command.Command == PlaystateCommand.Seek)
  82. {
  83. if (!command.SeekPositionTicks.HasValue)
  84. {
  85. throw new ArgumentException("SeekPositionTicks cannot be null");
  86. }
  87. args["SeekPositionTicks"] = command.SeekPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
  88. }
  89. return SendMessage(command.Command.ToString(), messageId, args, cancellationToken);
  90. }
  91. private string[] _supportedMessages = new string[] { };
  92. public Task SendMessage<T>(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken)
  93. {
  94. if (!IsSessionActive)
  95. {
  96. return Task.CompletedTask;
  97. }
  98. if (string.Equals(name, "Play", StringComparison.OrdinalIgnoreCase))
  99. {
  100. return SendPlayCommand(data as PlayRequest, messageId, cancellationToken);
  101. }
  102. if (string.Equals(name, "PlayState", StringComparison.OrdinalIgnoreCase))
  103. {
  104. return SendPlaystateCommand(data as PlaystateRequest, messageId, cancellationToken);
  105. }
  106. if (string.Equals(name, "GeneralCommand", StringComparison.OrdinalIgnoreCase))
  107. {
  108. var command = data as GeneralCommand;
  109. return SendMessage(command.Name, messageId, command.Arguments, cancellationToken);
  110. }
  111. if (!_supportedMessages.Contains(name, StringComparer.OrdinalIgnoreCase))
  112. {
  113. return Task.CompletedTask;
  114. }
  115. var url = PostUrl + "/" + name;
  116. url += "?messageId=" + messageId;
  117. var options = new HttpRequestOptions
  118. {
  119. Url = url,
  120. CancellationToken = cancellationToken,
  121. BufferContent = false
  122. };
  123. if (data != null)
  124. {
  125. if (typeof(T) == typeof(string))
  126. {
  127. var str = data as string;
  128. if (!string.IsNullOrEmpty(str))
  129. {
  130. options.RequestContent = str;
  131. options.RequestContentType = "application/json";
  132. }
  133. }
  134. else
  135. {
  136. options.RequestContent = _json.SerializeToString(data);
  137. options.RequestContentType = "application/json";
  138. }
  139. }
  140. return SendRequest(options);
  141. }
  142. private async Task SendRequest(HttpRequestOptions options)
  143. {
  144. using (var response = await _httpClient.Post(options).ConfigureAwait(false))
  145. {
  146. }
  147. }
  148. private static string ToQueryString(Dictionary<string, string> nvc)
  149. {
  150. var array = (from item in nvc
  151. select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
  152. .ToArray();
  153. var args = string.Join("&", array);
  154. if (string.IsNullOrEmpty(args))
  155. {
  156. return args;
  157. }
  158. return "?" + args;
  159. }
  160. }
  161. }