DlnaEntryPoint.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Net.Sockets;
  8. using System.Threading.Tasks;
  9. using Emby.Dlna.PlayTo;
  10. using Emby.Dlna.Ssdp;
  11. using Jellyfin.Networking.Configuration;
  12. using Jellyfin.Networking.Manager;
  13. using MediaBrowser.Common.Configuration;
  14. using MediaBrowser.Common.Extensions;
  15. using MediaBrowser.Common.Net;
  16. using MediaBrowser.Controller;
  17. using MediaBrowser.Controller.Configuration;
  18. using MediaBrowser.Controller.Dlna;
  19. using MediaBrowser.Controller.Drawing;
  20. using MediaBrowser.Controller.Library;
  21. using MediaBrowser.Controller.MediaEncoding;
  22. using MediaBrowser.Controller.Plugins;
  23. using MediaBrowser.Controller.Session;
  24. using MediaBrowser.Controller.TV;
  25. using MediaBrowser.Model.Dlna;
  26. using MediaBrowser.Model.Globalization;
  27. using MediaBrowser.Model.Net;
  28. using Microsoft.Extensions.Logging;
  29. using Rssdp;
  30. using Rssdp.Infrastructure;
  31. namespace Emby.Dlna.Main
  32. {
  33. public sealed class DlnaEntryPoint : IServerEntryPoint, IRunBeforeStartup
  34. {
  35. private readonly IServerConfigurationManager _config;
  36. private readonly ILogger<DlnaEntryPoint> _logger;
  37. private readonly IServerApplicationHost _appHost;
  38. private readonly ISessionManager _sessionManager;
  39. private readonly IHttpClientFactory _httpClientFactory;
  40. private readonly ILibraryManager _libraryManager;
  41. private readonly IUserManager _userManager;
  42. private readonly IDlnaManager _dlnaManager;
  43. private readonly IImageProcessor _imageProcessor;
  44. private readonly IUserDataManager _userDataManager;
  45. private readonly ILocalizationManager _localization;
  46. private readonly IMediaSourceManager _mediaSourceManager;
  47. private readonly IMediaEncoder _mediaEncoder;
  48. private readonly IDeviceDiscovery _deviceDiscovery;
  49. private readonly ISocketFactory _socketFactory;
  50. private readonly INetworkManager _networkManager;
  51. private readonly object _syncLock = new object();
  52. private readonly bool _disabled;
  53. private PlayToManager _manager;
  54. private SsdpDevicePublisher _publisher;
  55. private ISsdpCommunicationsServer _communicationsServer;
  56. private bool _disposed;
  57. public DlnaEntryPoint(
  58. IServerConfigurationManager config,
  59. ILoggerFactory loggerFactory,
  60. IServerApplicationHost appHost,
  61. ISessionManager sessionManager,
  62. IHttpClientFactory httpClientFactory,
  63. ILibraryManager libraryManager,
  64. IUserManager userManager,
  65. IDlnaManager dlnaManager,
  66. IImageProcessor imageProcessor,
  67. IUserDataManager userDataManager,
  68. ILocalizationManager localizationManager,
  69. IMediaSourceManager mediaSourceManager,
  70. IDeviceDiscovery deviceDiscovery,
  71. IMediaEncoder mediaEncoder,
  72. ISocketFactory socketFactory,
  73. INetworkManager networkManager,
  74. IUserViewManager userViewManager,
  75. ITVSeriesManager tvSeriesManager)
  76. {
  77. _config = config;
  78. _appHost = appHost;
  79. _sessionManager = sessionManager;
  80. _httpClientFactory = httpClientFactory;
  81. _libraryManager = libraryManager;
  82. _userManager = userManager;
  83. _dlnaManager = dlnaManager;
  84. _imageProcessor = imageProcessor;
  85. _userDataManager = userDataManager;
  86. _localization = localizationManager;
  87. _mediaSourceManager = mediaSourceManager;
  88. _deviceDiscovery = deviceDiscovery;
  89. _mediaEncoder = mediaEncoder;
  90. _socketFactory = socketFactory;
  91. _networkManager = networkManager;
  92. _logger = loggerFactory.CreateLogger<DlnaEntryPoint>();
  93. ContentDirectory = new ContentDirectory.ContentDirectoryService(
  94. dlnaManager,
  95. userDataManager,
  96. imageProcessor,
  97. libraryManager,
  98. config,
  99. userManager,
  100. loggerFactory.CreateLogger<ContentDirectory.ContentDirectoryService>(),
  101. httpClientFactory,
  102. localizationManager,
  103. mediaSourceManager,
  104. userViewManager,
  105. mediaEncoder,
  106. tvSeriesManager);
  107. ConnectionManager = new ConnectionManager.ConnectionManagerService(
  108. dlnaManager,
  109. config,
  110. loggerFactory.CreateLogger<ConnectionManager.ConnectionManagerService>(),
  111. httpClientFactory);
  112. MediaReceiverRegistrar = new MediaReceiverRegistrar.MediaReceiverRegistrarService(
  113. loggerFactory.CreateLogger<MediaReceiverRegistrar.MediaReceiverRegistrarService>(),
  114. httpClientFactory,
  115. config);
  116. Current = this;
  117. var netConfig = config.GetConfiguration<NetworkConfiguration>(NetworkConfigurationStore.StoreKey);
  118. _disabled = appHost.ListenWithHttps && netConfig.RequireHttps;
  119. if (_disabled && _config.GetDlnaConfiguration().EnableServer)
  120. {
  121. _logger.LogError("The DLNA specification does not support HTTPS.");
  122. }
  123. }
  124. public static DlnaEntryPoint Current { get; private set; }
  125. /// <summary>
  126. /// Gets a value indicating whether the dlna server is enabled.
  127. /// </summary>
  128. public static bool Enabled { get; private set; }
  129. public IContentDirectory ContentDirectory { get; private set; }
  130. public IConnectionManager ConnectionManager { get; private set; }
  131. public IMediaReceiverRegistrar MediaReceiverRegistrar { get; private set; }
  132. public async Task RunAsync()
  133. {
  134. await ((DlnaManager)_dlnaManager).InitProfilesAsync().ConfigureAwait(false);
  135. if (_disabled)
  136. {
  137. // No use starting as dlna won't work, as we're running purely on HTTPS.
  138. return;
  139. }
  140. ReloadComponents();
  141. _config.NamedConfigurationUpdated += OnNamedConfigurationUpdated;
  142. }
  143. private void OnNamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e)
  144. {
  145. if (string.Equals(e.Key, "dlna", StringComparison.OrdinalIgnoreCase))
  146. {
  147. ReloadComponents();
  148. }
  149. }
  150. private void ReloadComponents()
  151. {
  152. var options = _config.GetDlnaConfiguration();
  153. Enabled = options.EnableServer;
  154. StartSsdpHandler();
  155. if (options.EnableServer)
  156. {
  157. StartDevicePublisher(options);
  158. }
  159. else
  160. {
  161. DisposeDevicePublisher();
  162. }
  163. if (options.EnablePlayTo)
  164. {
  165. StartPlayToManager();
  166. }
  167. else
  168. {
  169. DisposePlayToManager();
  170. }
  171. }
  172. private void StartSsdpHandler()
  173. {
  174. try
  175. {
  176. if (_communicationsServer is null)
  177. {
  178. var enableMultiSocketBinding = OperatingSystem.IsWindows() ||
  179. OperatingSystem.IsLinux();
  180. _communicationsServer = new SsdpCommunicationsServer(_socketFactory, _networkManager, _logger, enableMultiSocketBinding)
  181. {
  182. IsShared = true
  183. };
  184. StartDeviceDiscovery(_communicationsServer);
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. _logger.LogError(ex, "Error starting ssdp handlers");
  190. }
  191. }
  192. private void StartDeviceDiscovery(ISsdpCommunicationsServer communicationsServer)
  193. {
  194. try
  195. {
  196. if (communicationsServer is not null)
  197. {
  198. ((DeviceDiscovery)_deviceDiscovery).Start(communicationsServer);
  199. }
  200. }
  201. catch (Exception ex)
  202. {
  203. _logger.LogError(ex, "Error starting device discovery");
  204. }
  205. }
  206. private void DisposeDeviceDiscovery()
  207. {
  208. try
  209. {
  210. _logger.LogInformation("Disposing DeviceDiscovery");
  211. ((DeviceDiscovery)_deviceDiscovery).Dispose();
  212. }
  213. catch (Exception ex)
  214. {
  215. _logger.LogError(ex, "Error stopping device discovery");
  216. }
  217. }
  218. public void StartDevicePublisher(Configuration.DlnaOptions options)
  219. {
  220. if (!options.BlastAliveMessages)
  221. {
  222. return;
  223. }
  224. if (_publisher is not null)
  225. {
  226. return;
  227. }
  228. try
  229. {
  230. _publisher = new SsdpDevicePublisher(
  231. _communicationsServer,
  232. MediaBrowser.Common.System.OperatingSystem.Name,
  233. Environment.OSVersion.VersionString,
  234. _config.GetDlnaConfiguration().SendOnlyMatchedHost)
  235. {
  236. LogFunction = (msg) => _logger.LogDebug("{Msg}", msg),
  237. SupportPnpRootDevice = false
  238. };
  239. RegisterServerEndpoints();
  240. _publisher.StartBroadcastingAliveMessages(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
  241. }
  242. catch (Exception ex)
  243. {
  244. _logger.LogError(ex, "Error registering endpoint");
  245. }
  246. }
  247. private void RegisterServerEndpoints()
  248. {
  249. var udn = CreateUuid(_appHost.SystemId);
  250. var descriptorUri = "/dlna/" + udn + "/description.xml";
  251. var bindAddresses = NetworkManager.CreateCollection(
  252. _networkManager.GetInternalBindAddresses()
  253. .Where(i => i.AddressFamily == AddressFamily.InterNetwork || (i.AddressFamily == AddressFamily.InterNetworkV6 && i.Address.ScopeId != 0)));
  254. if (bindAddresses.Count == 0)
  255. {
  256. // No interfaces returned, so use loopback.
  257. bindAddresses = _networkManager.GetLoopbacks();
  258. }
  259. foreach (IPNetAddress address in bindAddresses)
  260. {
  261. if (address.AddressFamily == AddressFamily.InterNetworkV6)
  262. {
  263. // Not supporting IPv6 right now
  264. continue;
  265. }
  266. // Limit to LAN addresses only
  267. if (!_networkManager.IsInLocalNetwork(address))
  268. {
  269. continue;
  270. }
  271. var fullService = "urn:schemas-upnp-org:device:MediaServer:1";
  272. _logger.LogInformation("Registering publisher for {ResourceName} on {DeviceAddress}", fullService, address);
  273. var uri = new UriBuilder(_appHost.GetApiUrlForLocalAccess(address, false) + descriptorUri);
  274. var device = new SsdpRootDevice
  275. {
  276. CacheLifetime = TimeSpan.FromSeconds(1800), // How long SSDP clients can cache this info.
  277. Location = uri.Uri, // Must point to the URL that serves your devices UPnP description document.
  278. Address = address.Address,
  279. PrefixLength = address.PrefixLength,
  280. FriendlyName = "Jellyfin",
  281. Manufacturer = "Jellyfin",
  282. ModelName = "Jellyfin Server",
  283. Uuid = udn
  284. // This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc.
  285. };
  286. SetProperies(device, fullService);
  287. _publisher.AddDevice(device);
  288. var embeddedDevices = new[]
  289. {
  290. "urn:schemas-upnp-org:service:ContentDirectory:1",
  291. "urn:schemas-upnp-org:service:ConnectionManager:1",
  292. // "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1"
  293. };
  294. foreach (var subDevice in embeddedDevices)
  295. {
  296. var embeddedDevice = new SsdpEmbeddedDevice
  297. {
  298. FriendlyName = device.FriendlyName,
  299. Manufacturer = device.Manufacturer,
  300. ModelName = device.ModelName,
  301. Uuid = udn
  302. // This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc.
  303. };
  304. SetProperies(embeddedDevice, subDevice);
  305. device.AddDevice(embeddedDevice);
  306. }
  307. }
  308. }
  309. private string CreateUuid(string text)
  310. {
  311. if (!Guid.TryParse(text, out var guid))
  312. {
  313. guid = text.GetMD5();
  314. }
  315. return guid.ToString("D", CultureInfo.InvariantCulture);
  316. }
  317. private void SetProperies(SsdpDevice device, string fullDeviceType)
  318. {
  319. var service = fullDeviceType.Replace("urn:", string.Empty, StringComparison.OrdinalIgnoreCase).Replace(":1", string.Empty, StringComparison.OrdinalIgnoreCase);
  320. var serviceParts = service.Split(':');
  321. var deviceTypeNamespace = serviceParts[0].Replace('.', '-');
  322. device.DeviceTypeNamespace = deviceTypeNamespace;
  323. device.DeviceClass = serviceParts[1];
  324. device.DeviceType = serviceParts[2];
  325. }
  326. private void StartPlayToManager()
  327. {
  328. lock (_syncLock)
  329. {
  330. if (_manager is not null)
  331. {
  332. return;
  333. }
  334. try
  335. {
  336. _manager = new PlayToManager(
  337. _logger,
  338. _sessionManager,
  339. _libraryManager,
  340. _userManager,
  341. _dlnaManager,
  342. _appHost,
  343. _imageProcessor,
  344. _deviceDiscovery,
  345. _httpClientFactory,
  346. _userDataManager,
  347. _localization,
  348. _mediaSourceManager,
  349. _mediaEncoder);
  350. _manager.Start();
  351. }
  352. catch (Exception ex)
  353. {
  354. _logger.LogError(ex, "Error starting PlayTo manager");
  355. }
  356. }
  357. }
  358. private void DisposePlayToManager()
  359. {
  360. lock (_syncLock)
  361. {
  362. if (_manager is not null)
  363. {
  364. try
  365. {
  366. _logger.LogInformation("Disposing PlayToManager");
  367. _manager.Dispose();
  368. }
  369. catch (Exception ex)
  370. {
  371. _logger.LogError(ex, "Error disposing PlayTo manager");
  372. }
  373. _manager = null;
  374. }
  375. }
  376. }
  377. public void DisposeDevicePublisher()
  378. {
  379. if (_publisher is not null)
  380. {
  381. _logger.LogInformation("Disposing SsdpDevicePublisher");
  382. _publisher.Dispose();
  383. _publisher = null;
  384. }
  385. }
  386. /// <inheritdoc />
  387. public void Dispose()
  388. {
  389. if (_disposed)
  390. {
  391. return;
  392. }
  393. DisposeDevicePublisher();
  394. DisposePlayToManager();
  395. DisposeDeviceDiscovery();
  396. if (_communicationsServer is not null)
  397. {
  398. _logger.LogInformation("Disposing SsdpCommunicationsServer");
  399. _communicationsServer.Dispose();
  400. _communicationsServer = null;
  401. }
  402. ContentDirectory = null;
  403. ConnectionManager = null;
  404. MediaReceiverRegistrar = null;
  405. Current = null;
  406. _disposed = true;
  407. }
  408. }
  409. }