DeviceDiscovery.cs 4.6 KB

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