jellyfin/Emby.Dlna/PlayTo/PlayToManager.cs

261 lines
9 KiB
C#
Raw Normal View History

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