2014-02-27 03:44:00 +01:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-03-17 05:25:11 +01:00
|
|
|
|
using MediaBrowser.Controller;
|
2014-03-13 15:54:11 +01:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-03-13 20:08:02 +01:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
2014-03-25 06:25:03 +01:00
|
|
|
|
using MediaBrowser.Dlna.Ssdp;
|
2014-03-21 04:31:40 +01:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-03-23 07:07:43 +01:00
|
|
|
|
using MediaBrowser.Model.Session;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2014-03-25 06:25:03 +01:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.PlayTo
|
|
|
|
|
{
|
|
|
|
|
class PlayToManager : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private bool _disposed = false;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly CancellationTokenSource _tokenSource;
|
|
|
|
|
private ConcurrentDictionary<string, DateTime> _locations;
|
|
|
|
|
|
|
|
|
|
private readonly IItemRepository _itemRepository;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly INetworkManager _networkManager;
|
2014-03-13 15:54:11 +01:00
|
|
|
|
private readonly IUserManager _userManager;
|
2014-03-13 20:08:02 +01:00
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
2014-03-14 18:09:22 +01:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-03-17 05:25:11 +01:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2014-02-27 19:00:49 +01:00
|
|
|
|
|
2014-03-17 05:25:11 +01:00
|
|
|
|
public PlayToManager(ILogger logger, IServerConfigurationManager config, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepository, ILibraryManager libraryManager, INetworkManager networkManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
|
|
|
|
_locations = new ConcurrentDictionary<string, DateTime>();
|
|
|
|
|
_tokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_itemRepository = itemRepository;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_networkManager = networkManager;
|
2014-02-27 19:00:49 +01:00
|
|
|
|
_userManager = userManager;
|
2014-03-13 20:08:02 +01:00
|
|
|
|
_dlnaManager = dlnaManager;
|
2014-03-17 05:25:11 +01:00
|
|
|
|
_appHost = appHost;
|
2014-03-14 18:09:22 +01:00
|
|
|
|
_config = config;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-23 07:07:43 +01:00
|
|
|
|
public void Start()
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
|
|
|
|
_locations = new ConcurrentDictionary<string, DateTime>();
|
|
|
|
|
|
|
|
|
|
foreach (var network in NetworkInterface.GetAllNetworkInterfaces())
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug("Found interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus);
|
|
|
|
|
|
|
|
|
|
if (!network.SupportsMulticast || OperationalStatus.Up != network.OperationalStatus || !network.GetIPProperties().MulticastAddresses.Any())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ipV4 = network.GetIPProperties().GetIPv4Properties();
|
|
|
|
|
if (null == ipV4)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
IPAddress localIp = null;
|
|
|
|
|
|
2014-03-23 07:07:43 +01:00
|
|
|
|
foreach (var ipInfo in network.GetIPProperties().UnicastAddresses)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (ipInfo.Address.AddressFamily == AddressFamily.InterNetwork)
|
|
|
|
|
{
|
|
|
|
|
localIp = ipInfo.Address;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (localIp == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CreateListener(localIp);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Failed to Initilize Socket", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a socket for the interface and listends for data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="localIp">The local ip.</param>
|
|
|
|
|
private void CreateListener(IPAddress localIp)
|
|
|
|
|
{
|
|
|
|
|
Task.Factory.StartNew(async (o) =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var socket = GetMulticastSocket();
|
|
|
|
|
|
|
|
|
|
socket.Bind(new IPEndPoint(localIp, 0));
|
|
|
|
|
|
|
|
|
|
_logger.Info("Creating SSDP listener");
|
|
|
|
|
|
|
|
|
|
var receiveBuffer = new byte[64000];
|
|
|
|
|
|
|
|
|
|
CreateNotifier(socket);
|
|
|
|
|
|
|
|
|
|
while (!_tokenSource.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
var receivedBytes = await socket.ReceiveAsync(receiveBuffer, 0, 64000);
|
|
|
|
|
|
|
|
|
|
if (receivedBytes > 0)
|
|
|
|
|
{
|
2014-03-25 06:25:03 +01:00
|
|
|
|
var headers = SsdpHelper.ParseSsdpResponse(receiveBuffer);
|
2014-02-27 03:44:00 +01:00
|
|
|
|
|
2014-03-25 06:25:03 +01:00
|
|
|
|
TryCreateController(headers);
|
2014-02-27 03:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.Info("SSDP listener - Task completed");
|
|
|
|
|
}
|
2014-03-23 21:07:02 +01:00
|
|
|
|
catch (OperationCanceledException)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in listener", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, _tokenSource.Token, TaskCreationOptions.LongRunning);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 23:55:19 +02:00
|
|
|
|
private void TryCreateController(IDictionary<string, string> headers)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
2014-03-25 06:25:03 +01:00
|
|
|
|
string location;
|
|
|
|
|
|
|
|
|
|
if (!headers.TryGetValue("Location", out location))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-27 03:44:00 +01:00
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-03-25 06:25:03 +01:00
|
|
|
|
await CreateController(new Uri(location)).ConfigureAwait(false);
|
2014-02-27 03:44:00 +01:00
|
|
|
|
}
|
2014-03-23 21:07:02 +01:00
|
|
|
|
catch (OperationCanceledException)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error creating play to controller", ex);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateNotifier(Socket socket)
|
|
|
|
|
{
|
|
|
|
|
Task.Factory.StartNew(async (o) =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var request = SsdpHelper.CreateRendererSSDP(3);
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
socket.SendTo(request, new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900));
|
|
|
|
|
|
2014-03-23 22:46:13 +01:00
|
|
|
|
var delay = _config.Configuration.DlnaOptions.ClientDiscoveryIntervalSeconds * 1000;
|
2014-03-23 21:49:05 +01:00
|
|
|
|
|
|
|
|
|
await Task.Delay(delay).ConfigureAwait(false);
|
2014-02-27 03:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-23 21:07:02 +01:00
|
|
|
|
catch (OperationCanceledException)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error in notifier", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, _tokenSource.Token, TaskCreationOptions.LongRunning);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a socket configured for SDDP multicasting.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private Socket GetMulticastSocket()
|
|
|
|
|
{
|
|
|
|
|
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250")));
|
|
|
|
|
//socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 3);
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new DlnaSessionController.
|
|
|
|
|
/// and logs the session in SessionManager
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uri">The URI.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private async Task CreateController(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
if (!IsUriValid(uri))
|
|
|
|
|
return;
|
|
|
|
|
|
2014-03-14 18:09:22 +01:00
|
|
|
|
var device = await Device.CreateuPnpDeviceAsync(uri, _httpClient, _config, _logger).ConfigureAwait(false);
|
2014-02-27 03:44:00 +01:00
|
|
|
|
|
|
|
|
|
if (device != null && device.RendererCommands != null && !_sessionManager.Sessions.Any(s => string.Equals(s.DeviceId, device.Properties.UUID) && s.IsActive))
|
|
|
|
|
{
|
2014-03-25 06:25:03 +01:00
|
|
|
|
var sessionInfo = await _sessionManager.LogSessionActivity(device.Properties.ClientType, _appHost.ApplicationVersion.ToString(), device.Properties.UUID, device.Properties.Name, uri.OriginalString, null)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var controller = sessionInfo.SessionController as PlayToController;
|
|
|
|
|
|
|
|
|
|
if (controller == null)
|
|
|
|
|
{
|
2014-03-17 05:25:11 +01:00
|
|
|
|
sessionInfo.SessionController = controller = new PlayToController(sessionInfo, _sessionManager, _itemRepository, _libraryManager, _logger, _networkManager, _dlnaManager, _userManager, _appHost);
|
2014-03-14 18:09:22 +01:00
|
|
|
|
|
2014-03-25 06:25:03 +01:00
|
|
|
|
controller.Init(device);
|
2014-02-27 03:44:00 +01:00
|
|
|
|
|
2014-03-26 17:10:46 +01:00
|
|
|
|
var profile = _dlnaManager.GetProfile(device.Properties.ToDeviceIdentification()) ??
|
|
|
|
|
_dlnaManager.GetDefaultProfile();
|
|
|
|
|
|
2014-03-25 06:25:03 +01:00
|
|
|
|
_sessionManager.ReportCapabilities(sessionInfo.Id, new SessionCapabilities
|
|
|
|
|
{
|
2014-04-02 23:55:19 +02:00
|
|
|
|
PlayableMediaTypes = profile.GetSupportedMediaTypes(),
|
2014-03-25 22:13:55 +01:00
|
|
|
|
|
2014-04-02 23:55:19 +02:00
|
|
|
|
SupportedCommands = new List<string>
|
|
|
|
|
{
|
|
|
|
|
GeneralCommandType.VolumeDown.ToString(),
|
|
|
|
|
GeneralCommandType.VolumeUp.ToString(),
|
|
|
|
|
GeneralCommandType.Mute.ToString(),
|
|
|
|
|
GeneralCommandType.Unmute.ToString(),
|
|
|
|
|
GeneralCommandType.ToggleMute.ToString()
|
|
|
|
|
}
|
2014-03-25 06:25:03 +01:00
|
|
|
|
});
|
2014-02-27 19:00:49 +01:00
|
|
|
|
|
2014-03-25 06:25:03 +01:00
|
|
|
|
_logger.Info("DLNA Session created for {0} - {1}", device.Properties.Name, device.Properties.ModelName);
|
|
|
|
|
}
|
2014-03-13 22:29:25 +01:00
|
|
|
|
}
|
2014-02-27 19:00:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-27 03:44:00 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines if the Uri is valid for further inspection or not.
|
|
|
|
|
/// (the limit for reinspection is 5 minutes)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uri">The URI.</param>
|
|
|
|
|
/// <returns>Returns <b>True</b> if the Uri is valid for further inspection</returns>
|
|
|
|
|
private bool IsUriValid(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
if (uri == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!_locations.ContainsKey(uri.OriginalString))
|
|
|
|
|
{
|
|
|
|
|
_locations.AddOrUpdate(uri.OriginalString, DateTime.UtcNow, (key, existingVal) => existingVal);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var time = _locations[uri.OriginalString];
|
|
|
|
|
|
|
|
|
|
if ((DateTime.UtcNow - time).TotalMinutes <= 5)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return _locations.TryUpdate(uri.OriginalString, DateTime.UtcNow, time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (!_disposed)
|
|
|
|
|
{
|
|
|
|
|
_disposed = true;
|
|
|
|
|
_tokenSource.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|