2020-01-22 15:00:07 -05:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2019-02-28 17:22:57 -05:00
|
|
|
using System.Globalization;
|
2019-01-13 14:16:19 -05:00
|
|
|
using System.Linq;
|
2019-07-07 15:03:26 -04:00
|
|
|
using System.Net;
|
2020-08-31 13:26:42 -04:00
|
|
|
using System.Net.Http;
|
2019-01-13 14:16:19 -05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-13 20:48:28 -04:00
|
|
|
using Jellyfin.Data.Events;
|
2019-01-13 14:16:19 -05:00
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Common.Net;
|
2016-10-29 18:22:20 -04:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2019-01-13 14:16:19 -05:00
|
|
|
using MediaBrowser.Controller.Session;
|
2016-10-29 18:22:20 -04:00
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
using MediaBrowser.Model.Globalization;
|
2019-01-13 14:16:19 -05:00
|
|
|
using MediaBrowser.Model.Session;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-10-29 18:22:20 -04:00
|
|
|
|
2016-10-29 18:34:54 -04:00
|
|
|
namespace Emby.Dlna.PlayTo
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2020-04-02 10:49:58 -04:00
|
|
|
public sealed class PlayToManager : IDisposable
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
private readonly IImageProcessor _imageProcessor;
|
2020-08-31 13:26:42 -04:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2016-10-29 18:22:20 -04:00
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
|
|
|
private readonly ILocalizationManager _localization;
|
|
|
|
|
|
|
|
private readonly IDeviceDiscovery _deviceDiscovery;
|
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
|
|
|
|
2017-01-24 14:54:18 -05:00
|
|
|
private bool _disposed;
|
2017-11-23 10:46:16 -05:00
|
|
|
private SemaphoreSlim _sessionLock = new SemaphoreSlim(1, 1);
|
|
|
|
private CancellationTokenSource _disposeCancellationTokenSource = new CancellationTokenSource();
|
2016-10-29 18:22:20 -04:00
|
|
|
|
2020-08-31 13:26:42 -04:00
|
|
|
public PlayToManager(ILogger logger, ISessionManager sessionManager, ILibraryManager libraryManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost, IImageProcessor imageProcessor, IDeviceDiscovery deviceDiscovery, IHttpClientFactory httpClientFactory, IServerConfigurationManager config, IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_userManager = userManager;
|
|
|
|
_dlnaManager = dlnaManager;
|
|
|
|
_appHost = appHost;
|
|
|
|
_imageProcessor = imageProcessor;
|
|
|
|
_deviceDiscovery = deviceDiscovery;
|
2020-08-31 13:26:42 -04:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2016-10-29 18:22:20 -04:00
|
|
|
_config = config;
|
|
|
|
_userDataManager = userDataManager;
|
|
|
|
_localization = localization;
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
|
|
|
_mediaEncoder = mediaEncoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
{
|
2020-01-22 15:00:07 -05:00
|
|
|
_deviceDiscovery.DeviceDiscovered += OnDeviceDiscoveryDeviceDiscovered;
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
|
|
|
|
2020-01-22 15:00:07 -05:00
|
|
|
private async void OnDeviceDiscoveryDeviceDiscovered(object sender, GenericEventArgs<UpnpDeviceInfo> e)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2017-01-24 14:54:18 -05:00
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-29 18:22:20 -04:00
|
|
|
var info = e.Argument;
|
|
|
|
|
2020-06-20 04:35:29 -04:00
|
|
|
if (!info.Headers.TryGetValue("USN", out string usn))
|
|
|
|
{
|
|
|
|
usn = string.Empty;
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
2020-06-20 04:35:29 -04:00
|
|
|
if (!info.Headers.TryGetValue("NT", out string nt))
|
|
|
|
{
|
|
|
|
nt = string.Empty;
|
|
|
|
}
|
2016-10-29 18:22:20 -04:00
|
|
|
|
|
|
|
string location = info.Location.ToString();
|
|
|
|
|
|
|
|
// It has to report that it's a media renderer
|
|
|
|
if (usn.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) == -1 &&
|
2020-08-20 11:01:04 -04:00
|
|
|
nt.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) == -1)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2020-06-14 05:11:11 -04:00
|
|
|
// _logger.LogDebug("Upnp device {0} does not contain a MediaRenderer device (0).", location);
|
2016-10-29 18:22:20 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:46:16 -05:00
|
|
|
var cancellationToken = _disposeCancellationTokenSource.Token;
|
|
|
|
|
|
|
|
await _sessionLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2016-10-29 18:22:20 -04:00
|
|
|
try
|
|
|
|
{
|
2017-11-23 10:46:16 -05:00
|
|
|
if (_disposed)
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2017-11-23 10:46:16 -05:00
|
|
|
return;
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
|
|
|
|
2019-02-14 15:53:04 -05:00
|
|
|
if (_sessionManager.Sessions.Any(i => usn.IndexOf(i.DeviceId, StringComparison.OrdinalIgnoreCase) != -1))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-23 10:46:16 -05:00
|
|
|
await AddDevice(info, location, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 07:11:26 -05:00
|
|
|
_logger.LogError(ex, "Error creating PlayTo device.");
|
2017-11-23 10:46:16 -05:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_sessionLock.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 16:43:48 -04:00
|
|
|
private static string GetUuid(string usn)
|
2017-11-23 10:46:16 -05:00
|
|
|
{
|
2020-09-08 04:26:52 -04:00
|
|
|
const string uuidStr = "uuid:";
|
2020-09-08 02:53:53 -04:00
|
|
|
const string uuidColonStr = "::";
|
2020-09-08 04:26:52 -04:00
|
|
|
|
2020-09-08 02:53:53 -04:00
|
|
|
var index = usn.IndexOf(uuidStr, StringComparison.OrdinalIgnoreCase);
|
2018-09-12 13:26:21 -04:00
|
|
|
if (index != -1)
|
|
|
|
{
|
2020-09-08 02:53:53 -04:00
|
|
|
return usn.Substring(index + uuidStr.Length);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
2020-06-15 17:43:52 -04:00
|
|
|
|
2020-09-08 02:53:53 -04:00
|
|
|
index = usn.IndexOf(uuidColonStr, StringComparison.OrdinalIgnoreCase);
|
2018-09-12 13:26:21 -04:00
|
|
|
if (index != -1)
|
2017-11-23 10:46:16 -05:00
|
|
|
{
|
2020-09-08 02:53:53 -04:00
|
|
|
usn = usn.Substring(0, index + uuidColonStr.Length);
|
2017-11-23 10:46:16 -05:00
|
|
|
}
|
|
|
|
|
2019-02-28 17:22:57 -05:00
|
|
|
return usn.GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task AddDevice(UpnpDeviceInfo info, string location, CancellationToken cancellationToken)
|
|
|
|
{
|
2017-11-23 10:46:16 -05:00
|
|
|
var uri = info.Location;
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogDebug("Attempting to create PlayToController from location {0}", location);
|
2017-11-23 10:46:16 -05:00
|
|
|
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogDebug("Logging session activity from location {0}", location);
|
2019-01-17 12:47:41 -05:00
|
|
|
if (info.Headers.TryGetValue("USN", out string uuid))
|
2017-11-23 10:46:16 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
uuid = GetUuid(uuid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-28 17:22:57 -05:00
|
|
|
uuid = location.GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
2017-11-23 10:46:16 -05:00
|
|
|
}
|
|
|
|
|
2019-10-08 14:51:11 -04:00
|
|
|
var sessionInfo = _sessionManager.LogSessionActivity("DLNA", _appHost.ApplicationVersionString, uuid, null, uri.OriginalString, null);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
var controller = sessionInfo.SessionControllers.OfType<PlayToController>().FirstOrDefault();
|
2016-10-29 18:22:20 -04:00
|
|
|
|
2017-11-23 10:46:16 -05:00
|
|
|
if (controller == null)
|
|
|
|
{
|
2020-08-31 13:26:42 -04:00
|
|
|
var device = await Device.CreateuPnpDeviceAsync(uri, _httpClientFactory, _logger, cancellationToken).ConfigureAwait(false);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2019-01-25 15:52:10 -05:00
|
|
|
string deviceName = device.Properties.Name;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
_sessionManager.UpdateDeviceName(sessionInfo.Id, deviceName);
|
|
|
|
|
2017-11-23 10:46:16 -05:00
|
|
|
string serverAddress;
|
2019-07-07 15:03:26 -04:00
|
|
|
if (info.LocalIpAddress == null || info.LocalIpAddress.Equals(IPAddress.Any) || info.LocalIpAddress.Equals(IPAddress.IPv6Any))
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
serverAddress = await _appHost.GetLocalApiUrl(cancellationToken).ConfigureAwait(false);
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
2017-11-23 10:46:16 -05:00
|
|
|
else
|
2017-01-24 14:54:18 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
serverAddress = _appHost.GetLocalApiUrl(info.LocalIpAddress);
|
2017-01-24 14:54:18 -05:00
|
|
|
}
|
|
|
|
|
2020-06-05 20:15:56 -04:00
|
|
|
controller = new PlayToController(
|
|
|
|
sessionInfo,
|
2020-08-20 11:01:04 -04:00
|
|
|
_sessionManager,
|
|
|
|
_libraryManager,
|
|
|
|
_logger,
|
|
|
|
_dlnaManager,
|
|
|
|
_userManager,
|
|
|
|
_imageProcessor,
|
|
|
|
serverAddress,
|
|
|
|
null,
|
|
|
|
_deviceDiscovery,
|
|
|
|
_userDataManager,
|
|
|
|
_localization,
|
|
|
|
_mediaSourceManager,
|
|
|
|
_config,
|
|
|
|
_mediaEncoder);
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
sessionInfo.AddController(controller);
|
2017-11-23 10:46:16 -05:00
|
|
|
|
|
|
|
controller.Init(device);
|
|
|
|
|
|
|
|
var profile = _dlnaManager.GetProfile(device.Properties.ToDeviceIdentification()) ??
|
|
|
|
_dlnaManager.GetDefaultProfile();
|
|
|
|
|
|
|
|
_sessionManager.ReportCapabilities(sessionInfo.Id, new ClientCapabilities
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2017-11-23 10:46:16 -05:00
|
|
|
PlayableMediaTypes = profile.GetSupportedMediaTypes(),
|
2017-01-24 14:54:18 -05:00
|
|
|
|
2020-08-20 11:01:04 -04:00
|
|
|
SupportedCommands = new[]
|
2016-10-29 18:22:20 -04:00
|
|
|
{
|
2020-08-20 11:01:04 -04:00
|
|
|
GeneralCommandType.VolumeDown.ToString(),
|
|
|
|
GeneralCommandType.VolumeUp.ToString(),
|
|
|
|
GeneralCommandType.Mute.ToString(),
|
|
|
|
GeneralCommandType.Unmute.ToString(),
|
|
|
|
GeneralCommandType.ToggleMute.ToString(),
|
|
|
|
GeneralCommandType.SetVolume.ToString(),
|
|
|
|
GeneralCommandType.SetAudioStreamIndex.ToString(),
|
|
|
|
GeneralCommandType.SetSubtitleStreamIndex.ToString(),
|
|
|
|
GeneralCommandType.PlayMediaSource.ToString()
|
2017-11-23 10:46:16 -05:00
|
|
|
},
|
2016-10-29 18:22:20 -04:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
SupportsMediaControl = true
|
2017-11-23 10:46:16 -05:00
|
|
|
});
|
2016-10-29 18:22:20 -04:00
|
|
|
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogInformation("DLNA Session created for {0} - {1}", device.Properties.Name, device.Properties.ModelName);
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 10:49:58 -04:00
|
|
|
/// <inheritdoc />
|
2016-10-29 18:22:20 -04:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2020-01-22 15:00:07 -05:00
|
|
|
_deviceDiscovery.DeviceDiscovered -= OnDeviceDiscoveryDeviceDiscovered;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2017-11-23 10:46:16 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_disposeCancellationTokenSource.Cancel();
|
|
|
|
}
|
2020-08-20 11:01:04 -04:00
|
|
|
catch (Exception ex)
|
2017-11-23 10:46:16 -05:00
|
|
|
{
|
2020-08-20 11:59:27 -04:00
|
|
|
_logger.LogDebug(ex, "Error while disposing PlayToManager");
|
2017-11-23 10:46:16 -05:00
|
|
|
}
|
|
|
|
|
2020-04-02 10:49:58 -04:00
|
|
|
_sessionLock.Dispose();
|
|
|
|
_disposeCancellationTokenSource.Dispose();
|
|
|
|
|
2017-01-24 14:54:18 -05:00
|
|
|
_disposed = true;
|
2016-10-29 18:22:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|