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

274 lines
9.8 KiB
C#
Raw Normal View History

2015-08-19 10:58:41 -07:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.LiveTv;
2015-08-19 12:25:18 -07:00
using MediaBrowser.Model.Dto;
2015-08-19 10:58:41 -07:00
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
2015-08-19 10:58:41 -07:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-09-25 11:39:13 -07:00
using MediaBrowser.Controller.Configuration;
2015-10-13 19:41:46 -07:00
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Model.Dlna;
2015-09-22 18:22:52 -07:00
using MediaBrowser.Model.Serialization;
2015-08-19 10:58:41 -07:00
2016-11-03 16:35:19 -07:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts
2015-08-19 10:58:41 -07:00
{
public abstract class BaseTunerHost
{
2016-09-25 11:39:13 -07:00
protected readonly IServerConfigurationManager Config;
2015-08-19 10:58:41 -07:00
protected readonly ILogger Logger;
2015-09-22 18:22:52 -07:00
protected IJsonSerializer JsonSerializer;
2015-10-13 19:41:46 -07:00
protected readonly IMediaEncoder MediaEncoder;
2015-08-19 10:58:41 -07:00
private readonly ConcurrentDictionary<string, ChannelCache> _channelCache =
new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase);
2016-09-25 11:39:13 -07:00
protected BaseTunerHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder)
2015-08-19 10:58:41 -07:00
{
Config = config;
Logger = logger;
2015-09-22 18:22:52 -07:00
JsonSerializer = jsonSerializer;
2015-10-13 19:41:46 -07:00
MediaEncoder = mediaEncoder;
2015-08-19 10:58:41 -07:00
}
2017-02-23 12:13:26 -07:00
protected abstract Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken);
2015-08-19 10:58:41 -07:00
public abstract string Type { get; }
2017-02-23 12:13:26 -07:00
public async Task<List<ChannelInfo>> GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken)
2015-08-19 10:58:41 -07:00
{
ChannelCache cache = null;
2015-08-19 19:22:47 -07:00
var key = tuner.Id;
2015-08-19 10:58:41 -07:00
2015-08-19 19:22:47 -07:00
if (enableCache && !string.IsNullOrWhiteSpace(key) && _channelCache.TryGetValue(key, out cache))
2015-08-19 10:58:41 -07:00
{
2016-03-27 14:11:27 -07:00
if (DateTime.UtcNow - cache.Date < TimeSpan.FromMinutes(60))
2015-08-19 10:58:41 -07:00
{
return cache.Channels.ToList();
}
}
var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);
2015-08-20 10:40:11 -07:00
var list = result.ToList();
2017-03-23 12:10:10 -07:00
Logger.Info("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list));
2015-08-19 10:58:41 -07:00
2015-10-01 09:28:24 -07:00
if (!string.IsNullOrWhiteSpace(key) && list.Count > 0)
2015-08-20 10:40:11 -07:00
{
cache = cache ?? new ChannelCache();
cache.Date = DateTime.UtcNow;
cache.Channels = list;
_channelCache.AddOrUpdate(key, cache, (k, v) => cache);
}
2015-08-19 10:58:41 -07:00
2015-08-20 10:40:11 -07:00
return list;
2015-08-19 10:58:41 -07:00
}
2016-02-18 23:20:18 -07:00
protected virtual List<TunerHostInfo> GetTunerHosts()
2015-08-19 10:58:41 -07:00
{
return GetConfiguration().TunerHosts
2017-03-12 21:56:41 -07:00
.Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
2015-08-19 10:58:41 -07:00
.ToList();
}
2017-02-23 12:13:26 -07:00
public async Task<List<ChannelInfo>> GetChannels(bool enableCache, CancellationToken cancellationToken)
2015-08-19 10:58:41 -07:00
{
var list = new List<ChannelInfo>();
var hosts = GetTunerHosts();
foreach (var host in hosts)
{
try
{
2016-09-29 05:55:49 -07:00
var channels = await GetChannels(host, enableCache, cancellationToken).ConfigureAwait(false);
2015-08-19 10:58:41 -07:00
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 12:25:18 -07:00
protected abstract Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancellationToken)
{
2016-11-16 20:58:27 -07:00
if (string.IsNullOrWhiteSpace(channelId))
{
throw new ArgumentNullException("channelId");
}
2015-08-19 12:25:18 -07:00
if (IsValidChannelId(channelId))
{
var hosts = GetTunerHosts();
var hostsWithChannel = new List<TunerHostInfo>();
foreach (var host in hosts)
{
2015-12-28 11:41:53 -07:00
try
{
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
2015-08-19 12:25:18 -07:00
2015-12-28 11:41:53 -07:00
if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
{
hostsWithChannel.Add(host);
}
}
catch (Exception ex)
2015-08-19 12:25:18 -07:00
{
2015-12-28 11:41:53 -07:00
Logger.Error("Error getting channels", ex);
2015-08-19 12:25:18 -07:00
}
}
foreach (var host in hostsWithChannel)
{
2015-09-13 14:33:46 -07:00
try
2015-08-19 12:25:18 -07:00
{
2015-10-30 09:40:12 -07:00
// Check to make sure the tuner is available
// If there's only one tuner, don't bother with the check and just let the tuner be the one to throw an error
2017-03-14 12:44:54 -07:00
if (hostsWithChannel.Count > 1 && !await IsAvailable(host, channelId, cancellationToken).ConfigureAwait(false))
2015-10-30 09:40:12 -07:00
{
Logger.Error("Tuner is not currently available");
continue;
}
2015-09-13 14:33:46 -07:00
var mediaSources = await GetChannelStreamMediaSources(host, channelId, cancellationToken).ConfigureAwait(false);
2015-08-19 12:25:18 -07:00
2015-09-13 14:33:46 -07: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 12:25:18 -07:00
2015-09-13 14:33:46 -07:00
return mediaSources;
}
catch (Exception ex)
2015-08-19 12:25:18 -07:00
{
2015-09-13 14:33:46 -07:00
Logger.Error("Error opening tuner", ex);
2015-08-19 12:25:18 -07:00
}
}
}
return new List<MediaSourceInfo>();
}
2016-09-25 11:39:13 -07:00
protected abstract Task<LiveStream> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken);
2015-08-19 12:25:18 -07:00
2016-09-25 11:39:13 -07:00
public async Task<LiveStream> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
2015-08-19 12:25:18 -07:00
{
2016-11-16 20:58:27 -07:00
if (string.IsNullOrWhiteSpace(channelId))
{
throw new ArgumentNullException("channelId");
}
2016-09-25 11:39:13 -07:00
if (!IsValidChannelId(channelId))
2015-08-19 12:25:18 -07:00
{
2016-09-25 11:39:13 -07:00
throw new FileNotFoundException();
}
2015-08-19 12:25:18 -07:00
2016-09-25 11:39:13 -07:00
var hosts = GetTunerHosts();
2015-08-19 12:25:18 -07:00
2016-09-25 11:39:13 -07:00
var hostsWithChannel = new List<TunerHostInfo>();
2015-08-19 12:25:18 -07:00
2016-09-25 11:39:13 -07:00
foreach (var host in hosts)
{
if (string.IsNullOrWhiteSpace(streamId))
2015-08-19 12:25:18 -07:00
{
2015-09-13 14:33:46 -07:00
try
2015-08-19 12:25:18 -07:00
{
2016-09-25 11:39:13 -07:00
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
2015-10-16 11:11:11 -07:00
2016-09-25 11:39:13 -07:00
if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
2016-04-21 11:00:46 -07:00
{
2016-09-25 11:39:13 -07:00
hostsWithChannel.Add(host);
2016-04-21 11:00:46 -07:00
}
2015-08-19 12:25:18 -07:00
}
2015-09-13 14:33:46 -07:00
catch (Exception ex)
2015-08-19 12:25:18 -07:00
{
2016-09-25 11:39:13 -07:00
Logger.Error("Error getting channels", ex);
2015-08-19 12:25:18 -07:00
}
}
2016-09-25 11:39:13 -07:00
else if (streamId.StartsWith(host.Id, StringComparison.OrdinalIgnoreCase))
{
hostsWithChannel = new List<TunerHostInfo> { host };
streamId = streamId.Substring(host.Id.Length);
break;
}
2015-08-19 12:25:18 -07:00
}
2016-09-25 11:39:13 -07:00
foreach (var host in hostsWithChannel)
{
2017-03-14 12:44:54 -07:00
if (!channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase))
{
continue;
}
2016-09-25 11:39:13 -07:00
try
{
var liveStream = await GetChannelStream(host, channelId, streamId, cancellationToken).ConfigureAwait(false);
await liveStream.Open(cancellationToken).ConfigureAwait(false);
return liveStream;
}
catch (Exception ex)
{
Logger.Error("Error opening tuner", ex);
}
}
2015-08-19 12:25:18 -07:00
throw new LiveTvConflictException();
}
2015-10-30 09:40:12 -07:00
protected async Task<bool> IsAvailable(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
{
try
{
return await IsAvailableInternal(tuner, channelId, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
Logger.ErrorException("Error checking tuner availability", ex);
return false;
}
}
protected abstract Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
2017-03-14 12:44:54 -07:00
protected virtual string ChannelIdPrefix
{
get
{
return Type + "_";
}
}
protected virtual bool IsValidChannelId(string channelId)
{
if (string.IsNullOrWhiteSpace(channelId))
{
throw new ArgumentNullException("channelId");
}
return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
}
2015-08-19 12:25:18 -07:00
2015-08-19 10:58:41 -07:00
protected LiveTvOptions GetConfiguration()
{
return Config.GetConfiguration<LiveTvOptions>("livetv");
}
2015-08-19 12:25:18 -07:00
2015-08-19 10:58:41 -07:00
private class ChannelCache
{
public DateTime Date;
public List<ChannelInfo> Channels;
}
}
}