jellyfin/MediaBrowser.Dlna/PlayTo/PlayToManager.cs

151 lines
5.8 KiB
C#
Raw Normal View History

2014-07-21 18:29:06 -07:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller;
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-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;
namespace MediaBrowser.Dlna.PlayTo
{
class PlayToManager : IDisposable
{
private readonly ILogger _logger;
private readonly ISessionManager _sessionManager;
private readonly IItemRepository _itemRepository;
private readonly ILibraryManager _libraryManager;
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-16 21:25:11 -07:00
private readonly IServerApplicationHost _appHost;
2014-04-23 09:29:21 -07:00
private readonly IImageProcessor _imageProcessor;
2014-07-21 18:29:06 -07:00
private readonly IHttpClient _httpClient;
private readonly IServerConfigurationManager _config;
2014-02-27 11:00:49 -07:00
private readonly DeviceDiscovery _deviceDiscovery;
2014-04-26 20:42:05 -07:00
2014-07-21 18:29:06 -07:00
public PlayToManager(ILogger logger, ISessionManager sessionManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost, IImageProcessor imageProcessor, DeviceDiscovery deviceDiscovery, IHttpClient httpClient, IServerConfigurationManager config)
2014-02-26 19:44:00 -07:00
{
_logger = logger;
_sessionManager = sessionManager;
_itemRepository = itemRepository;
_libraryManager = libraryManager;
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-23 09:29:21 -07:00
_imageProcessor = imageProcessor;
_deviceDiscovery = deviceDiscovery;
2014-07-21 18:29:06 -07:00
_httpClient = httpClient;
_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
{
_deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered;
2014-04-07 21:17:18 -07:00
}
2014-07-21 18:29:06 -07:00
async void _deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e)
2014-02-26 19:44:00 -07:00
{
2014-07-21 18:29:06 -07:00
var localIp = e.LocalIp;
2014-02-26 19:44:00 -07:00
2014-07-21 18:29:06 -07:00
string usn;
if (!e.Headers.TryGetValue("USN", out usn)) usn = string.Empty;
2014-04-26 20:42:05 -07:00
2014-07-21 18:29:06 -07:00
string nt;
if (!e.Headers.TryGetValue("NT", out nt)) nt = string.Empty;
string location;
if (!e.Headers.TryGetValue("Location", out location)) location = string.Empty;
2014-04-26 20:42:05 -07:00
// It has to report that it's a media renderer
if (usn.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) == -1 &&
nt.IndexOf("MediaRenderer:", StringComparison.OrdinalIgnoreCase) == -1)
{
return;
}
if (_sessionManager.Sessions.Any(i => usn.IndexOf(i.DeviceId, StringComparison.OrdinalIgnoreCase) != -1))
2014-03-24 22:25:03 -07:00
{
return;
2014-07-21 18:29:06 -07:00
}
var uri = new Uri(location);
var device = await Device.CreateuPnpDeviceAsync(uri, _httpClient, _config, _logger).ConfigureAwait(false);
if (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-30 08:07:02 -07:00
var serverAddress = GetServerAddress(localIp);
sessionInfo.SessionController = controller = new PlayToController(sessionInfo,
_sessionManager,
_itemRepository,
_libraryManager,
_logger,
_dlnaManager,
_userManager,
_imageProcessor,
2014-07-21 18:29:06 -07:00
serverAddress,
_deviceDiscovery);
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(),
GeneralCommandType.SetAudioStreamIndex.ToString(),
GeneralCommandType.SetSubtitleStreamIndex.ToString()
2014-05-17 11:37:40 -07:00
},
SupportsMediaControl = true
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-04-30 08:07:02 -07:00
private string GetServerAddress(IPAddress localIp)
{
return string.Format("{0}://{1}:{2}/mediabrowser",
"http",
localIp,
_appHost.HttpServerPort
);
}
2014-02-26 19:44:00 -07:00
public void Dispose()
{
_deviceDiscovery.DeviceDiscovered -= _deviceDiscovery_DeviceDiscovered;
2014-02-26 19:44:00 -07:00
}
}
}