2014-07-26 17:30:15 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-07-22 01:29:06 +00:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-07-18 00:39:07 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-07-26 17:30:15 +00:00
|
|
|
|
using MediaBrowser.Dlna.ContentDirectory;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
using MediaBrowser.Dlna.PlayTo;
|
|
|
|
|
using MediaBrowser.Dlna.Ssdp;
|
2014-07-22 01:29:06 +00:00
|
|
|
|
using MediaBrowser.Model.Channels;
|
2014-07-18 00:39:07 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-07-22 01:29:06 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
using System;
|
2014-07-18 00:39:07 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
using System.Linq;
|
2014-07-18 00:39:07 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Dlna.Channels
|
|
|
|
|
{
|
2014-07-18 19:07:28 +00:00
|
|
|
|
public class DlnaChannelFactory : IChannelFactory, IDisposable
|
2014-07-18 00:39:07 +00:00
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-07-26 17:30:15 +00:00
|
|
|
|
|
2014-07-18 19:07:28 +00:00
|
|
|
|
private DeviceDiscovery _deviceDiscovery;
|
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
private readonly SemaphoreSlim _syncLock = new SemaphoreSlim(1, 1);
|
|
|
|
|
private List<Device> _servers = new List<Device>();
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
|
|
|
|
public static DlnaChannelFactory Instance;
|
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
private Func<List<string>> _localServersLookup;
|
|
|
|
|
|
|
|
|
|
public DlnaChannelFactory(IServerConfigurationManager config, IHttpClient httpClient, ILogger logger)
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
_config = config;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_logger = logger;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
internal void Start(DeviceDiscovery deviceDiscovery, Func<List<string>> localServersLookup)
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
_localServersLookup = localServersLookup;
|
|
|
|
|
|
2014-07-18 19:07:28 +00:00
|
|
|
|
_deviceDiscovery = deviceDiscovery;
|
2014-07-30 03:31:35 +00:00
|
|
|
|
//deviceDiscovery.DeviceDiscovered += deviceDiscovery_DeviceDiscovered;
|
2014-07-22 01:29:06 +00:00
|
|
|
|
deviceDiscovery.DeviceLeft += deviceDiscovery_DeviceLeft;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
async void deviceDiscovery_DeviceDiscovered(object sender, SsdpMessageEventArgs e)
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
string usn;
|
|
|
|
|
if (!e.Headers.TryGetValue("USN", out usn)) usn = string.Empty;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
|
2014-07-22 01:29:06 +00: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;
|
|
|
|
|
|
|
|
|
|
if (!IsValid(nt, usn))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_localServersLookup != null)
|
|
|
|
|
{
|
|
|
|
|
if (_localServersLookup().Any(i => usn.IndexOf(i, StringComparison.OrdinalIgnoreCase) != -1))
|
|
|
|
|
{
|
|
|
|
|
// Don't add the local Dlna server to this
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GetExistingServers(usn).Any())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _syncLock.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (GetExistingServers(usn).Any())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 16:36:34 +00:00
|
|
|
|
var device = await Device.CreateuPnpDeviceAsync(new Uri(location), _httpClient, _config, _logger)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-07-22 01:29:06 +00:00
|
|
|
|
|
2014-07-22 16:36:34 +00:00
|
|
|
|
if (!_servers.Any(i => string.Equals(i.Properties.UUID, device.Properties.UUID, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
_servers.Add(device);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_syncLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void deviceDiscovery_DeviceLeft(object sender, SsdpMessageEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
string usn;
|
|
|
|
|
if (!e.Headers.TryGetValue("USN", out usn)) usn = String.Empty;
|
|
|
|
|
|
|
|
|
|
string nt;
|
|
|
|
|
if (!e.Headers.TryGetValue("NT", out nt)) nt = String.Empty;
|
|
|
|
|
|
|
|
|
|
if (!IsValid(nt, usn))
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
if (!GetExistingServers(usn).Any())
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
await _syncLock.WaitAsync().ConfigureAwait(false);
|
2014-07-26 17:30:15 +00:00
|
|
|
|
|
2014-07-22 01:29:06 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var list = _servers.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var device in GetExistingServers(usn).ToList())
|
|
|
|
|
{
|
|
|
|
|
list.Remove(device);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_servers = list;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_syncLock.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsValid(string nt, string usn)
|
|
|
|
|
{
|
|
|
|
|
// It has to report that it's a media renderer
|
|
|
|
|
if (usn.IndexOf("ContentDirectory:", StringComparison.OrdinalIgnoreCase) == -1 &&
|
|
|
|
|
nt.IndexOf("ContentDirectory:", StringComparison.OrdinalIgnoreCase) == -1 &&
|
|
|
|
|
usn.IndexOf("MediaServer:", StringComparison.OrdinalIgnoreCase) == -1 &&
|
|
|
|
|
nt.IndexOf("MediaServer:", StringComparison.OrdinalIgnoreCase) == -1)
|
2014-07-18 19:07:28 +00:00
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
return false;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
2014-07-22 01:29:06 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<Device> GetExistingServers(string usn)
|
|
|
|
|
{
|
|
|
|
|
return _servers
|
|
|
|
|
.Where(i => usn.IndexOf(i.Properties.UUID, StringComparison.OrdinalIgnoreCase) != -1);
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 00:39:07 +00:00
|
|
|
|
public IEnumerable<IChannel> GetChannels()
|
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
//if (_servers.Count > 0)
|
|
|
|
|
//{
|
|
|
|
|
// var service = _servers[0].Properties.Services
|
|
|
|
|
// .FirstOrDefault(i => string.Equals(i.ServiceType, "urn:schemas-upnp-org:service:ContentDirectory:1", StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
// var controlUrl = service == null ? null : (_servers[0].Properties.BaseUrl.TrimEnd('/') + "/" + service.ControlUrl.TrimStart('/'));
|
|
|
|
|
|
|
|
|
|
// if (!string.IsNullOrEmpty(controlUrl))
|
|
|
|
|
// {
|
|
|
|
|
// return new List<IChannel>
|
|
|
|
|
// {
|
|
|
|
|
// new ServerChannel(_servers.ToList(), _httpClient, _logger, controlUrl)
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
return new List<IChannel>();
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
2014-07-18 00:39:07 +00:00
|
|
|
|
|
2014-07-18 19:07:28 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_deviceDiscovery != null)
|
2014-07-18 00:39:07 +00:00
|
|
|
|
{
|
2014-07-18 19:07:28 +00:00
|
|
|
|
_deviceDiscovery.DeviceDiscovered -= deviceDiscovery_DeviceDiscovered;
|
2014-07-22 01:29:06 +00:00
|
|
|
|
_deviceDiscovery.DeviceLeft -= deviceDiscovery_DeviceLeft;
|
2014-07-18 19:07:28 +00:00
|
|
|
|
}
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 19:07:28 +00:00
|
|
|
|
public class ServerChannel : IChannel, IFactoryChannel
|
2014-07-18 00:39:07 +00:00
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly ILogger _logger;
|
2014-07-30 03:31:35 +00:00
|
|
|
|
public string ControlUrl { get; set; }
|
|
|
|
|
public List<Device> Servers { get; set; }
|
2014-07-18 00:39:07 +00:00
|
|
|
|
|
2014-07-30 03:31:35 +00:00
|
|
|
|
public ServerChannel(IHttpClient httpClient, ILogger logger)
|
2014-07-27 22:01:29 +00:00
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_logger = logger;
|
2014-07-30 03:31:35 +00:00
|
|
|
|
Servers = new List<Device>();
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
get { return "Devices"; }
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Description
|
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
get { return string.Empty; }
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string DataVersion
|
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
get { return DateTime.UtcNow.Ticks.ToString(); }
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string HomePageUrl
|
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
get { return string.Empty; }
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ChannelParentalRating ParentalRating
|
|
|
|
|
{
|
|
|
|
|
get { return ChannelParentalRating.GeneralAudience; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InternalChannelFeatures GetChannelFeatures()
|
|
|
|
|
{
|
|
|
|
|
return new InternalChannelFeatures
|
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
ContentTypes = new List<ChannelMediaContentType>
|
|
|
|
|
{
|
|
|
|
|
ChannelMediaContentType.Song,
|
|
|
|
|
ChannelMediaContentType.Clip
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
MediaTypes = new List<ChannelMediaType>
|
|
|
|
|
{
|
|
|
|
|
ChannelMediaType.Audio,
|
|
|
|
|
ChannelMediaType.Video,
|
|
|
|
|
ChannelMediaType.Photo
|
|
|
|
|
}
|
2014-07-18 00:39:07 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsEnabledFor(string userId)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-26 17:30:15 +00:00
|
|
|
|
public async Task<ChannelItemResult> GetChannelItems(InternalChannelItemQuery query, CancellationToken cancellationToken)
|
2014-07-18 00:39:07 +00:00
|
|
|
|
{
|
2014-07-26 17:30:15 +00:00
|
|
|
|
IEnumerable<ChannelItemInfo> items;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(query.FolderId))
|
|
|
|
|
{
|
2014-07-30 03:31:35 +00:00
|
|
|
|
items = Servers.Select(i => new ChannelItemInfo
|
2014-07-26 17:30:15 +00:00
|
|
|
|
{
|
|
|
|
|
FolderType = ChannelFolderType.Container,
|
|
|
|
|
Id = GetServerId(i),
|
|
|
|
|
Name = i.Properties.Name,
|
|
|
|
|
Overview = i.Properties.ModelDescription,
|
|
|
|
|
Type = ChannelItemType.Folder
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var idParts = query.FolderId.Split('|');
|
|
|
|
|
var folderId = idParts.Length == 2 ? idParts[1] : null;
|
|
|
|
|
|
|
|
|
|
var result = await new ContentDirectoryBrowser(_httpClient, _logger).Browse(new ContentDirectoryBrowseRequest
|
|
|
|
|
{
|
|
|
|
|
Limit = query.Limit,
|
|
|
|
|
StartIndex = query.StartIndex,
|
|
|
|
|
ParentId = folderId,
|
2014-07-30 03:31:35 +00:00
|
|
|
|
ContentDirectoryUrl = ControlUrl
|
2014-07-26 17:30:15 +00:00
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
items = result.Items.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var list = items.ToList();
|
|
|
|
|
var count = list.Count;
|
|
|
|
|
|
|
|
|
|
list = ApplyPaging(list, query).ToList();
|
|
|
|
|
|
|
|
|
|
return new ChannelItemResult
|
|
|
|
|
{
|
|
|
|
|
Items = list,
|
|
|
|
|
TotalRecordCount = count
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetServerId(Device device)
|
|
|
|
|
{
|
|
|
|
|
return device.Properties.UUID.GetMD5().ToString("N");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<T> ApplyPaging<T>(IEnumerable<T> items, InternalChannelItemQuery query)
|
|
|
|
|
{
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Skip(query.StartIndex.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Take(query.Limit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<DynamicImageResponse> GetChannelImage(ImageType type, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
// TODO: Implement
|
|
|
|
|
return Task.FromResult(new DynamicImageResponse
|
|
|
|
|
{
|
|
|
|
|
HasImage = false
|
|
|
|
|
});
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ImageType> GetSupportedChannelImages()
|
|
|
|
|
{
|
2014-07-22 01:29:06 +00:00
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary
|
|
|
|
|
};
|
2014-07-18 00:39:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|