DeviceDiscovery.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Model.Dlna;
  7. using MediaBrowser.Model.Events;
  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 int _listenerCount;
  17. private bool _disposed;
  18. private event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscoveredInternal;
  19. /// <inheritdoc />
  20. public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscovered
  21. {
  22. add
  23. {
  24. lock (_syncLock)
  25. {
  26. _listenerCount++;
  27. DeviceDiscoveredInternal += value;
  28. }
  29. StartInternal();
  30. }
  31. remove
  32. {
  33. lock (_syncLock)
  34. {
  35. _listenerCount--;
  36. DeviceDiscoveredInternal -= value;
  37. }
  38. }
  39. }
  40. /// <inheritdoc />
  41. public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceLeft;
  42. private SsdpDeviceLocator _deviceLocator;
  43. private ISsdpCommunicationsServer _commsServer;
  44. public DeviceDiscovery(IServerConfigurationManager config)
  45. {
  46. _config = config;
  47. }
  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. {
  82. Argument = new UpnpDeviceInfo
  83. {
  84. Location = e.DiscoveredDevice.DescriptionLocation,
  85. Headers = headers,
  86. LocalIpAddress = e.LocalIpAddress
  87. }
  88. };
  89. DeviceDiscoveredInternal?.Invoke(this, args);
  90. }
  91. private void OnDeviceLocatorDeviceUnavailable(object sender, DeviceUnavailableEventArgs e)
  92. {
  93. var originalHeaders = e.DiscoveredDevice.ResponseHeaders;
  94. var headerDict = originalHeaders == null ? new Dictionary<string, KeyValuePair<string, IEnumerable<string>>>() : originalHeaders.ToDictionary(i => i.Key, StringComparer.OrdinalIgnoreCase);
  95. var headers = headerDict.ToDictionary(i => i.Key, i => i.Value.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
  96. var args = new GenericEventArgs<UpnpDeviceInfo>
  97. {
  98. Argument = new UpnpDeviceInfo
  99. {
  100. Location = e.DiscoveredDevice.DescriptionLocation,
  101. Headers = headers
  102. }
  103. };
  104. DeviceLeft?.Invoke(this, args);
  105. }
  106. /// <inheritdoc />
  107. public void Dispose()
  108. {
  109. if (!_disposed)
  110. {
  111. _disposed = true;
  112. if (_deviceLocator != null)
  113. {
  114. _deviceLocator.Dispose();
  115. _deviceLocator = null;
  116. }
  117. }
  118. }
  119. }
  120. }