123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- #nullable disable
- #pragma warning disable CS1591
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.Json;
- using System.Threading;
- using System.Threading.Tasks;
- using MediaBrowser.Common.Configuration;
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Library;
- using MediaBrowser.Controller.LiveTv;
- using MediaBrowser.Model.Dto;
- using MediaBrowser.Model.IO;
- using MediaBrowser.Model.LiveTv;
- using Microsoft.Extensions.Logging;
- namespace Emby.Server.Implementations.LiveTv.TunerHosts
- {
- public abstract class BaseTunerHost
- {
- private readonly ConcurrentDictionary<string, List<ChannelInfo>> _cache;
- protected BaseTunerHost(IServerConfigurationManager config, ILogger<BaseTunerHost> logger, IFileSystem fileSystem)
- {
- Config = config;
- Logger = logger;
- FileSystem = fileSystem;
- _cache = new ConcurrentDictionary<string, List<ChannelInfo>>();
- }
- protected IServerConfigurationManager Config { get; }
- protected ILogger<BaseTunerHost> Logger { get; }
- protected IFileSystem FileSystem { get; }
- public virtual bool IsSupported => true;
- public abstract string Type { get; }
- protected virtual string ChannelIdPrefix => Type + "_";
- protected abstract Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken);
- public async Task<List<ChannelInfo>> GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken)
- {
- var key = tuner.Id;
- if (enableCache && !string.IsNullOrEmpty(key) && _cache.TryGetValue(key, out List<ChannelInfo> cache))
- {
- return cache;
- }
- var list = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);
- // logger.LogInformation("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list));
- if (!string.IsNullOrEmpty(key) && list.Count > 0)
- {
- _cache[key] = list;
- }
- return list;
- }
- protected virtual List<TunerHostInfo> GetTunerHosts()
- {
- return GetConfiguration().TunerHosts
- .Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
- .ToList();
- }
- public async Task<List<ChannelInfo>> GetChannels(bool enableCache, CancellationToken cancellationToken)
- {
- var list = new List<ChannelInfo>();
- var hosts = GetTunerHosts();
- foreach (var host in hosts)
- {
- var channelCacheFile = Path.Combine(Config.ApplicationPaths.CachePath, host.Id + "_channels");
- try
- {
- var channels = await GetChannels(host, enableCache, cancellationToken).ConfigureAwait(false);
- var newChannels = channels.Where(i => !list.Any(l => string.Equals(i.Id, l.Id, StringComparison.OrdinalIgnoreCase))).ToList();
- list.AddRange(newChannels);
- if (!enableCache)
- {
- try
- {
- Directory.CreateDirectory(Path.GetDirectoryName(channelCacheFile));
- await using var writeStream = AsyncFile.OpenWrite(channelCacheFile);
- await JsonSerializer.SerializeAsync(writeStream, channels, cancellationToken: cancellationToken).ConfigureAwait(false);
- }
- catch (IOException)
- {
- }
- }
- }
- catch (Exception ex)
- {
- Logger.LogError(ex, "Error getting channel list");
- if (enableCache)
- {
- try
- {
- await using var readStream = AsyncFile.OpenRead(channelCacheFile);
- var channels = await JsonSerializer.DeserializeAsync<List<ChannelInfo>>(readStream, cancellationToken: cancellationToken)
- .ConfigureAwait(false);
- list.AddRange(channels);
- }
- catch (IOException)
- {
- }
- }
- }
- }
- return list;
- }
- protected abstract Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, ChannelInfo channel, CancellationToken cancellationToken);
- public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancellationToken)
- {
- ArgumentException.ThrowIfNullOrEmpty(channelId);
- if (IsValidChannelId(channelId))
- {
- var hosts = GetTunerHosts();
- foreach (var host in hosts)
- {
- try
- {
- var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
- var channelInfo = channels.FirstOrDefault(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase));
- if (channelInfo is not null)
- {
- return await GetChannelStreamMediaSources(host, channelInfo, cancellationToken).ConfigureAwait(false);
- }
- }
- catch (Exception ex)
- {
- Logger.LogError(ex, "Error getting channels");
- }
- }
- }
- return new List<MediaSourceInfo>();
- }
- protected abstract Task<ILiveStream> GetChannelStream(TunerHostInfo tunerHost, ChannelInfo channel, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken);
- public async Task<ILiveStream> GetChannelStream(string channelId, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
- {
- ArgumentException.ThrowIfNullOrEmpty(channelId);
- if (!IsValidChannelId(channelId))
- {
- throw new FileNotFoundException();
- }
- var hosts = GetTunerHosts();
- var hostsWithChannel = new List<Tuple<TunerHostInfo, ChannelInfo>>();
- foreach (var host in hosts)
- {
- try
- {
- var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
- var channelInfo = channels.FirstOrDefault(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase));
- if (channelInfo is not null)
- {
- hostsWithChannel.Add(new Tuple<TunerHostInfo, ChannelInfo>(host, channelInfo));
- }
- }
- catch (Exception ex)
- {
- Logger.LogError(ex, "Error getting channels");
- }
- }
- foreach (var hostTuple in hostsWithChannel)
- {
- var host = hostTuple.Item1;
- var channelInfo = hostTuple.Item2;
- try
- {
- var liveStream = await GetChannelStream(host, channelInfo, streamId, currentLiveStreams, cancellationToken).ConfigureAwait(false);
- var startTime = DateTime.UtcNow;
- await liveStream.Open(cancellationToken).ConfigureAwait(false);
- var endTime = DateTime.UtcNow;
- Logger.LogInformation("Live stream opened after {0}ms", (endTime - startTime).TotalMilliseconds);
- return liveStream;
- }
- catch (Exception ex)
- {
- Logger.LogError(ex, "Error opening tuner");
- }
- }
- throw new LiveTvConflictException();
- }
- protected virtual bool IsValidChannelId(string channelId)
- {
- ArgumentException.ThrowIfNullOrEmpty(channelId);
- return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
- }
- protected LiveTvOptions GetConfiguration()
- {
- return Config.GetConfiguration<LiveTvOptions>("livetv");
- }
- }
- }
|