DeviceDiscovery.cs 5.0 KB

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