DlnaEntryPoint.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Emby.Dlna.PlayTo;
  6. using Emby.Dlna.Ssdp;
  7. using MediaBrowser.Common.Configuration;
  8. using MediaBrowser.Common.Extensions;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Controller;
  11. using MediaBrowser.Controller.Configuration;
  12. using MediaBrowser.Controller.Dlna;
  13. using MediaBrowser.Controller.Drawing;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Controller.MediaEncoding;
  16. using MediaBrowser.Controller.Plugins;
  17. using MediaBrowser.Controller.Session;
  18. using MediaBrowser.Controller.TV;
  19. using MediaBrowser.Model.Dlna;
  20. using MediaBrowser.Model.Globalization;
  21. using MediaBrowser.Model.Net;
  22. using MediaBrowser.Model.System;
  23. using MediaBrowser.Model.Threading;
  24. using MediaBrowser.Model.Xml;
  25. using Microsoft.Extensions.Logging;
  26. using Rssdp;
  27. using Rssdp.Infrastructure;
  28. namespace Emby.Dlna.Main
  29. {
  30. public class DlnaEntryPoint : IServerEntryPoint, IRunBeforeStartup
  31. {
  32. private readonly IServerConfigurationManager _config;
  33. private readonly ILogger _logger;
  34. private readonly IServerApplicationHost _appHost;
  35. private PlayToManager _manager;
  36. private readonly ISessionManager _sessionManager;
  37. private readonly IHttpClient _httpClient;
  38. private readonly ILibraryManager _libraryManager;
  39. private readonly IUserManager _userManager;
  40. private readonly IDlnaManager _dlnaManager;
  41. private readonly IImageProcessor _imageProcessor;
  42. private readonly IUserDataManager _userDataManager;
  43. private readonly ILocalizationManager _localization;
  44. private readonly IMediaSourceManager _mediaSourceManager;
  45. private readonly IMediaEncoder _mediaEncoder;
  46. private readonly IDeviceDiscovery _deviceDiscovery;
  47. private SsdpDevicePublisher _Publisher;
  48. private readonly ITimerFactory _timerFactory;
  49. private readonly ISocketFactory _socketFactory;
  50. private readonly IEnvironmentInfo _environmentInfo;
  51. private readonly INetworkManager _networkManager;
  52. private ISsdpCommunicationsServer _communicationsServer;
  53. internal IContentDirectory ContentDirectory { get; private set; }
  54. internal IConnectionManager ConnectionManager { get; private set; }
  55. internal IMediaReceiverRegistrar MediaReceiverRegistrar { get; private set; }
  56. public static DlnaEntryPoint Current;
  57. public DlnaEntryPoint(IServerConfigurationManager config,
  58. ILoggerFactory loggerFactory,
  59. IServerApplicationHost appHost,
  60. ISessionManager sessionManager,
  61. IHttpClient httpClient,
  62. ILibraryManager libraryManager,
  63. IUserManager userManager,
  64. IDlnaManager dlnaManager,
  65. IImageProcessor imageProcessor,
  66. IUserDataManager userDataManager,
  67. ILocalizationManager localizationManager,
  68. IMediaSourceManager mediaSourceManager,
  69. IDeviceDiscovery deviceDiscovery,
  70. IMediaEncoder mediaEncoder,
  71. ISocketFactory socketFactory,
  72. ITimerFactory timerFactory,
  73. IEnvironmentInfo environmentInfo,
  74. INetworkManager networkManager,
  75. IUserViewManager userViewManager,
  76. IXmlReaderSettingsFactory xmlReaderSettingsFactory,
  77. ITVSeriesManager tvSeriesManager)
  78. {
  79. _config = config;
  80. _appHost = appHost;
  81. _sessionManager = sessionManager;
  82. _httpClient = httpClient;
  83. _libraryManager = libraryManager;
  84. _userManager = userManager;
  85. _dlnaManager = dlnaManager;
  86. _imageProcessor = imageProcessor;
  87. _userDataManager = userDataManager;
  88. _localization = localizationManager;
  89. _mediaSourceManager = mediaSourceManager;
  90. _deviceDiscovery = deviceDiscovery;
  91. _mediaEncoder = mediaEncoder;
  92. _socketFactory = socketFactory;
  93. _timerFactory = timerFactory;
  94. _environmentInfo = environmentInfo;
  95. _networkManager = networkManager;
  96. _logger = loggerFactory.CreateLogger("Dlna");
  97. ContentDirectory = new ContentDirectory.ContentDirectory(dlnaManager,
  98. userDataManager,
  99. imageProcessor,
  100. libraryManager,
  101. config,
  102. userManager,
  103. _logger,
  104. httpClient,
  105. localizationManager,
  106. mediaSourceManager,
  107. userViewManager,
  108. mediaEncoder,
  109. xmlReaderSettingsFactory,
  110. tvSeriesManager);
  111. ConnectionManager = new ConnectionManager.ConnectionManager(dlnaManager, config, _logger, httpClient, xmlReaderSettingsFactory);
  112. MediaReceiverRegistrar = new MediaReceiverRegistrar.MediaReceiverRegistrar(_logger, httpClient, config, xmlReaderSettingsFactory);
  113. Current = this;
  114. }
  115. public void Run()
  116. {
  117. ((DlnaManager)_dlnaManager).InitProfiles();
  118. ReloadComponents();
  119. _config.NamedConfigurationUpdated += _config_NamedConfigurationUpdated;
  120. }
  121. void _config_NamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e)
  122. {
  123. if (string.Equals(e.Key, "dlna", StringComparison.OrdinalIgnoreCase))
  124. {
  125. ReloadComponents();
  126. }
  127. }
  128. private async void ReloadComponents()
  129. {
  130. var options = _config.GetDlnaConfiguration();
  131. StartSsdpHandler();
  132. if (options.EnableServer)
  133. {
  134. await StartDevicePublisher(options).ConfigureAwait(false);
  135. }
  136. else
  137. {
  138. DisposeDevicePublisher();
  139. }
  140. if (options.EnablePlayTo)
  141. {
  142. StartPlayToManager();
  143. }
  144. else
  145. {
  146. DisposePlayToManager();
  147. }
  148. }
  149. private void StartSsdpHandler()
  150. {
  151. try
  152. {
  153. if (_communicationsServer == null)
  154. {
  155. var enableMultiSocketBinding = _environmentInfo.OperatingSystem == MediaBrowser.Model.System.OperatingSystem.Windows;
  156. _communicationsServer = new SsdpCommunicationsServer(_socketFactory, _networkManager, _logger, enableMultiSocketBinding)
  157. {
  158. IsShared = true
  159. };
  160. StartDeviceDiscovery(_communicationsServer);
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. _logger.LogError(ex, "Error starting ssdp handlers");
  166. }
  167. }
  168. private void LogMessage(string msg)
  169. {
  170. _logger.LogDebug(msg);
  171. }
  172. private void StartDeviceDiscovery(ISsdpCommunicationsServer communicationsServer)
  173. {
  174. try
  175. {
  176. ((DeviceDiscovery)_deviceDiscovery).Start(communicationsServer);
  177. }
  178. catch (Exception ex)
  179. {
  180. _logger.LogError(ex, "Error starting device discovery");
  181. }
  182. }
  183. private void DisposeDeviceDiscovery()
  184. {
  185. try
  186. {
  187. _logger.LogInformation("Disposing DeviceDiscovery");
  188. ((DeviceDiscovery)_deviceDiscovery).Dispose();
  189. }
  190. catch (Exception ex)
  191. {
  192. _logger.LogError(ex, "Error stopping device discovery");
  193. }
  194. }
  195. public async Task StartDevicePublisher(Configuration.DlnaOptions options)
  196. {
  197. if (!options.BlastAliveMessages)
  198. {
  199. return;
  200. }
  201. if (_Publisher != null)
  202. {
  203. return;
  204. }
  205. try
  206. {
  207. _Publisher = new SsdpDevicePublisher(_communicationsServer, _timerFactory, _environmentInfo.OperatingSystemName, _environmentInfo.OperatingSystemVersion);
  208. _Publisher.LogFunction = LogMessage;
  209. _Publisher.SupportPnpRootDevice = false;
  210. await RegisterServerEndpoints().ConfigureAwait(false);
  211. _Publisher.StartBroadcastingAliveMessages(TimeSpan.FromSeconds(options.BlastAliveMessageIntervalSeconds));
  212. }
  213. catch (Exception ex)
  214. {
  215. _logger.LogError(ex, "Error registering endpoint");
  216. }
  217. }
  218. private async Task RegisterServerEndpoints()
  219. {
  220. var addresses = (await _appHost.GetLocalIpAddresses(CancellationToken.None).ConfigureAwait(false)).ToList();
  221. var udn = CreateUuid(_appHost.SystemId);
  222. foreach (var address in addresses)
  223. {
  224. // TODO: Remove this condition on platforms that support it
  225. //if (address.AddressFamily == IpAddressFamily.InterNetworkV6)
  226. //{
  227. // continue;
  228. //}
  229. var fullService = "urn:schemas-upnp-org:device:MediaServer:1";
  230. _logger.LogInformation("Registering publisher for {0} on {1}", fullService, address.ToString());
  231. var descriptorUri = "/dlna/" + udn + "/description.xml";
  232. var uri = new Uri(_appHost.GetLocalApiUrl(address) + descriptorUri);
  233. var device = new SsdpRootDevice
  234. {
  235. CacheLifetime = TimeSpan.FromSeconds(1800), //How long SSDP clients can cache this info.
  236. Location = uri, // Must point to the URL that serves your devices UPnP description document.
  237. FriendlyName = "Jellyfin",
  238. Manufacturer = "Jellyfin",
  239. ModelName = "Jellyfin Server",
  240. Uuid = udn
  241. // This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc.
  242. };
  243. SetProperies(device, fullService);
  244. _Publisher.AddDevice(device);
  245. var embeddedDevices = new[]
  246. {
  247. "urn:schemas-upnp-org:service:ContentDirectory:1",
  248. "urn:schemas-upnp-org:service:ConnectionManager:1",
  249. //"urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1"
  250. };
  251. foreach (var subDevice in embeddedDevices)
  252. {
  253. var embeddedDevice = new SsdpEmbeddedDevice
  254. {
  255. FriendlyName = device.FriendlyName,
  256. Manufacturer = device.Manufacturer,
  257. ModelName = device.ModelName,
  258. Uuid = udn
  259. // This must be a globally unique value that survives reboots etc. Get from storage or embedded hardware etc.
  260. };
  261. SetProperies(embeddedDevice, subDevice);
  262. device.AddDevice(embeddedDevice);
  263. }
  264. }
  265. }
  266. private string CreateUuid(string text)
  267. {
  268. if (!Guid.TryParse(text, out var guid))
  269. {
  270. guid = text.GetMD5();
  271. }
  272. return guid.ToString("N");
  273. }
  274. private void SetProperies(SsdpDevice device, string fullDeviceType)
  275. {
  276. var service = fullDeviceType.Replace("urn:", string.Empty).Replace(":1", string.Empty);
  277. var serviceParts = service.Split(':');
  278. var deviceTypeNamespace = serviceParts[0].Replace('.', '-');
  279. device.DeviceTypeNamespace = deviceTypeNamespace;
  280. device.DeviceClass = serviceParts[1];
  281. device.DeviceType = serviceParts[2];
  282. }
  283. private readonly object _syncLock = new object();
  284. private void StartPlayToManager()
  285. {
  286. lock (_syncLock)
  287. {
  288. if (_manager != null)
  289. {
  290. return;
  291. }
  292. try
  293. {
  294. _manager = new PlayToManager(_logger,
  295. _sessionManager,
  296. _libraryManager,
  297. _userManager,
  298. _dlnaManager,
  299. _appHost,
  300. _imageProcessor,
  301. _deviceDiscovery,
  302. _httpClient,
  303. _config,
  304. _userDataManager,
  305. _localization,
  306. _mediaSourceManager,
  307. _mediaEncoder,
  308. _timerFactory);
  309. _manager.Start();
  310. }
  311. catch (Exception ex)
  312. {
  313. _logger.LogError(ex, "Error starting PlayTo manager");
  314. }
  315. }
  316. }
  317. private void DisposePlayToManager()
  318. {
  319. lock (_syncLock)
  320. {
  321. if (_manager != null)
  322. {
  323. try
  324. {
  325. _logger.LogInformation("Disposing PlayToManager");
  326. _manager.Dispose();
  327. }
  328. catch (Exception ex)
  329. {
  330. _logger.LogError(ex, "Error disposing PlayTo manager");
  331. }
  332. _manager = null;
  333. }
  334. }
  335. }
  336. public void Dispose()
  337. {
  338. DisposeDevicePublisher();
  339. DisposePlayToManager();
  340. DisposeDeviceDiscovery();
  341. if (_communicationsServer != null)
  342. {
  343. _logger.LogInformation("Disposing SsdpCommunicationsServer");
  344. _communicationsServer.Dispose();
  345. _communicationsServer = null;
  346. }
  347. ContentDirectory = null;
  348. ConnectionManager = null;
  349. MediaReceiverRegistrar = null;
  350. Current = null;
  351. }
  352. public void DisposeDevicePublisher()
  353. {
  354. if (_Publisher != null)
  355. {
  356. _logger.LogInformation("Disposing SsdpDevicePublisher");
  357. _Publisher.Dispose();
  358. _Publisher = null;
  359. }
  360. }
  361. }
  362. }