jellyfin/Emby.Dlna/Main/DlnaEntryPoint.cs

364 lines
12 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
using System;
using System.Globalization;
2020-09-12 11:41:37 -04:00
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
2019-01-13 14:16:19 -05:00
using System.Threading.Tasks;
using Emby.Dlna.PlayTo;
using Emby.Dlna.Ssdp;
2020-12-07 17:06:28 -05:00
using Jellyfin.Networking.Configuration;
2023-07-03 15:51:36 -04:00
using Jellyfin.Networking.Extensions;
2019-01-13 14:16:19 -05:00
using MediaBrowser.Common.Configuration;
2016-10-29 18:22:20 -04:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Library;
2019-01-13 14:16:19 -05:00
using MediaBrowser.Controller.MediaEncoding;
2016-10-29 18:22:20 -04:00
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Globalization;
2019-01-13 14:16:19 -05:00
using Microsoft.Extensions.Logging;
2016-10-29 18:22:20 -04:00
using Rssdp;
using Rssdp.Infrastructure;
2016-10-29 18:34:54 -04:00
namespace Emby.Dlna.Main
2016-10-29 18:22:20 -04:00
{
public sealed class DlnaEntryPoint : IServerEntryPoint, IRunBeforeStartup
2016-10-29 18:22:20 -04:00
{
private readonly IServerConfigurationManager _config;
2020-06-05 20:15:56 -04:00
private readonly ILogger<DlnaEntryPoint> _logger;
2016-10-29 18:22:20 -04:00
private readonly IServerApplicationHost _appHost;
private readonly ISessionManager _sessionManager;
private readonly IHttpClientFactory _httpClientFactory;
2016-10-29 18:22:20 -04:00
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IDlnaManager _dlnaManager;
private readonly IImageProcessor _imageProcessor;
private readonly IUserDataManager _userDataManager;
private readonly ILocalizationManager _localization;
private readonly IMediaSourceManager _mediaSourceManager;
private readonly IMediaEncoder _mediaEncoder;
private readonly IDeviceDiscovery _deviceDiscovery;
2023-10-11 13:35:51 -04:00
private readonly ISsdpCommunicationsServer _communicationsServer;
2016-12-04 16:30:38 -05:00
private readonly INetworkManager _networkManager;
2023-10-11 13:26:42 -04:00
private readonly object _syncLock = new();
2020-12-07 17:06:28 -05:00
private readonly bool _disabled;
2016-11-04 04:31:05 -04:00
private PlayToManager _manager;
private SsdpDevicePublisher _publisher;
2016-11-14 14:48:01 -05:00
2020-08-20 11:01:04 -04:00
private bool _disposed;
2018-09-12 13:26:21 -04:00
public DlnaEntryPoint(
IServerConfigurationManager config,
ILoggerFactory loggerFactory,
2016-10-29 18:22:20 -04:00
IServerApplicationHost appHost,
ISessionManager sessionManager,
IHttpClientFactory httpClientFactory,
2016-10-29 18:22:20 -04:00
ILibraryManager libraryManager,
IUserManager userManager,
IDlnaManager dlnaManager,
IImageProcessor imageProcessor,
IUserDataManager userDataManager,
2018-09-12 13:26:21 -04:00
ILocalizationManager localizationManager,
2016-10-29 18:22:20 -04:00
IMediaSourceManager mediaSourceManager,
2019-01-07 18:27:46 -05:00
IDeviceDiscovery deviceDiscovery,
IMediaEncoder mediaEncoder,
2023-10-11 13:35:51 -04:00
ISsdpCommunicationsServer communicationsServer,
2023-10-11 11:05:14 -04:00
INetworkManager networkManager)
2016-10-29 18:22:20 -04:00
{
_config = config;
_appHost = appHost;
_sessionManager = sessionManager;
_httpClientFactory = httpClientFactory;
2016-10-29 18:22:20 -04:00
_libraryManager = libraryManager;
_userManager = userManager;
_dlnaManager = dlnaManager;
_imageProcessor = imageProcessor;
_userDataManager = userDataManager;
2018-09-12 13:26:21 -04:00
_localization = localizationManager;
2016-10-29 18:22:20 -04:00
_mediaSourceManager = mediaSourceManager;
_deviceDiscovery = deviceDiscovery;
_mediaEncoder = mediaEncoder;
2023-10-11 13:35:51 -04:00
_communicationsServer = communicationsServer;
2016-12-04 16:30:38 -05:00
_networkManager = networkManager;
2020-06-05 20:15:56 -04:00
_logger = loggerFactory.CreateLogger<DlnaEntryPoint>();
2018-09-12 13:26:21 -04:00
var netConfig = config.GetConfiguration<NetworkConfiguration>(NetworkConfigurationStore.StoreKey);
2021-11-08 04:58:04 -05:00
_disabled = appHost.ListenWithHttps && netConfig.RequireHttps;
2021-03-10 14:32:13 -05:00
if (_disabled && _config.GetDlnaConfiguration().EnableServer)
2020-12-07 17:06:28 -05:00
{
_logger.LogError("The DLNA specification does not support HTTPS.");
}
2016-10-29 18:22:20 -04:00
}
2020-08-20 15:04:57 -04:00
2019-01-27 09:40:37 -05:00
public async Task RunAsync()
2016-10-29 18:22:20 -04:00
{
2019-01-27 09:40:37 -05:00
await ((DlnaManager)_dlnaManager).InitProfilesAsync().ConfigureAwait(false);
2016-10-29 18:22:20 -04:00
2020-12-07 17:06:28 -05:00
if (_disabled)
{
// No use starting as dlna won't work, as we're running purely on HTTPS.
return;
}
2020-09-12 11:41:37 -04:00
ReloadComponents();
2016-10-29 18:22:20 -04:00
2020-06-09 17:05:22 -04:00
_config.NamedConfigurationUpdated += OnNamedConfigurationUpdated;
2016-10-29 18:22:20 -04:00
}
2020-09-12 11:41:37 -04:00
private void OnNamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e)
2016-10-29 18:22:20 -04:00
{
if (string.Equals(e.Key, "dlna", StringComparison.OrdinalIgnoreCase))
{
2020-09-12 11:41:37 -04:00
ReloadComponents();
2016-10-29 18:22:20 -04:00
}
}
2020-09-12 11:41:37 -04:00
private void ReloadComponents()
2016-10-29 18:22:20 -04:00
{
var options = _config.GetDlnaConfiguration();
2023-10-11 13:35:51 -04:00
StartDeviceDiscovery();
2016-10-29 18:22:20 -04:00
2018-09-12 13:26:21 -04:00
if (options.EnableServer)
2016-10-29 18:22:20 -04:00
{
2020-09-12 11:41:37 -04:00
StartDevicePublisher(options);
2016-10-29 18:22:20 -04:00
}
2018-09-12 13:26:21 -04:00
else
2016-10-29 18:22:20 -04:00
{
2018-09-12 13:26:21 -04:00
DisposeDevicePublisher();
2016-10-29 18:22:20 -04:00
}
2018-09-12 13:26:21 -04:00
if (options.EnablePlayTo)
2016-10-29 18:22:20 -04:00
{
StartPlayToManager();
}
2018-09-12 13:26:21 -04:00
else
2016-10-29 18:22:20 -04:00
{
DisposePlayToManager();
}
}
2023-10-11 13:35:51 -04:00
private void StartDeviceDiscovery()
2016-10-29 18:22:20 -04:00
{
try
{
2023-10-11 13:35:51 -04:00
((DeviceDiscovery)_deviceDiscovery).Start(_communicationsServer);
2016-10-29 18:22:20 -04:00
}
catch (Exception ex)
{
2018-12-20 07:11:26 -05:00
_logger.LogError(ex, "Error starting device discovery");
2016-10-29 18:22:20 -04:00
}
}
2020-09-12 11:41:37 -04:00
public void StartDevicePublisher(Configuration.DlnaOptions options)
2016-10-29 18:22:20 -04:00
{
2022-12-05 09:01:13 -05:00
if (_publisher is not null)
2016-10-29 18:22:20 -04:00
{
2018-09-12 13:26:21 -04:00
return;
2016-10-29 18:22:20 -04:00
}
try
{
2021-07-12 14:20:50 -04:00
_publisher = new SsdpDevicePublisher(
_communicationsServer,
Environment.OSVersion.Platform.ToString(),
// Can not use VersionString here since that includes OS and version
Environment.OSVersion.Version.ToString(),
2021-07-12 14:20:50 -04:00
_config.GetDlnaConfiguration().SendOnlyMatchedHost)
2020-06-09 17:05:22 -04:00
{
2021-11-09 16:29:33 -05:00
LogFunction = (msg) => _logger.LogDebug("{Msg}", msg),
2020-06-09 17:05:22 -04:00
SupportPnpRootDevice = false
};
2018-09-12 13:26:21 -04:00
2020-09-12 11:41:37 -04:00
RegisterServerEndpoints();
2016-10-29 18:22:20 -04:00
if (options.BlastAliveMessages)
{
_publisher.StartSendingAliveNotifications(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
}
2016-10-29 18:22:20 -04:00
}
catch (Exception ex)
{
2018-12-20 07:11:26 -05:00
_logger.LogError(ex, "Error registering endpoint");
2016-10-29 18:22:20 -04:00
}
}
2020-09-12 11:41:37 -04:00
private void RegisterServerEndpoints()
2016-10-29 18:22:20 -04:00
{
2016-11-04 19:57:21 -04:00
var udn = CreateUuid(_appHost.SystemId);
2020-11-06 10:15:30 -05:00
var descriptorUri = "/dlna/" + udn + "/description.xml";
2016-11-04 19:57:21 -04:00
// Only get bind addresses in LAN
// IPv6 is currently unsupported
var validInterfaces = _networkManager.GetInternalBindAddresses()
.Where(x => x.Address is not null)
.Where(x => x.AddressFamily != AddressFamily.InterNetworkV6)
.ToList();
2020-09-24 10:43:06 -04:00
if (validInterfaces.Count == 0)
2016-10-29 18:22:20 -04:00
{
// No interfaces returned, fall back to loopback
validInterfaces = _networkManager.GetLoopbacks().ToList();
2020-09-12 11:41:37 -04:00
}
foreach (var intf in validInterfaces)
2020-09-12 11:41:37 -04:00
{
2016-10-29 18:22:20 -04:00
var fullService = "urn:schemas-upnp-org:device:MediaServer:1";
_logger.LogInformation("Registering publisher for {ResourceName} on {DeviceAddress}", fullService, intf.Address);
2016-10-29 18:22:20 -04:00
var uri = new UriBuilder(_appHost.GetApiUrlForLocalAccess(intf.Address, false) + descriptorUri);
2016-10-29 18:22:20 -04:00
var device = new SsdpRootDevice
{
CacheLifetime = TimeSpan.FromSeconds(1800), // How long SSDP clients can cache this info.
2020-12-07 17:06:28 -05:00
Location = uri.Uri, // Must point to the URL that serves your devices UPnP description document.
Address = intf.Address,
PrefixLength = NetworkExtensions.MaskToCidr(intf.Subnet.Prefix),
2018-12-13 04:18:29 -05:00
FriendlyName = "Jellyfin",
Manufacturer = "Jellyfin",
2018-12-13 14:10:22 -05:00
ModelName = "Jellyfin Server",
2016-10-29 18:22:20 -04:00
Uuid = udn
2019-01-07 18:27:46 -05:00
// This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc.
2016-10-29 18:22:20 -04:00
};
2023-10-11 13:26:42 -04:00
SetProperties(device, fullService);
_publisher.AddDevice(device);
2016-10-29 18:22:20 -04:00
2019-01-13 14:16:19 -05:00
var embeddedDevices = new[]
2016-10-29 18:22:20 -04:00
{
"urn:schemas-upnp-org:service:ContentDirectory:1",
"urn:schemas-upnp-org:service:ConnectionManager:1",
// "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1"
2016-10-29 18:22:20 -04:00
};
foreach (var subDevice in embeddedDevices)
{
var embeddedDevice = new SsdpEmbeddedDevice
{
FriendlyName = device.FriendlyName,
Manufacturer = device.Manufacturer,
ModelName = device.ModelName,
Uuid = udn
2019-01-07 18:27:46 -05:00
// This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc.
2016-10-29 18:22:20 -04:00
};
2023-10-11 13:26:42 -04:00
SetProperties(embeddedDevice, subDevice);
2016-10-29 18:22:20 -04:00
device.AddDevice(embeddedDevice);
}
}
}
2023-10-11 13:26:42 -04:00
private static string CreateUuid(string text)
2016-10-29 18:22:20 -04:00
{
if (!Guid.TryParse(text, out var guid))
2016-11-04 19:57:21 -04:00
{
guid = text.GetMD5();
}
return guid.ToString("D", CultureInfo.InvariantCulture);
2016-10-29 18:22:20 -04:00
}
2023-10-11 13:26:42 -04:00
private static void SetProperties(SsdpDevice device, string fullDeviceType)
2016-10-29 18:22:20 -04:00
{
2023-10-11 13:26:42 -04:00
var serviceParts = fullDeviceType
.Replace("urn:", string.Empty, StringComparison.OrdinalIgnoreCase)
.Replace(":1", string.Empty, StringComparison.OrdinalIgnoreCase)
.Split(':');
2016-10-29 18:22:20 -04:00
2023-10-11 13:26:42 -04:00
device.DeviceTypeNamespace = serviceParts[0].Replace('.', '-');
2016-10-29 18:22:20 -04:00
device.DeviceClass = serviceParts[1];
device.DeviceType = serviceParts[2];
}
private void StartPlayToManager()
{
lock (_syncLock)
{
2022-12-05 09:01:13 -05:00
if (_manager is not null)
2018-09-12 13:26:21 -04:00
{
return;
}
2016-10-29 18:22:20 -04:00
try
{
_manager = new PlayToManager(
_logger,
2016-10-29 18:22:20 -04:00
_sessionManager,
_libraryManager,
_userManager,
_dlnaManager,
_appHost,
_imageProcessor,
_deviceDiscovery,
_httpClientFactory,
2016-10-29 18:22:20 -04:00
_userDataManager,
_localization,
_mediaSourceManager,
2019-02-05 03:49:46 -05:00
_mediaEncoder);
2016-10-29 18:22:20 -04:00
_manager.Start();
}
catch (Exception ex)
{
2018-12-20 07:11:26 -05:00
_logger.LogError(ex, "Error starting PlayTo manager");
2016-10-29 18:22:20 -04:00
}
}
}
private void DisposePlayToManager()
{
lock (_syncLock)
{
2022-12-05 09:01:13 -05:00
if (_manager is not null)
2016-10-29 18:22:20 -04:00
{
try
{
_logger.LogInformation("Disposing PlayToManager");
2016-10-29 18:22:20 -04:00
_manager.Dispose();
}
catch (Exception ex)
{
2018-12-20 07:11:26 -05:00
_logger.LogError(ex, "Error disposing PlayTo manager");
2016-10-29 18:22:20 -04:00
}
2016-10-29 18:22:20 -04:00
_manager = null;
}
}
}
2020-08-20 11:01:04 -04:00
public void DisposeDevicePublisher()
{
2022-12-05 09:01:13 -05:00
if (_publisher is not null)
2020-08-20 11:01:04 -04:00
{
_logger.LogInformation("Disposing SsdpDevicePublisher");
_publisher.Dispose();
_publisher = null;
}
}
/// <inheritdoc />
2016-10-29 18:22:20 -04:00
public void Dispose()
2020-08-20 11:01:04 -04:00
{
if (_disposed)
{
return;
}
2020-08-20 15:04:57 -04:00
DisposeDevicePublisher();
DisposePlayToManager();
2020-08-20 11:01:04 -04:00
_disposed = true;
2016-10-29 18:22:20 -04:00
}
}
}