2020-01-22 20:00:07 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
#pragma warning disable SA1600
|
|
|
|
|
2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2016-10-29 22:22:20 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-01-13 19:16:19 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2016-10-29 22:22:20 +00:00
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
using MediaBrowser.Model.Events;
|
|
|
|
using Rssdp;
|
2016-11-14 19:48:01 +00:00
|
|
|
using Rssdp.Infrastructure;
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2016-10-29 22:34:54 +00:00
|
|
|
namespace Emby.Dlna.Ssdp
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2020-01-22 20:00:07 +00:00
|
|
|
public sealed class DeviceDiscovery : IDeviceDiscovery, IDisposable
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2020-01-22 20:00:07 +00:00
|
|
|
private readonly object _syncLock = new object();
|
2016-10-29 22:22:20 +00:00
|
|
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private int _listenerCount;
|
2020-01-22 20:00:07 +00:00
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
private event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscoveredInternal;
|
2019-11-01 20:22:35 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2018-09-12 17:26:21 +00:00
|
|
|
public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscovered
|
|
|
|
{
|
|
|
|
add
|
|
|
|
{
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
|
|
|
_listenerCount++;
|
|
|
|
DeviceDiscoveredInternal += value;
|
|
|
|
}
|
2019-11-01 20:22:35 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
StartInternal();
|
|
|
|
}
|
2020-01-22 20:00:07 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
remove
|
|
|
|
{
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
|
|
|
_listenerCount--;
|
|
|
|
DeviceDiscoveredInternal -= value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 20:22:35 +00:00
|
|
|
/// <inheritdoc />
|
2016-10-29 22:22:20 +00:00
|
|
|
public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceLeft;
|
|
|
|
|
2016-11-14 19:48:01 +00:00
|
|
|
private SsdpDeviceLocator _deviceLocator;
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
private ISsdpCommunicationsServer _commsServer;
|
2016-11-04 08:31:05 +00:00
|
|
|
|
2019-11-01 20:22:35 +00:00
|
|
|
public DeviceDiscovery(IServerConfigurationManager config)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call this method from somewhere in your code to start the search.
|
2016-11-14 19:48:01 +00:00
|
|
|
public void Start(ISsdpCommunicationsServer communicationsServer)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
_commsServer = communicationsServer;
|
|
|
|
|
|
|
|
StartInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void StartInternal()
|
|
|
|
{
|
|
|
|
lock (_syncLock)
|
|
|
|
{
|
|
|
|
if (_listenerCount > 0 && _deviceLocator == null)
|
|
|
|
{
|
2019-02-05 08:49:46 +00:00
|
|
|
_deviceLocator = new SsdpDeviceLocator(_commsServer);
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2019-01-07 23:27:46 +00:00
|
|
|
// (Optional) Set the filter so we only see notifications for devices we care about
|
|
|
|
// (can be any search target value i.e device type, uuid value etc - any value that appears in the
|
2018-09-12 17:26:21 +00:00
|
|
|
// DiscoverdSsdpDevice.NotificationType property or that is used with the searchTarget parameter of the Search method).
|
|
|
|
//_DeviceLocator.NotificationFilter = "upnp:rootdevice";
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
// Connect our event handler so we process devices as they are found
|
2019-11-01 20:22:35 +00:00
|
|
|
_deviceLocator.DeviceAvailable += OnDeviceLocatorDeviceAvailable;
|
|
|
|
_deviceLocator.DeviceUnavailable += OnDeviceLocatorDeviceUnavailable;
|
2016-10-29 22:22:20 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var dueTime = TimeSpan.FromSeconds(5);
|
|
|
|
var interval = TimeSpan.FromSeconds(_config.GetDlnaConfiguration().ClientDiscoveryIntervalSeconds);
|
2017-02-05 20:44:08 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
_deviceLocator.RestartBroadcastTimer(dueTime, interval);
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process each found device in the event handler
|
2019-11-01 20:22:35 +00:00
|
|
|
private void OnDeviceLocatorDeviceAvailable(object sender, DeviceAvailableEventArgs e)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
|
|
|
var originalHeaders = e.DiscoveredDevice.ResponseHeaders;
|
|
|
|
|
|
|
|
var headerDict = originalHeaders == null ? new Dictionary<string, KeyValuePair<string, IEnumerable<string>>>() : originalHeaders.ToDictionary(i => i.Key, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
var headers = headerDict.ToDictionary(i => i.Key, i => i.Value.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
var args = new GenericEventArgs<UpnpDeviceInfo>
|
|
|
|
{
|
|
|
|
Argument = new UpnpDeviceInfo
|
|
|
|
{
|
|
|
|
Location = e.DiscoveredDevice.DescriptionLocation,
|
2017-01-24 19:54:18 +00:00
|
|
|
Headers = headers,
|
|
|
|
LocalIpAddress = e.LocalIpAddress
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-28 14:21:02 +00:00
|
|
|
DeviceDiscoveredInternal?.Invoke(this, args);
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 20:22:35 +00:00
|
|
|
private void OnDeviceLocatorDeviceUnavailable(object sender, DeviceUnavailableEventArgs e)
|
2016-10-29 22:22:20 +00:00
|
|
|
{
|
|
|
|
var originalHeaders = e.DiscoveredDevice.ResponseHeaders;
|
|
|
|
|
|
|
|
var headerDict = originalHeaders == null ? new Dictionary<string, KeyValuePair<string, IEnumerable<string>>>() : originalHeaders.ToDictionary(i => i.Key, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
var headers = headerDict.ToDictionary(i => i.Key, i => i.Value.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
var args = new GenericEventArgs<UpnpDeviceInfo>
|
|
|
|
{
|
|
|
|
Argument = new UpnpDeviceInfo
|
|
|
|
{
|
|
|
|
Location = e.DiscoveredDevice.DescriptionLocation,
|
|
|
|
Headers = headers
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-28 14:21:02 +00:00
|
|
|
DeviceLeft?.Invoke(this, args);
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 20:00:07 +00:00
|
|
|
/// <inheritdoc />
|
2016-10-29 22:22:20 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (!_disposed)
|
|
|
|
{
|
|
|
|
_disposed = true;
|
2017-02-07 07:33:24 +00:00
|
|
|
if (_deviceLocator != null)
|
|
|
|
{
|
|
|
|
_deviceLocator.Dispose();
|
|
|
|
_deviceLocator = null;
|
|
|
|
}
|
2016-10-29 22:22:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|