jellyfin/MediaBrowser.Dlna/PlayTo/PlayToManager.cs

317 lines
12 KiB
C#
Raw Normal View History

2014-04-26 20:42:05 -07:00
using MediaBrowser.Common.Net;
2014-03-16 21:25:11 -07:00
using MediaBrowser.Controller;
2014-03-13 07:54:11 -07:00
using MediaBrowser.Controller.Configuration;
2014-03-13 12:08:02 -07:00
using MediaBrowser.Controller.Dlna;
2014-04-23 09:29:21 -07:00
using MediaBrowser.Controller.Drawing;
2014-04-17 22:03:01 -07:00
using MediaBrowser.Controller.Dto;
2014-02-26 19:44:00 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Session;
2014-03-24 22:25:03 -07:00
using MediaBrowser.Dlna.Ssdp;
2014-02-26 19:44:00 -07:00
using MediaBrowser.Model.Logging;
2014-03-22 23:07:43 -07:00
using MediaBrowser.Model.Session;
2014-02-26 19:44:00 -07:00
using System;
2014-03-24 22:25:03 -07:00
using System.Collections.Generic;
2014-02-26 19:44:00 -07:00
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
2014-04-26 20:42:05 -07:00
using System.Text;
2014-02-26 19:44:00 -07:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Dlna.PlayTo
{
class PlayToManager : IDisposable
{
2014-04-22 10:25:54 -07:00
private bool _disposed;
2014-02-26 19:44:00 -07:00
private readonly ILogger _logger;
private readonly ISessionManager _sessionManager;
private readonly IHttpClient _httpClient;
private readonly CancellationTokenSource _tokenSource;
private readonly IItemRepository _itemRepository;
private readonly ILibraryManager _libraryManager;
private readonly INetworkManager _networkManager;
2014-03-13 07:54:11 -07:00
private readonly IUserManager _userManager;
2014-03-13 12:08:02 -07:00
private readonly IDlnaManager _dlnaManager;
2014-03-14 10:09:22 -07:00
private readonly IServerConfigurationManager _config;
2014-03-16 21:25:11 -07:00
private readonly IServerApplicationHost _appHost;
2014-04-17 22:03:01 -07:00
private readonly IDtoService _dtoService;
2014-04-23 09:29:21 -07:00
private readonly IImageProcessor _imageProcessor;
2014-02-27 11:00:49 -07:00
2014-04-26 20:42:05 -07:00
private readonly SsdpHandler _ssdpHandler;
public PlayToManager(ILogger logger, IServerConfigurationManager config, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepository, ILibraryManager libraryManager, INetworkManager networkManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost, IDtoService dtoService, IImageProcessor imageProcessor, SsdpHandler ssdpHandler)
2014-02-26 19:44:00 -07:00
{
_tokenSource = new CancellationTokenSource();
_logger = logger;
_sessionManager = sessionManager;
_httpClient = httpClient;
_itemRepository = itemRepository;
_libraryManager = libraryManager;
_networkManager = networkManager;
2014-02-27 11:00:49 -07:00
_userManager = userManager;
2014-03-13 12:08:02 -07:00
_dlnaManager = dlnaManager;
2014-03-16 21:25:11 -07:00
_appHost = appHost;
2014-04-17 22:03:01 -07:00
_dtoService = dtoService;
2014-04-23 09:29:21 -07:00
_imageProcessor = imageProcessor;
2014-04-26 20:42:05 -07:00
_ssdpHandler = ssdpHandler;
2014-03-14 10:09:22 -07:00
_config = config;
2014-02-26 19:44:00 -07:00
}
2014-03-22 23:07:43 -07:00
public void Start()
2014-02-26 19:44:00 -07:00
{
2014-04-07 21:17:18 -07:00
foreach (var network in GetNetworkInterfaces())
2014-02-26 19:44:00 -07:00
{
_logger.Debug("Found interface: {0}. Type: {1}. Status: {2}", network.Name, network.NetworkInterfaceType, network.OperationalStatus);
2014-04-25 10:30:41 -07:00
if (!network.SupportsMulticast || OperationalStatus.Up != network.OperationalStatus || !network.GetIPProperties().MulticastAddresses.Any())
2014-02-26 19:44:00 -07:00
continue;
var ipV4 = network.GetIPProperties().GetIPv4Properties();
if (null == ipV4)
continue;
2014-04-22 10:25:54 -07:00
var localIp = network.GetIPProperties().UnicastAddresses
.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork)
.Select(i => i.Address)
.FirstOrDefault();
2014-02-26 19:44:00 -07:00
2014-04-22 10:25:54 -07:00
if (localIp != null)
2014-02-26 19:44:00 -07:00
{
2014-04-22 10:25:54 -07:00
try
2014-02-26 19:44:00 -07:00
{
2014-04-25 10:30:41 -07:00
CreateListener(localIp, ipV4.Index);
2014-04-22 10:25:54 -07:00
}
catch (Exception e)
{
_logger.ErrorException("Failed to Initilize Socket", e);
2014-02-26 19:44:00 -07:00
}
}
}
}
2014-04-07 21:17:18 -07:00
private IEnumerable<NetworkInterface> GetNetworkInterfaces()
{
try
{
return NetworkInterface.GetAllNetworkInterfaces();
}
catch (Exception ex)
{
_logger.ErrorException("Error in GetAllNetworkInterfaces", ex);
return new List<NetworkInterface>();
}
}
2014-02-26 19:44:00 -07:00
/// <summary>
/// Creates a socket for the interface and listends for data.
/// </summary>
/// <param name="localIp">The local ip.</param>
2014-04-25 19:55:07 -07:00
/// <param name="networkInterfaceIndex">Index of the network interface.</param>
2014-04-25 10:30:41 -07:00
private void CreateListener(IPAddress localIp, int networkInterfaceIndex)
2014-02-26 19:44:00 -07:00
{
Task.Factory.StartNew(async (o) =>
{
try
{
2014-04-25 10:30:41 -07:00
var socket = GetMulticastSocket(networkInterfaceIndex);
2014-02-26 19:44:00 -07:00
2014-04-26 20:42:05 -07:00
var endPoint = new IPEndPoint(localIp, 1900);
socket.Bind(endPoint);
2014-02-26 19:44:00 -07:00
_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-04-26 20:42:05 -07:00
var args = SsdpHelper.ParseSsdpResponse(receiveBuffer, endPoint);
2014-02-26 19:44:00 -07:00
2014-04-26 20:42:05 -07:00
TryCreateController(args);
2014-02-26 19:44:00 -07:00
}
}
_logger.Info("SSDP listener - Task completed");
}
2014-03-23 13:07:02 -07:00
catch (OperationCanceledException)
2014-02-26 19:44:00 -07:00
{
}
catch (Exception e)
{
_logger.ErrorException("Error in listener", e);
}
}, _tokenSource.Token, TaskCreationOptions.LongRunning);
}
2014-04-26 20:42:05 -07:00
private void TryCreateController(SsdpMessageEventArgs args)
2014-02-26 19:44:00 -07:00
{
2014-04-26 20:42:05 -07:00
string nts;
args.Headers.TryGetValue("NTS", out nts);
string usn;
if (!args.Headers.TryGetValue("USN", out usn)) usn = string.Empty;
string nt;
if (!args.Headers.TryGetValue("NT", out nt)) nt = string.Empty;
// Don't create a new controller when a device is indicating it's shutting down
if (string.Equals(nts, "ssdp:byebye", StringComparison.OrdinalIgnoreCase))
{
return;
}
// It has to report that it's a media renderer
if (usn.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) == -1 &&
nt.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) == -1)
{
return;
}
// Need to be able to download device description
2014-03-24 22:25:03 -07:00
string location;
2014-04-26 20:42:05 -07:00
if (!args.Headers.TryGetValue("Location", out location) ||
string.IsNullOrEmpty(location))
{
return;
}
2014-03-24 22:25:03 -07:00
2014-04-26 20:42:05 -07:00
if (_config.Configuration.DlnaOptions.EnableDebugLogging)
{
var headerTexts = args.Headers.Select(i => string.Format("{0}={1}", i.Key, i.Value));
var headerText = string.Join(",", headerTexts.ToArray());
_logger.Debug("{0} PlayTo message received from {1}. Headers: {2}", args.Method, args.EndPoint, headerText);
}
if (_sessionManager.Sessions.Any(i => usn.IndexOf(i.DeviceId, StringComparison.OrdinalIgnoreCase) != -1))
2014-03-24 22:25:03 -07:00
{
return;
}
2014-02-26 19:44:00 -07:00
Task.Run(async () =>
{
try
{
2014-03-24 22:25:03 -07:00
await CreateController(new Uri(location)).ConfigureAwait(false);
2014-02-26 19:44:00 -07:00
}
2014-03-23 13:07:02 -07:00
catch (OperationCanceledException)
2014-02-26 19:44:00 -07:00
{
}
catch (Exception ex)
{
_logger.ErrorException("Error creating play to controller", ex);
}
});
}
private void CreateNotifier(Socket socket)
{
Task.Factory.StartNew(async (o) =>
{
try
{
2014-04-25 10:30:41 -07:00
var msg = new SsdpMessageBuilder().BuildRendererDiscoveryMessage();
var request = Encoding.UTF8.GetBytes(msg);
2014-02-26 19:44:00 -07:00
while (true)
{
socket.SendTo(request, new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900));
2014-03-23 14:46:13 -07:00
var delay = _config.Configuration.DlnaOptions.ClientDiscoveryIntervalSeconds * 1000;
2014-03-23 13:49:05 -07:00
await Task.Delay(delay).ConfigureAwait(false);
2014-02-26 19:44:00 -07:00
}
}
2014-03-23 13:07:02 -07:00
catch (OperationCanceledException)
2014-02-26 19:44:00 -07: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>
2014-04-25 10:30:41 -07:00
private Socket GetMulticastSocket(int networkInterfaceIndex)
2014-02-26 19:44:00 -07:00
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
2014-04-25 10:30:41 -07:00
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), networkInterfaceIndex));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 4);
2014-02-26 19:44:00 -07:00
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)
{
2014-03-14 10:09:22 -07:00
var device = await Device.CreateuPnpDeviceAsync(uri, _httpClient, _config, _logger).ConfigureAwait(false);
2014-02-26 19:44:00 -07:00
2014-04-26 20:42:05 -07:00
if (device != null && device.RendererCommands != null)
2014-02-26 19:44:00 -07:00
{
2014-03-24 22:25:03 -07:00
var sessionInfo = await _sessionManager.LogSessionActivity(device.Properties.ClientType, _appHost.ApplicationVersion.ToString(), device.Properties.UUID, device.Properties.Name, uri.OriginalString, null)
2014-02-26 19:44:00 -07:00
.ConfigureAwait(false);
var controller = sessionInfo.SessionController as PlayToController;
if (controller == null)
{
2014-04-26 20:42:05 -07:00
sessionInfo.SessionController = controller = new PlayToController(sessionInfo, _sessionManager, _itemRepository, _libraryManager, _logger, _networkManager, _dlnaManager, _userManager, _appHost, _dtoService, _imageProcessor, _ssdpHandler);
2014-03-14 10:09:22 -07:00
2014-03-24 22:25:03 -07:00
controller.Init(device);
2014-02-26 19:44:00 -07:00
2014-03-26 09:10:46 -07:00
var profile = _dlnaManager.GetProfile(device.Properties.ToDeviceIdentification()) ??
_dlnaManager.GetDefaultProfile();
2014-03-24 22:25:03 -07:00
_sessionManager.ReportCapabilities(sessionInfo.Id, new SessionCapabilities
{
2014-04-02 14:55:19 -07:00
PlayableMediaTypes = profile.GetSupportedMediaTypes(),
2014-03-25 14:13:55 -07:00
2014-04-02 14:55:19 -07:00
SupportedCommands = new List<string>
{
GeneralCommandType.VolumeDown.ToString(),
GeneralCommandType.VolumeUp.ToString(),
GeneralCommandType.Mute.ToString(),
GeneralCommandType.Unmute.ToString(),
2014-04-14 20:54:52 -07:00
GeneralCommandType.ToggleMute.ToString(),
GeneralCommandType.SetVolume.ToString()
2014-04-02 14:55:19 -07:00
}
2014-03-24 22:25:03 -07:00
});
2014-02-27 11:00:49 -07:00
2014-03-24 22:25:03 -07:00
_logger.Info("DLNA Session created for {0} - {1}", device.Properties.Name, device.Properties.ModelName);
}
2014-03-13 14:29:25 -07:00
}
2014-02-27 11:00:49 -07:00
}
2014-02-26 19:44:00 -07:00
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
_tokenSource.Cancel();
}
}
}
}