DeviceDiscovery.cs 5.0 KB

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