2020-02-06 14:20:23 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
#pragma warning disable SA1600
|
|
|
|
|
2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2015-08-19 17:58:41 +00:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
2016-09-14 16:21:33 +00:00
|
|
|
using System.IO;
|
2015-08-19 17:58:41 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 19:22:00 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2016-09-25 18:39:13 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2019-01-13 19:22:00 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
|
|
|
using MediaBrowser.Model.Dto;
|
2017-07-05 18:30:12 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2019-01-13 19:22:00 +00:00
|
|
|
using MediaBrowser.Model.LiveTv;
|
2015-09-23 01:22:52 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-01-13 19:22:00 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2015-08-19 17:58:41 +00:00
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts
|
2015-08-19 17:58:41 +00:00
|
|
|
{
|
|
|
|
public abstract class BaseTunerHost
|
|
|
|
{
|
2016-09-25 18:39:13 +00:00
|
|
|
protected readonly IServerConfigurationManager Config;
|
2015-08-19 17:58:41 +00:00
|
|
|
protected readonly ILogger Logger;
|
2015-09-23 01:22:52 +00:00
|
|
|
protected IJsonSerializer JsonSerializer;
|
2017-07-05 18:30:12 +00:00
|
|
|
protected readonly IFileSystem FileSystem;
|
2015-08-19 17:58:41 +00:00
|
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, ChannelCache> _channelCache =
|
|
|
|
new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
2019-02-06 19:38:42 +00:00
|
|
|
protected BaseTunerHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IFileSystem fileSystem)
|
2015-08-19 17:58:41 +00:00
|
|
|
{
|
|
|
|
Config = config;
|
|
|
|
Logger = logger;
|
2015-09-23 01:22:52 +00:00
|
|
|
JsonSerializer = jsonSerializer;
|
2017-07-05 18:30:12 +00:00
|
|
|
FileSystem = fileSystem;
|
2015-08-19 17:58:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
public virtual bool IsSupported => true;
|
2017-12-03 22:12:46 +00:00
|
|
|
|
2017-02-23 19:13:26 +00:00
|
|
|
protected abstract Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken);
|
2015-08-19 17:58:41 +00:00
|
|
|
public abstract string Type { get; }
|
|
|
|
|
2017-02-23 19:13:26 +00:00
|
|
|
public async Task<List<ChannelInfo>> GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken)
|
2015-08-19 17:58:41 +00:00
|
|
|
{
|
|
|
|
ChannelCache cache = null;
|
2015-08-20 02:22:47 +00:00
|
|
|
var key = tuner.Id;
|
2015-08-19 17:58:41 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (enableCache && !string.IsNullOrEmpty(key) && _channelCache.TryGetValue(key, out cache))
|
2015-08-19 17:58:41 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return cache.Channels.ToList();
|
2015-08-19 17:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);
|
2015-08-20 17:40:11 +00:00
|
|
|
var list = result.ToList();
|
2018-12-13 13:18:25 +00:00
|
|
|
//logger.LogInformation("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list));
|
2015-08-19 17:58:41 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!string.IsNullOrEmpty(key) && list.Count > 0)
|
2015-08-20 17:40:11 +00:00
|
|
|
{
|
|
|
|
cache = cache ?? new ChannelCache();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-19 06:20:18 +00:00
|
|
|
protected virtual List<TunerHostInfo> GetTunerHosts()
|
2015-08-19 17:58:41 +00:00
|
|
|
{
|
|
|
|
return GetConfiguration().TunerHosts
|
2017-03-13 04:56:41 +00:00
|
|
|
.Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
|
2015-08-19 17:58:41 +00:00
|
|
|
.ToList();
|
|
|
|
}
|
|
|
|
|
2017-02-23 19:13:26 +00:00
|
|
|
public async Task<List<ChannelInfo>> GetChannels(bool enableCache, CancellationToken cancellationToken)
|
2015-08-19 17:58:41 +00:00
|
|
|
{
|
|
|
|
var list = new List<ChannelInfo>();
|
|
|
|
|
|
|
|
var hosts = GetTunerHosts();
|
|
|
|
|
|
|
|
foreach (var host in hosts)
|
|
|
|
{
|
2017-07-05 18:30:12 +00:00
|
|
|
var channelCacheFile = Path.Combine(Config.ApplicationPaths.CachePath, host.Id + "_channels");
|
|
|
|
|
2015-08-19 17:58:41 +00:00
|
|
|
try
|
|
|
|
{
|
2016-09-29 12:55:49 +00:00
|
|
|
var channels = await GetChannels(host, enableCache, cancellationToken).ConfigureAwait(false);
|
2015-08-19 17:58:41 +00:00
|
|
|
var newChannels = channels.Where(i => !list.Any(l => string.Equals(i.Id, l.Id, StringComparison.OrdinalIgnoreCase))).ToList();
|
|
|
|
|
|
|
|
list.AddRange(newChannels);
|
2017-07-05 18:30:12 +00:00
|
|
|
|
|
|
|
if (!enableCache)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(channelCacheFile));
|
2017-07-05 18:30:12 +00:00
|
|
|
JsonSerializer.SerializeToFile(channels, channelCacheFile);
|
|
|
|
}
|
|
|
|
catch (IOException)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 17:58:41 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
Logger.LogError(ex, "Error getting channel list");
|
2017-07-05 18:30:12 +00:00
|
|
|
|
|
|
|
if (enableCache)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var channels = JsonSerializer.DeserializeFromFile<List<ChannelInfo>>(channelCacheFile);
|
|
|
|
list.AddRange(channels);
|
|
|
|
}
|
|
|
|
catch (IOException)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 17:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
protected abstract Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, ChannelInfo channel, CancellationToken cancellationToken);
|
2015-08-19 19:25:18 +00:00
|
|
|
|
|
|
|
public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancellationToken)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (string.IsNullOrEmpty(channelId))
|
2016-11-17 03:58:27 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(channelId));
|
2016-11-17 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 19:25:18 +00:00
|
|
|
if (IsValidChannelId(channelId))
|
|
|
|
{
|
|
|
|
var hosts = GetTunerHosts();
|
|
|
|
|
|
|
|
foreach (var host in hosts)
|
|
|
|
{
|
2015-12-28 18:41:53 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
var channelInfo = channels.FirstOrDefault(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase));
|
2015-08-19 19:25:18 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (channelInfo != null)
|
2015-12-28 18:41:53 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return await GetChannelStreamMediaSources(host, channelInfo, cancellationToken).ConfigureAwait(false);
|
2015-12-28 18:41:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2015-08-19 19:25:18 +00:00
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
Logger.LogError(ex, "Error getting channels");
|
2015-08-19 19:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new List<MediaSourceInfo>();
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
protected abstract Task<ILiveStream> GetChannelStream(TunerHostInfo tuner, ChannelInfo channel, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken);
|
2015-08-19 19:25:18 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task<ILiveStream> GetChannelStream(string channelId, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
|
2015-08-19 19:25:18 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (string.IsNullOrEmpty(channelId))
|
2016-11-17 03:58:27 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(channelId));
|
2016-11-17 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 18:39:13 +00:00
|
|
|
if (!IsValidChannelId(channelId))
|
2015-08-19 19:25:18 +00:00
|
|
|
{
|
2016-09-25 18:39:13 +00:00
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
2015-08-19 19:25:18 +00:00
|
|
|
|
2016-09-25 18:39:13 +00:00
|
|
|
var hosts = GetTunerHosts();
|
2015-08-19 19:25:18 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
var hostsWithChannel = new List<Tuple<TunerHostInfo, ChannelInfo>>();
|
2015-08-19 19:25:18 +00:00
|
|
|
|
2016-09-25 18:39:13 +00:00
|
|
|
foreach (var host in hosts)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
try
|
2015-08-19 19:25:18 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
|
|
|
|
var channelInfo = channels.FirstOrDefault(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase));
|
2015-10-16 18:11:11 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (channelInfo != null)
|
2015-08-19 19:25:18 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
hostsWithChannel.Add(new Tuple<TunerHostInfo, ChannelInfo>(host, channelInfo));
|
2015-08-19 19:25:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:26:21 +00:00
|
|
|
catch (Exception ex)
|
2016-09-25 18:39:13 +00:00
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
Logger.LogError(ex, "Error getting channels");
|
2016-09-25 18:39:13 +00:00
|
|
|
}
|
2015-08-19 19:25:18 +00:00
|
|
|
}
|
2016-09-25 18:39:13 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
foreach (var hostTuple in hostsWithChannel)
|
2016-09-14 16:21:33 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var host = hostTuple.Item1;
|
|
|
|
var channelInfo = hostTuple.Item2;
|
2017-03-14 19:44:54 +00:00
|
|
|
|
2016-09-25 18:39:13 +00:00
|
|
|
try
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var liveStream = await GetChannelStream(host, channelInfo, streamId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
|
2017-09-27 14:52:01 +00:00
|
|
|
var startTime = DateTime.UtcNow;
|
2016-09-25 18:39:13 +00:00
|
|
|
await liveStream.Open(cancellationToken).ConfigureAwait(false);
|
2017-09-27 14:52:01 +00:00
|
|
|
var endTime = DateTime.UtcNow;
|
2018-12-13 13:18:25 +00:00
|
|
|
Logger.LogInformation("Live stream opened after {0}ms", (endTime - startTime).TotalMilliseconds);
|
2016-09-25 18:39:13 +00:00
|
|
|
return liveStream;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
Logger.LogError(ex, "Error opening tuner");
|
2016-09-25 18:39:13 +00:00
|
|
|
}
|
2016-09-14 16:21:33 +00:00
|
|
|
}
|
2015-08-19 19:25:18 +00:00
|
|
|
|
|
|
|
throw new LiveTvConflictException();
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
protected virtual string ChannelIdPrefix => Type + "_";
|
|
|
|
|
2017-03-14 19:44:54 +00:00
|
|
|
protected virtual bool IsValidChannelId(string channelId)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (string.IsNullOrEmpty(channelId))
|
2017-03-14 19:44:54 +00:00
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(channelId));
|
2017-03-14 19:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
2015-08-19 19:25:18 +00:00
|
|
|
|
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 List<ChannelInfo> Channels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|