2015-07-20 18:32:55 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.LiveTv;
|
2015-08-19 16:43:23 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2015-07-20 18:32:55 +00:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
|
|
|
|
|
{
|
2015-08-19 17:58:41 +00:00
|
|
|
|
public class M3UTunerHost : BaseTunerHost, ITunerHost
|
2015-07-20 18:32:55 +00:00
|
|
|
|
{
|
2015-08-19 19:25:18 +00:00
|
|
|
|
public M3UTunerHost(IConfigurationManager config, ILogger logger)
|
|
|
|
|
: base(config, logger)
|
2015-07-20 18:32:55 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 17:58:41 +00:00
|
|
|
|
public override string Type
|
2015-07-20 18:32:55 +00:00
|
|
|
|
{
|
2015-08-19 17:58:41 +00:00
|
|
|
|
get { return "m3u"; }
|
2015-08-19 16:43:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 17:58:41 +00:00
|
|
|
|
public string Name
|
2015-08-19 16:43:23 +00:00
|
|
|
|
{
|
2015-08-19 17:58:41 +00:00
|
|
|
|
get { return "M3U Tuner"; }
|
2015-08-19 16:43:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 19:25:18 +00:00
|
|
|
|
private const string ChannelIdPrefix = "m3u_";
|
|
|
|
|
|
2015-08-19 17:58:41 +00:00
|
|
|
|
protected override async Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
|
2015-08-19 16:43:23 +00:00
|
|
|
|
{
|
2015-08-19 17:58:41 +00:00
|
|
|
|
var url = info.Url;
|
2015-08-19 16:43:23 +00:00
|
|
|
|
var urlHash = url.GetMD5().ToString("N");
|
|
|
|
|
|
2015-07-20 18:32:55 +00:00
|
|
|
|
int position = 0;
|
|
|
|
|
string line;
|
|
|
|
|
// Read the file and display it line by line.
|
2015-08-19 16:43:23 +00:00
|
|
|
|
var file = new StreamReader(url);
|
2015-07-20 18:32:55 +00:00
|
|
|
|
var channels = new List<M3UChannel>();
|
|
|
|
|
while ((line = file.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
line = line.Trim();
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(line))
|
|
|
|
|
{
|
|
|
|
|
if (position == 0 && !line.StartsWith("#EXTM3U"))
|
|
|
|
|
{
|
|
|
|
|
throw new ApplicationException("wrong file");
|
|
|
|
|
}
|
|
|
|
|
if (position % 2 == 0)
|
|
|
|
|
{
|
|
|
|
|
if (position != 0)
|
|
|
|
|
{
|
|
|
|
|
channels.Last().Path = line;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
line = line.Replace("#EXTM3U", "");
|
|
|
|
|
line = line.Trim();
|
|
|
|
|
var vars = line.Split(' ').ToList();
|
|
|
|
|
foreach (var variable in vars)
|
|
|
|
|
{
|
|
|
|
|
var list = variable.Replace('"', ' ').Split('=');
|
|
|
|
|
switch (list[0])
|
|
|
|
|
{
|
|
|
|
|
case ("id"):
|
|
|
|
|
//_id = list[1];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!line.StartsWith("#EXTINF:")) { throw new ApplicationException("Bad file"); }
|
|
|
|
|
line = line.Replace("#EXTINF:", "");
|
|
|
|
|
var nameStart = line.LastIndexOf(',');
|
|
|
|
|
line = line.Substring(0, nameStart);
|
|
|
|
|
var vars = line.Split(' ').ToList();
|
|
|
|
|
vars.RemoveAt(0);
|
|
|
|
|
channels.Add(new M3UChannel());
|
|
|
|
|
foreach (var variable in vars)
|
|
|
|
|
{
|
|
|
|
|
var list = variable.Replace('"', ' ').Split('=');
|
|
|
|
|
switch (list[0])
|
|
|
|
|
{
|
|
|
|
|
case "tvg-id":
|
2015-08-19 19:25:18 +00:00
|
|
|
|
channels.Last().Id = ChannelIdPrefix + urlHash + list[1];
|
2015-07-20 18:32:55 +00:00
|
|
|
|
channels.Last().Number = list[1];
|
|
|
|
|
break;
|
|
|
|
|
case "tvg-name":
|
|
|
|
|
channels.Last().Name = list[1];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
position++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file.Close();
|
2015-08-19 17:58:41 +00:00
|
|
|
|
return channels;
|
2015-07-20 18:32:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 16:43:23 +00:00
|
|
|
|
public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
|
2015-07-20 18:32:55 +00:00
|
|
|
|
{
|
2015-08-19 16:43:23 +00:00
|
|
|
|
var list = GetConfiguration().TunerHosts
|
|
|
|
|
.Where(i => i.IsEnabled && string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
.Select(i => new LiveTvTunerInfo()
|
2015-07-20 18:32:55 +00:00
|
|
|
|
{
|
|
|
|
|
Name = Name,
|
|
|
|
|
SourceType = Type,
|
|
|
|
|
Status = LiveTvTunerStatus.Available,
|
2015-08-19 16:43:23 +00:00
|
|
|
|
Id = i.Url.GetMD5().ToString("N"),
|
|
|
|
|
Url = i.Url
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
2015-08-19 19:25:18 +00:00
|
|
|
|
|
2015-07-20 18:32:55 +00:00
|
|
|
|
return Task.FromResult(list);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 19:25:18 +00:00
|
|
|
|
protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
|
2015-07-20 18:32:55 +00:00
|
|
|
|
{
|
2015-08-16 18:37:53 +00:00
|
|
|
|
var urlHash = info.Url.GetMD5().ToString("N");
|
2015-08-19 19:25:18 +00:00
|
|
|
|
var prefix = ChannelIdPrefix + urlHash;
|
|
|
|
|
if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
2015-08-16 18:37:53 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-19 19:25:18 +00:00
|
|
|
|
channelId = channelId.Substring(prefix.Length);
|
2015-08-16 18:37:53 +00:00
|
|
|
|
|
2015-08-19 17:58:41 +00:00
|
|
|
|
var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
|
2015-07-20 18:32:55 +00:00
|
|
|
|
var m3uchannels = channels.Cast<M3UChannel>();
|
|
|
|
|
var channel = m3uchannels.FirstOrDefault(c => c.Id == channelId);
|
|
|
|
|
if (channel != null)
|
|
|
|
|
{
|
|
|
|
|
var path = channel.Path;
|
|
|
|
|
MediaProtocol protocol = MediaProtocol.File;
|
|
|
|
|
if (path.StartsWith("http"))
|
|
|
|
|
{
|
|
|
|
|
protocol = MediaProtocol.Http;
|
|
|
|
|
}
|
|
|
|
|
else if (path.StartsWith("rtmp"))
|
|
|
|
|
{
|
|
|
|
|
protocol = MediaProtocol.Rtmp;
|
|
|
|
|
}
|
|
|
|
|
else if (path.StartsWith("rtsp"))
|
|
|
|
|
{
|
|
|
|
|
protocol = MediaProtocol.Rtsp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new MediaSourceInfo
|
|
|
|
|
{
|
|
|
|
|
Path = channel.Path,
|
|
|
|
|
Protocol = protocol,
|
|
|
|
|
MediaStreams = new List<MediaStream>
|
|
|
|
|
{
|
|
|
|
|
new MediaStream
|
|
|
|
|
{
|
|
|
|
|
Type = MediaStreamType.Video,
|
|
|
|
|
// Set the index to -1 because we don't know the exact index of the video stream within the container
|
|
|
|
|
Index = -1,
|
|
|
|
|
IsInterlaced = true
|
|
|
|
|
},
|
|
|
|
|
new MediaStream
|
|
|
|
|
{
|
|
|
|
|
Type = MediaStreamType.Audio,
|
|
|
|
|
// Set the index to -1 because we don't know the exact index of the audio stream within the container
|
|
|
|
|
Index = -1
|
|
|
|
|
|
|
|
|
|
}
|
2015-07-23 23:40:54 +00:00
|
|
|
|
},
|
|
|
|
|
RequiresOpening = false,
|
|
|
|
|
RequiresClosing = false
|
2015-07-20 18:32:55 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
throw new ApplicationException("Host doesnt provide this channel");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class M3UChannel : ChannelInfo
|
|
|
|
|
{
|
|
|
|
|
public string Path { get; set; }
|
|
|
|
|
|
|
|
|
|
public M3UChannel()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-23 13:23:22 +00:00
|
|
|
|
|
|
|
|
|
public async Task Validate(TunerHostInfo info)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(info.Url))
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-07-23 23:40:54 +00:00
|
|
|
|
|
2015-08-19 19:25:18 +00:00
|
|
|
|
protected override bool IsValidChannelId(string channelId)
|
|
|
|
|
{
|
|
|
|
|
return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
|
2015-07-23 23:40:54 +00:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2015-08-19 19:25:18 +00:00
|
|
|
|
|
|
|
|
|
protected override Task<bool> IsAvailable(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
}
|
2015-07-20 18:32:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|