add live tv logging

This commit is contained in:
Luke Pulverenti 2015-09-22 21:22:52 -04:00
parent d469e3623e
commit 1ae288b0b3
3 changed files with 60 additions and 44 deletions

View file

@ -9,6 +9,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
{ {
@ -16,14 +17,16 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
{ {
protected readonly IConfigurationManager Config; protected readonly IConfigurationManager Config;
protected readonly ILogger Logger; protected readonly ILogger Logger;
protected IJsonSerializer JsonSerializer;
private readonly ConcurrentDictionary<string, ChannelCache> _channelCache = private readonly ConcurrentDictionary<string, ChannelCache> _channelCache =
new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase); new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase);
public BaseTunerHost(IConfigurationManager config, ILogger logger) public BaseTunerHost(IConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer)
{ {
Config = config; Config = config;
Logger = logger; Logger = logger;
JsonSerializer = jsonSerializer;
} }
protected abstract Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken); protected abstract Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken);
@ -44,6 +47,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false); var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);
var list = result.ToList(); var list = result.ToList();
Logger.Debug("Channels from {0}: {1}", tuner.Url, JsonSerializer.SerializeToString(list));
if (!string.IsNullOrWhiteSpace(key)) if (!string.IsNullOrWhiteSpace(key))
{ {

View file

@ -20,13 +20,11 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.HdHomerun
public class HdHomerunHost : BaseTunerHost, ITunerHost public class HdHomerunHost : BaseTunerHost, ITunerHost
{ {
private readonly IHttpClient _httpClient; private readonly IHttpClient _httpClient;
private readonly IJsonSerializer _jsonSerializer;
public HdHomerunHost(IConfigurationManager config, ILogger logger, IHttpClient httpClient, IJsonSerializer jsonSerializer) public HdHomerunHost(IConfigurationManager config, ILogger logger, IHttpClient httpClient, IJsonSerializer jsonSerializer)
: base(config, logger) : base(config, logger, jsonSerializer)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_jsonSerializer = jsonSerializer;
} }
public string Name public string Name
@ -55,7 +53,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.HdHomerun
}; };
using (var stream = await _httpClient.Get(options)) using (var stream = await _httpClient.Get(options))
{ {
var root = _jsonSerializer.DeserializeFromStream<List<Channels>>(stream); var root = JsonSerializer.DeserializeFromStream<List<Channels>>(stream);
if (root != null) if (root != null)
{ {
@ -380,7 +378,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.HdHomerun
protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken) protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
{ {
Logger.Debug("GetChannelStream: channel id: {0}. stream id: {1}", channelId, streamId ?? string.Empty); Logger.Info("GetChannelStream: channel id: {0}. stream id: {1}", channelId, streamId ?? string.Empty);
if (!channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase)) if (!channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase))
{ {

View file

@ -13,17 +13,21 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Common.IO; using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
{ {
public class M3UTunerHost : BaseTunerHost, ITunerHost public class M3UTunerHost : BaseTunerHost, ITunerHost
{ {
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private IHttpClient _httpClient;
public M3UTunerHost(IConfigurationManager config, ILogger logger, IFileSystem fileSystem) public M3UTunerHost(IConfigurationManager config, ILogger logger, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer jsonSerializer)
: base(config, logger) : base(config, logger, jsonSerializer)
{ {
_fileSystem = fileSystem; _fileSystem = fileSystem;
_httpClient = httpClient;
} }
public override string Type public override string Type
@ -45,47 +49,48 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
string line; string line;
// Read the file and display it line by line. // Read the file and display it line by line.
var file = new StreamReader(url); using (var file = new StreamReader(await GetListingsStream(info, cancellationToken).ConfigureAwait(false)))
var channels = new List<M3UChannel>();
string channnelName = null;
string channelNumber = null;
while ((line = file.ReadLine()) != null)
{ {
line = line.Trim(); var channels = new List<M3UChannel>();
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase)) string channnelName = null;
{ string channelNumber = null;
continue;
}
if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase)) while ((line = file.ReadLine()) != null)
{ {
var parts = line.Split(new[] { ':' }, 2).Last().Split(new[] { ',' }, 2); line = line.Trim();
channelNumber = parts[0]; if (string.IsNullOrWhiteSpace(line))
channnelName = parts[1];
}
else if (!string.IsNullOrWhiteSpace(channelNumber))
{
channels.Add(new M3UChannel
{ {
Name = channnelName, continue;
Number = channelNumber, }
Id = ChannelIdPrefix + urlHash + channelNumber,
Path = line
});
channelNumber = null; if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase))
channnelName = null; {
continue;
}
if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase))
{
var parts = line.Split(new[] { ':' }, 2).Last().Split(new[] { ',' }, 2);
channelNumber = parts[0];
channnelName = parts[1];
}
else if (!string.IsNullOrWhiteSpace(channelNumber))
{
channels.Add(new M3UChannel
{
Name = channnelName,
Number = channelNumber,
Id = ChannelIdPrefix + urlHash + channelNumber,
Path = line
});
channelNumber = null;
channnelName = null;
}
} }
return channels;
} }
file.Close();
return channels;
} }
public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken) public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
@ -123,9 +128,9 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
public async Task Validate(TunerHostInfo info) public async Task Validate(TunerHostInfo info)
{ {
if (!_fileSystem.FileExists(info.Url)) using (var stream = await GetListingsStream(info, CancellationToken.None).ConfigureAwait(false))
{ {
throw new FileNotFoundException();
} }
} }
@ -134,6 +139,15 @@ namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase); return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
} }
private Task<Stream> GetListingsStream(TunerHostInfo info, CancellationToken cancellationToken)
{
if (info.Url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
return _httpClient.Get(info.Url, cancellationToken);
}
return Task.FromResult(_fileSystem.OpenRead(info.Url));
}
protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken) protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
{ {
var urlHash = info.Url.GetMD5().ToString("N"); var urlHash = info.Url.GetMD5().ToString("N");