jellyfin/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/BaseTunerHost.cs

198 lines
7.0 KiB
C#
Raw Normal View History

2015-08-19 17:58:41 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.LiveTv;
2015-08-19 19:25:18 +00:00
using MediaBrowser.Model.Dto;
2015-08-19 17:58:41 +00:00
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
{
public abstract class BaseTunerHost
{
protected readonly IConfigurationManager Config;
protected readonly ILogger Logger;
private readonly ConcurrentDictionary<string, ChannelCache> _channelCache =
new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase);
public BaseTunerHost(IConfigurationManager config, ILogger logger)
{
Config = config;
Logger = logger;
}
protected abstract Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken);
public abstract string Type { get; }
public async Task<IEnumerable<ChannelInfo>> GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken)
{
ChannelCache cache = null;
2015-08-20 02:22:47 +00:00
var key = tuner.Id;
2015-08-19 17:58:41 +00:00
2015-08-20 02:22:47 +00:00
if (enableCache && !string.IsNullOrWhiteSpace(key) && _channelCache.TryGetValue(key, out cache))
2015-08-19 17:58:41 +00:00
{
if ((DateTime.UtcNow - cache.Date) < TimeSpan.FromMinutes(60))
{
return cache.Channels.ToList();
}
}
var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);
2015-08-20 17:40:11 +00:00
var list = result.ToList();
2015-08-19 17:58:41 +00:00
2015-08-20 17:40:11 +00:00
if (!string.IsNullOrWhiteSpace(key))
{
cache = cache ?? new ChannelCache();
cache.Date = DateTime.UtcNow;
cache.Channels = list;
_channelCache.AddOrUpdate(key, cache, (k, v) => cache);
}
2015-08-19 17:58:41 +00:00
2015-08-20 17:40:11 +00:00
return list;
2015-08-19 17:58:41 +00:00
}
private List<TunerHostInfo> GetTunerHosts()
{
return GetConfiguration().TunerHosts
.Where(i => i.IsEnabled && string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
.ToList();
}
public async Task<IEnumerable<ChannelInfo>> GetChannels(CancellationToken cancellationToken)
{
var list = new List<ChannelInfo>();
var hosts = GetTunerHosts();
foreach (var host in hosts)
{
try
{
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
var newChannels = channels.Where(i => !list.Any(l => string.Equals(i.Id, l.Id, StringComparison.OrdinalIgnoreCase))).ToList();
list.AddRange(newChannels);
}
catch (Exception ex)
{
Logger.ErrorException("Error getting channel list", ex);
}
}
return list;
}
2015-08-19 19:25:18 +00:00
protected abstract Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancellationToken)
{
if (IsValidChannelId(channelId))
{
var hosts = GetTunerHosts();
var hostsWithChannel = new List<TunerHostInfo>();
foreach (var host in hosts)
{
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
{
hostsWithChannel.Add(host);
}
}
foreach (var host in hostsWithChannel)
{
2015-09-13 21:33:46 +00:00
try
2015-08-19 19:25:18 +00:00
{
2015-09-13 21:33:46 +00:00
var mediaSources = await GetChannelStreamMediaSources(host, channelId, cancellationToken).ConfigureAwait(false);
2015-08-19 19:25:18 +00:00
2015-09-13 21:33:46 +00:00
// Prefix the id with the host Id so that we can easily find it
foreach (var mediaSource in mediaSources)
{
mediaSource.Id = host.Id + mediaSource.Id;
}
2015-08-19 19:25:18 +00:00
2015-09-13 21:33:46 +00:00
return mediaSources;
}
catch (Exception ex)
2015-08-19 19:25:18 +00:00
{
2015-09-13 21:33:46 +00:00
Logger.Error("Error opening tuner", ex);
2015-08-19 19:25:18 +00:00
}
}
}
return new List<MediaSourceInfo>();
}
protected abstract Task<MediaSourceInfo> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken);
public async Task<MediaSourceInfo> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
{
if (IsValidChannelId(channelId))
{
var hosts = GetTunerHosts();
var hostsWithChannel = new List<TunerHostInfo>();
foreach (var host in hosts)
{
if (string.IsNullOrWhiteSpace(streamId))
{
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
{
hostsWithChannel.Add(host);
}
}
else if (streamId.StartsWith(host.Id, StringComparison.OrdinalIgnoreCase))
{
hostsWithChannel = new List<TunerHostInfo> { host };
streamId = streamId.Substring(host.Id.Length);
break;
}
}
foreach (var host in hostsWithChannel)
{
2015-09-13 21:33:46 +00:00
try
2015-08-19 19:25:18 +00:00
{
2015-09-13 21:33:46 +00:00
var stream = await GetChannelStream(host, channelId, streamId, cancellationToken).ConfigureAwait(false);
if (stream != null)
2015-08-19 19:25:18 +00:00
{
2015-09-13 21:33:46 +00:00
return stream;
2015-08-19 19:25:18 +00:00
}
}
2015-09-13 21:33:46 +00:00
catch (Exception ex)
2015-08-19 19:25:18 +00:00
{
2015-09-13 21:33:46 +00:00
Logger.Error("Error opening tuner", ex);
2015-08-19 19:25:18 +00:00
}
}
}
throw new LiveTvConflictException();
}
protected abstract bool IsValidChannelId(string channelId);
2015-08-19 17:58:41 +00:00
protected LiveTvOptions GetConfiguration()
{
return Config.GetConfiguration<LiveTvOptions>("livetv");
}
2015-08-19 19:25:18 +00:00
2015-08-19 17:58:41 +00:00
private class ChannelCache
{
public DateTime Date;
public List<ChannelInfo> Channels;
}
}
}