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

238 lines
8.8 KiB
C#
Raw Normal View History

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