DeviceDiscovery.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Jellyfin.Data.Events;
  7. using MediaBrowser.Controller.Configuration;
  8. using MediaBrowser.Model.Dlna;
  9. using Rssdp;
  10. using Rssdp.Infrastructure;
  11. namespace Emby.Dlna.Ssdp
  12. {
  13. public sealed class DeviceDiscovery : IDeviceDiscovery, IDisposable
  14. {
  15. private readonly object _syncLock = new object();
  16. private readonly IServerConfigurationManager _config;
  17. private SsdpDeviceLocator _deviceLocator;
  18. private ISsdpCommunicationsServer _commsServer;
  19. private int _listenerCount;
  20. private bool _disposed;
  21. public DeviceDiscovery(IServerConfigurationManager config)
  22. {
  23. _config = config;
  24. }
  25. private event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscoveredInternal;
  26. /// <inheritdoc />
  27. public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscovered
  28. {
  29. add
  30. {
  31. lock (_syncLock)
  32. {
  33. _listenerCount++;
  34. DeviceDiscoveredInternal += value;
  35. }
  36. StartInternal();
  37. }
  38. remove
  39. {
  40. lock (_syncLock)
  41. {
  42. _listenerCount--;
  43. DeviceDiscoveredInternal -= value;
  44. }
  45. }
  46. }
  47. /// <inheritdoc />
  48. public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceLeft;
  49. // Call this method from somewhere in your code to start the search.
  50. public void Start(ISsdpCommunicationsServer communicationsServer)
  51. {
  52. _commsServer = communicationsServer;
  53. StartInternal();
  54. }
  55. private void StartInternal()
  56. {
  57. lock (_syncLock)
  58. {
  59. if (_listenerCount > 0 && _deviceLocator is null && _commsServer is not null)
  60. {
  61. _deviceLocator = new SsdpDeviceLocator(_commsServer);
  62. // (Optional) Set the filter so we only see notifications for devices we care about
  63. // (can be any search target value i.e device type, uuid value etc - any value that appears in the
  64. // DiscoverdSsdpDevice.NotificationType property or that is used with the searchTarget parameter of the Search method).
  65. // _DeviceLocator.NotificationFilter = "upnp:rootdevice";
  66. // Connect our event handler so we process devices as they are found
  67. _deviceLocator.DeviceAvailable += OnDeviceLocatorDeviceAvailable;
  68. _deviceLocator.DeviceUnavailable += OnDeviceLocatorDeviceUnavailable;
  69. var dueTime = TimeSpan.FromSeconds(5);
  70. var interval = TimeSpan.FromSeconds(_config.GetDlnaConfiguration().ClientDiscoveryIntervalSeconds);
  71. _deviceLocator.RestartBroadcastTimer(dueTime, interval);
  72. }
  73. }
  74. }
  75. // Process each found device in the event handler
  76. private void OnDeviceLocatorDeviceAvailable(object sender, DeviceAvailableEventArgs e)
  77. {
  78. var originalHeaders = e.DiscoveredDevice.ResponseHeaders;
  79. var headerDict = originalHeaders is null ? new Dictionary<string, KeyValuePair<string, IEnumerable<string>>>() : originalHeaders.ToDictionary(i => i.Key, StringComparer.OrdinalIgnoreCase);
  80. var headers = headerDict.ToDictionary(i => i.Key, i => i.Value.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
  81. var args = new GenericEventArgs<UpnpDeviceInfo>(
  82. new UpnpDeviceInfo
  83. {
  84. Location = e.DiscoveredDevice.DescriptionLocation,
  85. Headers = headers,
  86. RemoteIpAddress = e.RemoteIpAddress
  87. });
  88. DeviceDiscoveredInternal?.Invoke(this, args);
  89. }
  90. private void OnDeviceLocatorDeviceUnavailable(object sender, DeviceUnavailableEventArgs e)
  91. {
  92. var originalHeaders = e.DiscoveredDevice.ResponseHeaders;
  93. var headerDict = originalHeaders is null ? new Dictionary<string, KeyValuePair<string, IEnumerable<string>>>() : originalHeaders.ToDictionary(i => i.Key, StringComparer.OrdinalIgnoreCase);
  94. var headers = headerDict.ToDictionary(i => i.Key, i => i.Value.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
  95. var args = new GenericEventArgs<UpnpDeviceInfo>(
  96. new UpnpDeviceInfo
  97. {
  98. Location = e.DiscoveredDevice.DescriptionLocation,
  99. Headers = headers
  100. });
  101. DeviceLeft?.Invoke(this, args);
  102. }
  103. /// <inheritdoc />
  104. public void Dispose()
  105. {
  106. if (!_disposed)
  107. {
  108. _disposed = true;
  109. if (_deviceLocator is not null)
  110. {
  111. _deviceLocator.Dispose();
  112. _deviceLocator = null;
  113. }
  114. }
  115. }
  116. }
  117. }