2014-05-28 08:51:42 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-12-23 17:00:27 -07: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 08:51:42 -07:00
|
|
|
|
using System.Collections.Generic;
|
2014-06-02 12:32:41 -07:00
|
|
|
|
using System.Globalization;
|
2014-05-28 08:51:42 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
2013-12-23 17:00:27 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
|
namespace Emby.Server.Implementations.Session
|
2013-12-23 17:00:27 -07:00
|
|
|
|
{
|
2017-09-05 12:49:02 -07:00
|
|
|
|
public class HttpSessionController : ISessionController
|
2013-12-23 17:00:27 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IJsonSerializer _json;
|
2014-05-28 08:51:42 -07:00
|
|
|
|
private readonly ISessionManager _sessionManager;
|
2013-12-23 17:00:27 -07:00
|
|
|
|
|
|
|
|
|
public SessionInfo Session { get; private set; }
|
|
|
|
|
|
2014-05-12 11:04:25 -07:00
|
|
|
|
private readonly string _postUrl;
|
|
|
|
|
|
2014-05-18 12:58:42 -07:00
|
|
|
|
public HttpSessionController(IHttpClient httpClient,
|
|
|
|
|
IJsonSerializer json,
|
|
|
|
|
SessionInfo session,
|
2014-05-28 08:51:42 -07:00
|
|
|
|
string postUrl, ISessionManager sessionManager)
|
2013-12-23 17:00:27 -07:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_json = json;
|
|
|
|
|
Session = session;
|
2014-05-12 11:04:25 -07:00
|
|
|
|
_postUrl = postUrl;
|
2014-05-28 08:51:42 -07:00
|
|
|
|
_sessionManager = sessionManager;
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-21 09:10:02 -07:00
|
|
|
|
public void OnActivity()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-04 09:15:44 -07:00
|
|
|
|
private string PostUrl
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return string.Format("http://{0}{1}", Session.RemoteEndPoint, _postUrl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-23 17:00:27 -07:00
|
|
|
|
public bool IsSessionActive
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-06-23 15:13:06 -07:00
|
|
|
|
return (DateTime.UtcNow - Session.LastActivityDate).TotalMinutes <= 10;
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-17 11:37:40 -07:00
|
|
|
|
public bool SupportsMediaControl
|
|
|
|
|
{
|
|
|
|
|
get { return true; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-18 12:58:42 -07:00
|
|
|
|
private Task SendMessage(string name, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-20 17:56:24 -07:00
|
|
|
|
return SendMessage(name, new Dictionary<string, string>(), cancellationToken);
|
2014-05-18 12:58:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 09:16:56 -07:00
|
|
|
|
private async Task SendMessage(string name,
|
2014-12-15 22:01:57 -07:00
|
|
|
|
Dictionary<string, string> args,
|
2014-05-28 08:51:42 -07:00
|
|
|
|
CancellationToken cancellationToken)
|
2014-05-18 12:58:42 -07:00
|
|
|
|
{
|
2014-06-04 09:15:44 -07:00
|
|
|
|
var url = PostUrl + "/" + name + ToQueryString(args);
|
2014-05-18 12:58:42 -07:00
|
|
|
|
|
2017-10-20 09:16:56 -07:00
|
|
|
|
using ((await _httpClient.Post(new HttpRequestOptions
|
2014-05-20 17:56:24 -07:00
|
|
|
|
{
|
|
|
|
|
Url = url,
|
2016-10-06 11:55:01 -07:00
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
BufferContent = false
|
2014-05-28 08:51:42 -07:00
|
|
|
|
|
2017-10-20 09:16:56 -07:00
|
|
|
|
}).ConfigureAwait(false)))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2014-05-18 12:58:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 10:53:23 -07:00
|
|
|
|
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 10:27:53 -07:00
|
|
|
|
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-23 17:00:27 -07:00
|
|
|
|
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-06-02 12:32:41 -07:00
|
|
|
|
var dict = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
dict["ItemIds"] = string.Join(",", command.ItemIds);
|
|
|
|
|
|
|
|
|
|
if (command.StartPositionTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
dict["StartPositionTicks"] = command.StartPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
|
|
|
|
|
}
|
2017-11-09 13:58:09 -07: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);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(command.MediaSourceId))
|
|
|
|
|
{
|
|
|
|
|
dict["MediaSourceId"] = command.MediaSourceId;
|
|
|
|
|
}
|
2013-12-23 17:00:27 -07:00
|
|
|
|
|
2014-06-02 12:32:41 -07:00
|
|
|
|
return SendMessage(command.PlayCommand.ToString(), dict, cancellationToken);
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-18 12:58:42 -07:00
|
|
|
|
var args = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
|
if (command.Command == PlaystateCommand.Seek)
|
2013-12-23 17:00:27 -07:00
|
|
|
|
{
|
2014-06-02 12:32:41 -07:00
|
|
|
|
if (!command.SeekPositionTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("SeekPositionTicks cannot be null");
|
|
|
|
|
}
|
2013-12-23 17:00:27 -07:00
|
|
|
|
|
2014-08-26 20:25:39 -07:00
|
|
|
|
args["SeekPositionTicks"] = command.SeekPositionTicks.Value.ToString(CultureInfo.InvariantCulture);
|
2014-05-18 12:58:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-26 20:25:39 -07:00
|
|
|
|
return SendMessage(command.Command.ToString(), args, cancellationToken);
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2017-05-05 10:55:38 -07:00
|
|
|
|
return SendMessage("LibraryChanged", info, cancellationToken);
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-18 12:58:42 -07:00
|
|
|
|
public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
|
2013-12-23 17:00:27 -07:00
|
|
|
|
{
|
2014-05-18 12:58:42 -07:00
|
|
|
|
return SendMessage("RestartRequired", cancellationToken);
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-04-06 10:53:23 -07:00
|
|
|
|
return Task.FromResult(true);
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-18 12:58:42 -07:00
|
|
|
|
return SendMessage("ServerShuttingDown", cancellationToken);
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendServerRestartNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-05-18 12:58:42 -07:00
|
|
|
|
return SendMessage("ServerRestarting", cancellationToken);
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 14:04:22 -07:00
|
|
|
|
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
|
2014-03-28 12:58:18 -07:00
|
|
|
|
{
|
2014-05-20 17:56:24 -07:00
|
|
|
|
return SendMessage(command.Name, command.Arguments, cancellationToken);
|
2014-03-28 12:58:18 -07:00
|
|
|
|
}
|
2014-05-18 12:58:42 -07:00
|
|
|
|
|
2015-01-12 20:46:44 -07:00
|
|
|
|
public Task SendMessage<T>(string name, T data, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2017-06-20 12:38:42 -07:00
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
//var url = PostUrl + "/" + name;
|
|
|
|
|
|
|
|
|
|
//var options = new HttpRequestOptions
|
|
|
|
|
//{
|
|
|
|
|
// Url = url,
|
|
|
|
|
// CancellationToken = cancellationToken,
|
|
|
|
|
// BufferContent = false
|
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
//options.RequestContent = _json.SerializeToString(data);
|
|
|
|
|
//options.RequestContentType = "application/json";
|
|
|
|
|
|
|
|
|
|
//return _httpClient.Post(new HttpRequestOptions
|
|
|
|
|
//{
|
|
|
|
|
// Url = url,
|
|
|
|
|
// CancellationToken = cancellationToken,
|
|
|
|
|
// BufferContent = false
|
|
|
|
|
//});
|
2015-01-12 20:46:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-18 12:58:42 -07:00
|
|
|
|
private string ToQueryString(Dictionary<string, string> nvc)
|
|
|
|
|
{
|
|
|
|
|
var array = (from item in nvc
|
|
|
|
|
select string.Format("{0}={1}", WebUtility.UrlEncode(item.Key), WebUtility.UrlEncode(item.Value)))
|
|
|
|
|
.ToArray();
|
2014-05-20 17:56:24 -07:00
|
|
|
|
|
|
|
|
|
var args = string.Join("&", array);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(args))
|
|
|
|
|
{
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "?" + args;
|
2014-05-18 12:58:42 -07:00
|
|
|
|
}
|
2013-12-23 17:00:27 -07:00
|
|
|
|
}
|
|
|
|
|
}
|