2014-06-29 17:35:05 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
using MediaBrowser.Dlna.Channels;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
using MediaBrowser.Dlna.PlayTo;
|
2014-04-25 17:30:41 +00:00
|
|
|
|
using MediaBrowser.Dlna.Ssdp;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
2014-04-25 17:30:41 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
using System.Net;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
namespace MediaBrowser.Dlna.Main
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
public class DlnaEntryPoint : IServerEntryPoint
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
private readonly INetworkManager _network;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
private PlayToManager _manager;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IItemRepository _itemRepo;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly IDlnaManager _dlnaManager;
|
|
|
|
|
private readonly IImageProcessor _imageProcessor;
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private SsdpHandler _ssdpHandler;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
private DeviceDiscovery _deviceDiscovery;
|
2014-04-18 05:03:01 +00:00
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
private readonly List<string> _registeredServerIds = new List<string>();
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private bool _dlnaServerStarted;
|
2014-04-18 05:03:01 +00:00
|
|
|
|
|
2014-06-14 18:24:20 +00:00
|
|
|
|
public DlnaEntryPoint(IServerConfigurationManager config, ILogManager logManager, IServerApplicationHost appHost, INetworkManager network, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepo, ILibraryManager libraryManager, IUserManager userManager, IDlnaManager dlnaManager, IImageProcessor imageProcessor)
|
2014-04-25 17:30:41 +00:00
|
|
|
|
{
|
2014-03-24 12:47:39 +00:00
|
|
|
|
_config = config;
|
|
|
|
|
_appHost = appHost;
|
2014-04-10 15:06:54 +00:00
|
|
|
|
_network = network;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_itemRepo = itemRepo;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_dlnaManager = dlnaManager;
|
|
|
|
|
_imageProcessor = imageProcessor;
|
2014-04-25 17:30:41 +00:00
|
|
|
|
_logger = logManager.GetLogger("Dlna");
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
StartSsdpHandler();
|
|
|
|
|
ReloadComponents();
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-06-29 17:35:05 +00:00
|
|
|
|
_config.NamedConfigurationUpdated += _config_NamedConfigurationUpdated;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
DlnaChannelFactory.Instance.Start(_deviceDiscovery, () => _registeredServerIds);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-29 17:35:05 +00:00
|
|
|
|
void _config_NamedConfigurationUpdated(object sender, ConfigurationUpdateEventArgs e)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-06-29 17:35:05 +00:00
|
|
|
|
if (string.Equals(e.Key, "dlna", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
ReloadComponents();
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private void ReloadComponents()
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-27 03:42:05 +00:00
|
|
|
|
var isServerStarted = _dlnaServerStarted;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-06-29 17:35:05 +00:00
|
|
|
|
var options = _config.GetDlnaConfiguration();
|
|
|
|
|
|
|
|
|
|
if (options.EnableServer && !isServerStarted)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
StartDlnaServer();
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
2014-06-29 17:35:05 +00:00
|
|
|
|
else if (!options.EnableServer && isServerStarted)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
DisposeDlnaServer();
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
2014-04-27 03:42:05 +00:00
|
|
|
|
|
|
|
|
|
var isPlayToStarted = _manager != null;
|
|
|
|
|
|
2014-06-29 17:35:05 +00:00
|
|
|
|
if (options.EnablePlayTo && !isPlayToStarted)
|
2014-04-27 03:42:05 +00:00
|
|
|
|
{
|
|
|
|
|
StartPlayToManager();
|
|
|
|
|
}
|
2014-06-29 17:35:05 +00:00
|
|
|
|
else if (!options.EnablePlayTo && isPlayToStarted)
|
2014-04-27 03:42:05 +00:00
|
|
|
|
{
|
|
|
|
|
DisposePlayToManager();
|
|
|
|
|
}
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private void StartSsdpHandler()
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_ssdpHandler = new SsdpHandler(_logger, _config, GenerateServerSignature());
|
2014-03-24 12:47:39 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
_ssdpHandler.Start();
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
_deviceDiscovery = new DeviceDiscovery(_logger, _config, _httpClient, _ssdpHandler, _network);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
|
|
|
|
_deviceDiscovery.Start();
|
2014-04-25 17:30:41 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2014-03-24 12:47:39 +00:00
|
|
|
|
{
|
2014-07-18 19:07:28 +00:00
|
|
|
|
_logger.ErrorException("Error starting ssdp handlers", ex);
|
2014-04-25 17:30:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-10 15:06:54 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
private void DisposeSsdpHandler()
|
|
|
|
|
{
|
2014-07-18 19:07:28 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_deviceDiscovery.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error disposing device discovery", ex);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_ssdpHandler.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error disposing ssdp handler", ex);
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
public void StartDlnaServer()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
RegisterServerEndpoints();
|
|
|
|
|
|
|
|
|
|
_dlnaServerStarted = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error registering endpoint", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterServerEndpoints()
|
2014-04-10 15:06:54 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var address in _network.GetLocalIpAddresses())
|
|
|
|
|
{
|
|
|
|
|
var guid = address.GetMD5();
|
|
|
|
|
|
|
|
|
|
var descriptorURI = "/mediabrowser/dlna/" + guid.ToString("N") + "/description.xml";
|
|
|
|
|
|
|
|
|
|
var uri = new Uri(string.Format("http://{0}:{1}{2}", address, _config.Configuration.HttpServerPortNumber, descriptorURI));
|
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
var services = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"upnp:rootdevice",
|
|
|
|
|
"urn:schemas-upnp-org:device:MediaServer:1",
|
|
|
|
|
"urn:schemas-upnp-org:service:ContentDirectory:1",
|
|
|
|
|
"uuid:" + guid.ToString("N")
|
|
|
|
|
};
|
2014-05-01 03:24:55 +00:00
|
|
|
|
|
2014-04-25 17:30:41 +00:00
|
|
|
|
_ssdpHandler.RegisterNotification(guid, uri, IPAddress.Parse(address), services);
|
2014-04-18 05:03:01 +00:00
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
_registeredServerIds.Add(guid.ToString("N"));
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GenerateServerSignature()
|
|
|
|
|
{
|
|
|
|
|
var os = Environment.OSVersion;
|
|
|
|
|
var pstring = os.Platform.ToString();
|
|
|
|
|
switch (os.Platform)
|
|
|
|
|
{
|
|
|
|
|
case PlatformID.Win32NT:
|
|
|
|
|
case PlatformID.Win32S:
|
|
|
|
|
case PlatformID.Win32Windows:
|
|
|
|
|
pstring = "WIN";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String.Format(
|
|
|
|
|
"{0}{1}/{2}.{3} UPnP/1.0 DLNADOC/1.5 MediaBrowser/{4}",
|
|
|
|
|
pstring,
|
|
|
|
|
IntPtr.Size * 8,
|
|
|
|
|
os.Version.Major,
|
|
|
|
|
os.Version.Minor,
|
|
|
|
|
_appHost.ApplicationVersion
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
private readonly object _syncLock = new object();
|
|
|
|
|
private void StartPlayToManager()
|
|
|
|
|
{
|
|
|
|
|
lock (_syncLock)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_manager = new PlayToManager(_logger,
|
|
|
|
|
_sessionManager,
|
|
|
|
|
_itemRepo,
|
|
|
|
|
_libraryManager,
|
|
|
|
|
_userManager,
|
|
|
|
|
_dlnaManager,
|
|
|
|
|
_appHost,
|
|
|
|
|
_imageProcessor,
|
2014-07-22 01:29:06 +00:00
|
|
|
|
_deviceDiscovery,
|
|
|
|
|
_httpClient,
|
|
|
|
|
_config);
|
2014-04-27 03:42:05 +00:00
|
|
|
|
|
|
|
|
|
_manager.Start();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error starting PlayTo manager", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DisposePlayToManager()
|
|
|
|
|
{
|
|
|
|
|
lock (_syncLock)
|
|
|
|
|
{
|
|
|
|
|
if (_manager != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_manager.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error disposing PlayTo manager", ex);
|
|
|
|
|
}
|
|
|
|
|
_manager = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 12:47:39 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-04-25 17:30:41 +00:00
|
|
|
|
DisposeDlnaServer();
|
2014-04-27 03:42:05 +00:00
|
|
|
|
DisposePlayToManager();
|
2014-04-25 17:30:41 +00:00
|
|
|
|
DisposeSsdpHandler();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisposeDlnaServer()
|
|
|
|
|
{
|
|
|
|
|
foreach (var id in _registeredServerIds)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
_ssdpHandler.UnregisterNotification(new Guid(id));
|
2014-04-25 17:30:41 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error unregistering server", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_registeredServerIds.Clear();
|
|
|
|
|
|
|
|
|
|
_dlnaServerStarted = false;
|
2014-03-24 12:47:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|