2014-07-22 03:29:06 +02:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-03-13 20:08:02 +01:00
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
2014-04-23 18:29:21 +02:00
|
|
|
|
using MediaBrowser.Controller.Drawing;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-09-24 03:44:05 +02:00
|
|
|
|
using MediaBrowser.Controller.Localization;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
2014-03-25 06:25:03 +01:00
|
|
|
|
using MediaBrowser.Dlna.Ssdp;
|
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;
|
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;
|
2015-04-29 21:48:46 +02:00
|
|
|
|
using System.Threading;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.PlayTo
|
|
|
|
|
{
|
|
|
|
|
class PlayToManager : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
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-17 05:25:11 +01:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2014-04-23 18:29:21 +02:00
|
|
|
|
private readonly IImageProcessor _imageProcessor;
|
2014-07-22 03:29:06 +02:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-09-01 22:10:54 +02:00
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
2014-09-24 03:44:05 +02:00
|
|
|
|
private readonly ILocalizationManager _localization;
|
2014-02-27 19:00:49 +01:00
|
|
|
|
|
2015-07-23 18:32:34 +02:00
|
|
|
|
private readonly IDeviceDiscovery _deviceDiscovery;
|
2015-03-07 23:43:53 +01:00
|
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
|
|
|
|
|
2015-04-29 21:48:46 +02:00
|
|
|
|
private readonly List<string> _nonRendererUrls = new List<string>();
|
2016-01-29 20:20:09 +01:00
|
|
|
|
private DateTime _lastRendererClear;
|
2015-04-29 21:48:46 +02:00
|
|
|
|
|
2015-07-23 18:32:34 +02:00
|
|
|
|
public PlayToManager(ILogger logger, ISessionManager sessionManager, ILibraryManager libraryManager, IUserManager userManager, IDlnaManager dlnaManager, IServerApplicationHost appHost, IImageProcessor imageProcessor, IDeviceDiscovery deviceDiscovery, IHttpClient httpClient, IServerConfigurationManager config, IUserDataManager userDataManager, ILocalizationManager localization, IMediaSourceManager mediaSourceManager)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
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-04-23 18:29:21 +02:00
|
|
|
|
_imageProcessor = imageProcessor;
|
2014-07-18 21:07:28 +02:00
|
|
|
|
_deviceDiscovery = deviceDiscovery;
|
2014-07-22 03:29:06 +02:00
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_config = config;
|
2014-09-01 22:10:54 +02:00
|
|
|
|
_userDataManager = userDataManager;
|
2014-09-24 03:44:05 +02:00
|
|
|
|
_localization = localization;
|
2015-03-07 23:43:53 +01:00
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
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
|
|
|
|
{
|
2014-07-18 21:07:28 +02:00
|
|
|
|
_deviceDiscovery.DeviceDiscovered += _deviceDiscovery_DeviceDiscovered;
|
2014-04-08 06:17:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 03:29:06 +02:00
|
|
|
|
async void _deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
2014-07-22 03:29:06 +02:00
|
|
|
|
string usn;
|
|
|
|
|
if (!e.Headers.TryGetValue("USN", out usn)) usn = string.Empty;
|
2014-04-27 05:42:05 +02:00
|
|
|
|
|
2014-07-22 03:29:06 +02: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;
|
2015-04-29 21:48:46 +02:00
|
|
|
|
|
2014-04-27 05:42:05 +02: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-25 06:25:03 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
2014-07-22 03:29:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
try
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
2015-04-29 21:48:46 +02:00
|
|
|
|
lock (_nonRendererUrls)
|
|
|
|
|
{
|
2016-01-29 20:20:09 +01:00
|
|
|
|
if ((DateTime.UtcNow - _lastRendererClear).TotalMinutes >= 10)
|
|
|
|
|
{
|
|
|
|
|
_nonRendererUrls.Clear();
|
|
|
|
|
_lastRendererClear = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 21:48:46 +02:00
|
|
|
|
if (_nonRendererUrls.Contains(location, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 17:17:03 +01:00
|
|
|
|
var uri = new Uri(location);
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var device = await Device.CreateuPnpDeviceAsync(uri, _httpClient, _config, _logger).ConfigureAwait(false);
|
2014-02-27 03:44:00 +01:00
|
|
|
|
|
2015-04-29 21:48:46 +02:00
|
|
|
|
if (device.RendererCommands == null)
|
2014-02-27 03:44:00 +01:00
|
|
|
|
{
|
2015-04-29 21:48:46 +02:00
|
|
|
|
lock (_nonRendererUrls)
|
|
|
|
|
{
|
|
|
|
|
_nonRendererUrls.Add(location);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sessionInfo = await _sessionManager.LogSessionActivity(device.Properties.ClientType, _appHost.ApplicationVersion.ToString(), device.Properties.UUID, device.Properties.Name, uri.OriginalString, null)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-04-30 17:07:02 +02:00
|
|
|
|
|
2015-04-29 21:48:46 +02:00
|
|
|
|
var controller = sessionInfo.SessionController as PlayToController;
|
2014-03-14 18:09:22 +01:00
|
|
|
|
|
2015-04-29 21:48:46 +02:00
|
|
|
|
if (controller == null)
|
|
|
|
|
{
|
2015-05-12 04:36:46 +02:00
|
|
|
|
var serverAddress = GetServerAddress(e.LocalEndPoint.Address);
|
2015-04-29 21:48:46 +02:00
|
|
|
|
string accessToken = null;
|
|
|
|
|
|
|
|
|
|
sessionInfo.SessionController = controller = new PlayToController(sessionInfo,
|
|
|
|
|
_sessionManager,
|
|
|
|
|
_libraryManager,
|
|
|
|
|
_logger,
|
|
|
|
|
_dlnaManager,
|
|
|
|
|
_userManager,
|
|
|
|
|
_imageProcessor,
|
|
|
|
|
serverAddress,
|
|
|
|
|
accessToken,
|
|
|
|
|
_deviceDiscovery,
|
|
|
|
|
_userDataManager,
|
|
|
|
|
_localization,
|
2016-02-15 03:25:14 +01:00
|
|
|
|
_mediaSourceManager,
|
|
|
|
|
_config);
|
2015-04-29 21:48:46 +02:00
|
|
|
|
|
|
|
|
|
controller.Init(device);
|
|
|
|
|
|
|
|
|
|
var profile = _dlnaManager.GetProfile(device.Properties.ToDeviceIdentification()) ??
|
|
|
|
|
_dlnaManager.GetDefaultProfile();
|
|
|
|
|
|
|
|
|
|
_sessionManager.ReportCapabilities(sessionInfo.Id, new ClientCapabilities
|
2014-07-30 05:31:35 +02:00
|
|
|
|
{
|
2015-04-29 21:48:46 +02:00
|
|
|
|
PlayableMediaTypes = profile.GetSupportedMediaTypes(),
|
|
|
|
|
|
|
|
|
|
SupportedCommands = new List<string>
|
2014-07-30 05:31:35 +02:00
|
|
|
|
{
|
2015-04-29 21:48:46 +02:00
|
|
|
|
GeneralCommandType.VolumeDown.ToString(),
|
|
|
|
|
GeneralCommandType.VolumeUp.ToString(),
|
|
|
|
|
GeneralCommandType.Mute.ToString(),
|
|
|
|
|
GeneralCommandType.Unmute.ToString(),
|
|
|
|
|
GeneralCommandType.ToggleMute.ToString(),
|
|
|
|
|
GeneralCommandType.SetVolume.ToString(),
|
|
|
|
|
GeneralCommandType.SetAudioStreamIndex.ToString(),
|
|
|
|
|
GeneralCommandType.SetSubtitleStreamIndex.ToString()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
SupportsMediaControl = true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_logger.Info("DLNA Session created for {0} - {1}", device.Properties.Name, device.Properties.ModelName);
|
2014-03-25 06:25:03 +01:00
|
|
|
|
}
|
2014-03-13 22:29:25 +01:00
|
|
|
|
}
|
2014-07-30 05:31:35 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error creating PlayTo device.", ex);
|
|
|
|
|
}
|
2014-02-27 19:00:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 17:07:02 +02:00
|
|
|
|
private string GetServerAddress(IPAddress localIp)
|
|
|
|
|
{
|
2015-02-10 06:54:58 +01:00
|
|
|
|
return _appHost.GetLocalApiUrl(localIp.ToString());
|
2014-04-30 17:07:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-27 03:44:00 +01:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2014-07-18 21:07:28 +02:00
|
|
|
|
_deviceDiscovery.DeviceDiscovered -= _deviceDiscovery_DeviceDiscovered;
|
2014-02-27 03:44:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|