2014-05-28 15:51:42 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-12-24 00:00:27 +00:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
|
using MediaBrowser.Model.System;
|
|
|
|
|
using System;
|
2014-05-28 15:51:42 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-06-02 19:32:41 +00:00
|
|
|
|
using System.Globalization;
|
2014-05-28 15:51:42 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2013-12-24 00:00:27 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
|
namespace Emby.Server.Implementations.Session
|
2013-12-24 00:00:27 +00:00
|
|
|
|
{
|
2017-09-05 19:49:02 +00:00
|
|
|
|
public class HttpSessionController : ISessionController
|
2013-12-24 00:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IJsonSerializer _json;
|
2014-05-28 15:51:42 +00:00
|
|
|
|
private readonly ISessionManager _sessionManager;
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
|
|
|
|
public SessionInfo Session { get; private set; }
|
|
|
|
|
|
2014-05-12 18:04:25 +00:00
|
|
|
|
private readonly string _postUrl;
|
|
|
|
|
|
2014-05-18 19:58:42 +00:00
|
|
|
|
public HttpSessionController(IHttpClient httpClient,
|
|
|
|
|
IJsonSerializer json,
|
|
|
|
|
SessionInfo session,
|
2014-05-28 15:51:42 +00:00
|
|
|
|
string postUrl, ISessionManager sessionManager)
|
2013-12-24 00:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_json = json;
|
|
|
|
|
Session = session;
|
2014-05-12 18:04:25 +00:00
|
|
|
|
_postUrl = postUrl;
|
2014-05-28 15:51:42 +00:00
|
|
|
|
_sessionManager = sessionManager;
|
2013-12-24 00:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
|
private string PostUrl => string.Format("http://{0}{1}", Session.RemoteEndPoint, _postUrl);
|
2014-06-04 16:15:44 +00:00
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
|
public bool IsSessionActive => (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 5;
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
|
public bool SupportsMediaControl => true;
|
2014-05-17 18:37:40 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
private Task SendMessage(string name, string messageId, CancellationToken cancellationToken)
|
2014-05-18 19:58:42 +00:00
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
return SendMessage(name, messageId, new Dictionary<string, string>(), cancellationToken);
|
2014-05-18 19:58:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
private Task SendMessage(string name, string messageId, Dictionary<string, string> args, CancellationToken cancellationToken)
|
2014-05-18 19:58:42 +00:00
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
args["messageId"] = messageId;
|
2014-06-04 16:15:44 +00:00
|
|
|
|
var url = PostUrl + "/" + name + ToQueryString(args);
|
2014-05-18 19:58:42 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
return SendRequest(new HttpRequestOptions
|
2014-05-21 00:56:24 +00:00
|
|
|
|
{
|
|
|
|
|
Url = url,
|
2016-10-06 18:55:01 +00:00
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
BufferContent = false
|
2018-09-12 17:26:21 +00:00
|
|
|
|
});
|
2014-04-12 17:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
private Task SendPlayCommand(PlayRequest command, string messageId, CancellationToken cancellationToken)
|
2013-12-24 00:00:27 +00:00
|
|
|
|
{
|
2014-06-02 19:32:41 +00:00
|
|
|
|
var dict = new Dictionary<string, string>();
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
dict["ItemIds"] = string.Join(",", command.ItemIds.Select(i => i.ToString("N")).ToArray());
|
2014-06-02 19:32:41 +00:00
|
|
|
|
|
|
|
|
|
if (command.StartPositionTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
dict["StartPositionTicks"] = command.StartPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
2017-11-09 20:58:09 +00:00
|
|
|
|
if (command.AudioStreamIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
dict["AudioStreamIndex"] = command.AudioStreamIndex.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
|
|
|
|
if (command.SubtitleStreamIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
dict["SubtitleStreamIndex"] = command.SubtitleStreamIndex.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
2017-11-21 22:14:56 +00:00
|
|
|
|
if (command.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
dict["StartIndex"] = command.StartIndex.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(command.MediaSourceId))
|
2017-11-09 20:58:09 +00:00
|
|
|
|
{
|
|
|
|
|
dict["MediaSourceId"] = command.MediaSourceId;
|
|
|
|
|
}
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
return SendMessage(command.PlayCommand.ToString(), messageId, dict, cancellationToken);
|
2013-12-24 00:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
private Task SendPlaystateCommand(PlaystateRequest command, string messageId, CancellationToken cancellationToken)
|
2013-12-24 00:00:27 +00:00
|
|
|
|
{
|
2014-05-18 19:58:42 +00:00
|
|
|
|
var args = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
if (command.Command == PlaystateCommand.Seek)
|
2013-12-24 00:00:27 +00:00
|
|
|
|
{
|
2014-06-02 19:32:41 +00:00
|
|
|
|
if (!command.SeekPositionTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("SeekPositionTicks cannot be null");
|
|
|
|
|
}
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2014-08-27 03:25:39 +00:00
|
|
|
|
args["SeekPositionTicks"] = command.SeekPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
|
2014-05-18 19:58:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
return SendMessage(command.Command.ToString(), messageId, args, cancellationToken);
|
2013-12-24 00:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
private string[] _supportedMessages = new string[] { };
|
|
|
|
|
public Task SendMessage<T>(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken)
|
2013-12-24 00:00:27 +00:00
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
if (!IsSessionActive)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
if (string.Equals(name, "Play", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SendPlayCommand(data as PlayRequest, messageId, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(name, "PlayState", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return SendPlaystateCommand(data as PlaystateRequest, messageId, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
if (string.Equals(name, "GeneralCommand", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
var command = data as GeneralCommand;
|
|
|
|
|
return SendMessage(command.Name, messageId, command.Arguments, cancellationToken);
|
|
|
|
|
}
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
if (!_supportedMessages.Contains(name, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
var url = PostUrl + "/" + name;
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
url += "?messageId=" + messageId;
|
2013-12-24 00:00:27 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
BufferContent = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (data != null)
|
|
|
|
|
{
|
|
|
|
|
if (typeof(T) == typeof(string))
|
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
|
var str = data as string;
|
2018-09-12 17:26:21 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(str))
|
|
|
|
|
{
|
|
|
|
|
options.RequestContent = str;
|
|
|
|
|
options.RequestContentType = "application/json";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
options.RequestContent = _json.SerializeToString(data);
|
|
|
|
|
options.RequestContentType = "application/json";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SendRequest(options);
|
2014-03-28 19:58:18 +00:00
|
|
|
|
}
|
2014-05-18 19:58:42 +00:00
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
|
private async Task SendRequest(HttpRequestOptions options)
|
2015-01-13 03:46:44 +00:00
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
|
using (var response = await _httpClient.Post(options).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2015-01-13 03:46:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
|
private static string ToQueryString(Dictionary<string, string> nvc)
|
2014-05-18 19:58:42 +00:00
|
|
|
|
{
|
|
|
|
|
var array = (from item in nvc
|
|
|
|
|
select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
|
|
|
|
|
.ToArray();
|
2014-05-21 00:56:24 +00:00
|
|
|
|
|
|
|
|
|
var args = string.Join("&", array);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(args))
|
|
|
|
|
{
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "?" + args;
|
2014-05-18 19:58:42 +00:00
|
|
|
|
}
|
2013-12-24 00:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|