2014-07-18 19:07:28 +00:00
|
|
|
|
using MediaBrowser.Common.Events;
|
2014-09-17 03:04:10 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2015-04-29 18:48:34 +00:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-01-07 03:54:26 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2016-09-11 07:33:53 +00:00
|
|
|
|
using MediaBrowser.Model.Events;
|
|
|
|
|
using Rssdp;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.Ssdp
|
|
|
|
|
{
|
2015-07-23 16:32:34 +00:00
|
|
|
|
public class DeviceDiscovery : IDeviceDiscovery, IDisposable
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly CancellationTokenSource _tokenSource;
|
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceDiscovered;
|
|
|
|
|
public event EventHandler<GenericEventArgs<UpnpDeviceInfo>> DeviceLeft;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
private SsdpDeviceLocator _DeviceLocator;
|
|
|
|
|
|
|
|
|
|
public DeviceDiscovery(ILogger logger, IServerConfigurationManager config)
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
|
|
|
|
_tokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
|
|
|
|
}
|
2015-07-23 16:32:34 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
// Call this method from somewhere in your code to start the search.
|
|
|
|
|
public void BeginSearch()
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2016-09-11 07:33:53 +00:00
|
|
|
|
_DeviceLocator = new SsdpDeviceLocator();
|
|
|
|
|
|
|
|
|
|
// (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
|
|
|
|
|
// DiscoverdSsdpDevice.NotificationType property or that is used with the searchTarget parameter of the Search method).
|
|
|
|
|
//_DeviceLocator.NotificationFilter = "upnp:rootdevice";
|
|
|
|
|
|
|
|
|
|
// Connect our event handler so we process devices as they are found
|
|
|
|
|
_DeviceLocator.DeviceAvailable += deviceLocator_DeviceAvailable;
|
|
|
|
|
_DeviceLocator.DeviceUnavailable += _DeviceLocator_DeviceUnavailable;
|
|
|
|
|
|
|
|
|
|
// Perform a search so we don't have to wait for devices to broadcast notifications
|
|
|
|
|
// again to get any results right away (notifications are broadcast periodically).
|
|
|
|
|
StartAsyncSearch();
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
private void StartAsyncSearch()
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
|
|
|
|
Task.Factory.StartNew(async (o) =>
|
|
|
|
|
{
|
2016-09-13 17:49:13 +00:00
|
|
|
|
while (!_tokenSource.IsCancellationRequested)
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2016-09-13 17:49:13 +00:00
|
|
|
|
try
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2016-09-13 17:49:13 +00:00
|
|
|
|
// Enable listening for notifications (optional)
|
|
|
|
|
_DeviceLocator.StartListeningForNotifications();
|
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
await _DeviceLocator.SearchAsync().ConfigureAwait(false);
|
2016-09-13 17:49:13 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error searching for devices", ex);
|
|
|
|
|
}
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2016-09-13 17:49:13 +00:00
|
|
|
|
var delay = _config.GetDlnaConfiguration().ClientDiscoveryIntervalSeconds * 1000;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2016-09-13 17:49:13 +00:00
|
|
|
|
await Task.Delay(delay, _tokenSource.Token).ConfigureAwait(false);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
}, CancellationToken.None, TaskCreationOptions.LongRunning);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
// Process each found device in the event handler
|
|
|
|
|
void deviceLocator_DeviceAvailable(object sender, DeviceAvailableEventArgs e)
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2016-09-11 07:33:53 +00:00
|
|
|
|
var originalHeaders = e.DiscoveredDevice.ResponseHeaders;
|
2014-09-17 03:04:10 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
var headerDict = originalHeaders == null ? new Dictionary<string, KeyValuePair<string, IEnumerable<string>>>() : originalHeaders.ToDictionary(i => i.Key, StringComparer.OrdinalIgnoreCase);
|
2015-07-23 16:32:34 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
var headers = headerDict.ToDictionary(i => i.Key, i => i.Value.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
var args = new GenericEventArgs<UpnpDeviceInfo>
|
2015-05-04 18:01:01 +00:00
|
|
|
|
{
|
2016-09-11 07:33:53 +00:00
|
|
|
|
Argument = new UpnpDeviceInfo
|
2015-05-04 18:01:01 +00:00
|
|
|
|
{
|
2016-09-11 07:33:53 +00:00
|
|
|
|
Location = e.DiscoveredDevice.DescriptionLocation,
|
|
|
|
|
Headers = headers
|
2015-05-04 18:01:01 +00:00
|
|
|
|
}
|
2016-09-11 07:33:53 +00:00
|
|
|
|
};
|
2015-05-04 18:01:01 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
EventHelper.FireEventIfNotNull(DeviceDiscovered, this, args, _logger);
|
|
|
|
|
}
|
2015-05-04 18:01:01 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
private void _DeviceLocator_DeviceUnavailable(object sender, DeviceUnavailableEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var originalHeaders = e.DiscoveredDevice.ResponseHeaders;
|
|
|
|
|
|
|
|
|
|
var headerDict = originalHeaders == null ? new Dictionary<string, KeyValuePair<string, IEnumerable<string>>>() : originalHeaders.ToDictionary(i => i.Key, StringComparer.OrdinalIgnoreCase);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
var headers = headerDict.ToDictionary(i => i.Key, i => i.Value.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
var args = new GenericEventArgs<UpnpDeviceInfo>
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2016-09-11 07:33:53 +00:00
|
|
|
|
Argument = new UpnpDeviceInfo
|
|
|
|
|
{
|
|
|
|
|
Location = e.DiscoveredDevice.DescriptionLocation,
|
|
|
|
|
Headers = headers
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
EventHelper.FireEventIfNotNull(DeviceLeft, this, args, _logger);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
public void Start()
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2016-09-11 07:33:53 +00:00
|
|
|
|
BeginSearch();
|
|
|
|
|
}
|
2014-08-26 02:30:52 +00:00
|
|
|
|
|
2016-09-11 07:33:53 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-07-18 19:07:28 +00:00
|
|
|
|
if (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
_disposed = true;
|
|
|
|
|
_tokenSource.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|