DeviceDiscovery.cs 5.5 KB

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