|
|
|
@ -10,6 +10,7 @@ using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
|
using MediaBrowser.Model.Dlna;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
@ -18,7 +19,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
|
|
|
|
|
{
|
|
|
|
|
public abstract class BaseTunerHost
|
|
|
|
|
{
|
|
|
|
|
protected readonly IConfigurationManager Config;
|
|
|
|
|
protected readonly IServerConfigurationManager Config;
|
|
|
|
|
protected readonly ILogger Logger;
|
|
|
|
|
protected IJsonSerializer JsonSerializer;
|
|
|
|
|
protected readonly IMediaEncoder MediaEncoder;
|
|
|
|
@ -26,7 +27,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
|
|
|
|
|
private readonly ConcurrentDictionary<string, ChannelCache> _channelCache =
|
|
|
|
|
new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
protected BaseTunerHost(IConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder)
|
|
|
|
|
protected BaseTunerHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder)
|
|
|
|
|
{
|
|
|
|
|
Config = config;
|
|
|
|
|
Logger = logger;
|
|
|
|
@ -125,12 +126,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
|
|
|
|
|
|
|
|
|
|
foreach (var host in hostsWithChannel)
|
|
|
|
|
{
|
|
|
|
|
var resourcePool = GetLock(host.Url);
|
|
|
|
|
Logger.Debug("GetChannelStreamMediaSources - Waiting on tuner resource pool");
|
|
|
|
|
|
|
|
|
|
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
Logger.Debug("GetChannelStreamMediaSources - Unlocked resource pool");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Check to make sure the tuner is available
|
|
|
|
@ -156,93 +151,63 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Error opening tuner", ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
resourcePool.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new List<MediaSourceInfo>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected abstract Task<MediaSourceInfo> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken);
|
|
|
|
|
protected abstract Task<LiveStream> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken);
|
|
|
|
|
|
|
|
|
|
public async Task<Tuple<MediaSourceInfo, SemaphoreSlim>> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
|
|
|
|
|
public async Task<LiveStream> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (IsValidChannelId(channelId))
|
|
|
|
|
if (!IsValidChannelId(channelId))
|
|
|
|
|
{
|
|
|
|
|
var hosts = GetTunerHosts();
|
|
|
|
|
|
|
|
|
|
var hostsWithChannel = new List<TunerHostInfo>();
|
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var host in hosts)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(streamId))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
var hosts = GetTunerHosts();
|
|
|
|
|
|
|
|
|
|
if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
hostsWithChannel.Add(host);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Error getting channels", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (streamId.StartsWith(host.Id, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
hostsWithChannel = new List<TunerHostInfo> {host};
|
|
|
|
|
streamId = streamId.Substring(host.Id.Length);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var hostsWithChannel = new List<TunerHostInfo>();
|
|
|
|
|
|
|
|
|
|
foreach (var host in hostsWithChannel)
|
|
|
|
|
foreach (var host in hosts)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(streamId))
|
|
|
|
|
{
|
|
|
|
|
var resourcePool = GetLock(host.Url);
|
|
|
|
|
Logger.Debug("GetChannelStream - Waiting on tuner resource pool");
|
|
|
|
|
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
Logger.Debug("GetChannelStream - Unlocked resource pool");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
|
|
// If a streamId is specified then availibility has already been checked in GetChannelStreamMediaSources
|
|
|
|
|
if (string.IsNullOrWhiteSpace(streamId) && hostsWithChannel.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
if (!await IsAvailable(host, channelId, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Tuner is not currently available");
|
|
|
|
|
resourcePool.Release();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var stream = await GetChannelStream(host, channelId, streamId, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (EnableMediaProbing)
|
|
|
|
|
if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
await AddMediaInfo(stream, false, resourcePool, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
hostsWithChannel.Add(host);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Tuple<MediaSourceInfo, SemaphoreSlim>(stream, resourcePool);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error("Error opening tuner", ex);
|
|
|
|
|
|
|
|
|
|
resourcePool.Release();
|
|
|
|
|
Logger.Error("Error getting channels", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (streamId.StartsWith(host.Id, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
hostsWithChannel = new List<TunerHostInfo> { host };
|
|
|
|
|
streamId = streamId.Substring(host.Id.Length);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
|
|
|
|
|
foreach (var host in hostsWithChannel)
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new LiveTvConflictException();
|
|
|
|
@ -268,37 +233,23 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
|
|
|
|
|
|
|
|
|
|
protected abstract Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _semaphoreLocks
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks = new ConcurrentDictionary<string, SemaphoreSlim>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the lock.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url">The filename.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
private SemaphoreSlim GetLock(string url)
|
|
|
|
|
{
|
|
|
|
|
return _semaphoreLocks.GetOrAdd(url, key => new SemaphoreSlim(1, 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task AddMediaInfo(MediaSourceInfo mediaSource, bool isAudio, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
|
|
|
|
|
private async Task AddMediaInfo(LiveStream stream, bool isAudio, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
//await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await AddMediaInfoInternal(mediaSource, isAudio, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
//try
|
|
|
|
|
//{
|
|
|
|
|
// await AddMediaInfoInternal(mediaSource, isAudio, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Leave the resource locked. it will be released upstream
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
// Release the resource if there's some kind of failure.
|
|
|
|
|
resourcePool.Release();
|
|
|
|
|
// // Leave the resource locked. it will be released upstream
|
|
|
|
|
//}
|
|
|
|
|
//catch (Exception)
|
|
|
|
|
//{
|
|
|
|
|
// // Release the resource if there's some kind of failure.
|
|
|
|
|
// resourcePool.Release();
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
// throw;
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task AddMediaInfoInternal(MediaSourceInfo mediaSource, bool isAudio, CancellationToken cancellationToken)
|
|
|
|
|