jellyfin/MediaBrowser.Server.Implementations/LiveTv/TunerHosts/M3UTunerHost.cs

158 lines
5.8 KiB
C#
Raw Normal View History

2015-07-20 20:32:55 +02: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 18:43:23 +02:00
using MediaBrowser.Model.Logging;
2015-07-20 20:32:55 +02:00
using MediaBrowser.Model.MediaInfo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2015-10-04 06:23:11 +02:00
using CommonIO;
2015-09-23 03:22:52 +02:00
using MediaBrowser.Common.Net;
2015-10-14 04:41:46 +02:00
using MediaBrowser.Controller.MediaEncoding;
2015-09-23 03:22:52 +02:00
using MediaBrowser.Model.Serialization;
2015-07-20 20:32:55 +02:00
namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
{
2016-02-19 07:20:18 +01:00
public class M3UTunerHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
2015-07-20 20:32:55 +02:00
{
2015-09-14 01:07:54 +02:00
private readonly IFileSystem _fileSystem;
2015-10-14 04:41:46 +02:00
private readonly IHttpClient _httpClient;
2015-09-23 03:22:52 +02:00
2015-10-30 17:40:12 +01:00
public M3UTunerHost(IConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient)
: base(config, logger, jsonSerializer, mediaEncoder)
2015-07-20 20:32:55 +02:00
{
2015-09-14 01:07:54 +02:00
_fileSystem = fileSystem;
2015-09-23 03:22:52 +02:00
_httpClient = httpClient;
2015-07-20 20:32:55 +02:00
}
2015-08-19 19:58:41 +02:00
public override string Type
2015-07-20 20:32:55 +02:00
{
2015-08-19 19:58:41 +02:00
get { return "m3u"; }
2015-08-19 18:43:23 +02:00
}
2015-08-19 19:58:41 +02:00
public string Name
2015-08-19 18:43:23 +02:00
{
2015-08-19 19:58:41 +02:00
get { return "M3U Tuner"; }
2015-08-19 18:43:23 +02:00
}
2015-08-19 21:25:18 +02:00
private const string ChannelIdPrefix = "m3u_";
2015-08-19 19:58:41 +02:00
protected override async Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
2015-08-19 18:43:23 +02:00
{
2016-02-24 20:06:26 +01:00
return await new M3uParser(Logger, _fileSystem, _httpClient).Parse(info.Url, ChannelIdPrefix, info.Id, cancellationToken).ConfigureAwait(false);
2015-07-20 20:32:55 +02:00
}
2015-08-19 18:43:23 +02:00
public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
2015-07-20 20:32:55 +02:00
{
2016-02-19 07:20:18 +01:00
var list = GetTunerHosts()
2015-08-19 18:43:23 +02:00
.Select(i => new LiveTvTunerInfo()
2015-07-20 20:32:55 +02:00
{
Name = Name,
SourceType = Type,
Status = LiveTvTunerStatus.Available,
2015-08-19 18:43:23 +02:00
Id = i.Url.GetMD5().ToString("N"),
Url = i.Url
})
.ToList();
2015-08-19 21:25:18 +02:00
2015-07-20 20:32:55 +02:00
return Task.FromResult(list);
}
2015-08-19 21:25:18 +02:00
protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
2015-08-27 21:59:42 +02:00
{
var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
return sources.First();
}
public async Task Validate(TunerHostInfo info)
{
2016-02-19 07:20:18 +01:00
using (var stream = await new M3uParser(Logger, _fileSystem, _httpClient).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
2015-08-27 21:59:42 +02:00
{
2015-09-23 03:22:52 +02:00
2015-08-27 21:59:42 +02:00
}
}
protected override bool IsValidChannelId(string channelId)
{
return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
}
protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
2015-07-20 20:32:55 +02:00
{
2015-08-16 20:37:53 +02:00
var urlHash = info.Url.GetMD5().ToString("N");
2015-08-19 21:25:18 +02:00
var prefix = ChannelIdPrefix + urlHash;
if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
2015-08-16 20:37:53 +02:00
{
return null;
}
2015-08-19 19:58:41 +02:00
var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
2015-07-20 20:32:55 +02:00
var m3uchannels = channels.Cast<M3UChannel>();
2015-08-27 21:59:42 +02:00
var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
2015-07-20 20:32:55 +02:00
if (channel != null)
{
var path = channel.Path;
MediaProtocol protocol = MediaProtocol.File;
2015-08-27 21:59:42 +02:00
if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
2015-07-20 20:32:55 +02:00
{
protocol = MediaProtocol.Http;
}
2015-08-27 21:59:42 +02:00
else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
2015-07-20 20:32:55 +02:00
{
protocol = MediaProtocol.Rtmp;
}
2015-08-27 21:59:42 +02:00
else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
2015-07-20 20:32:55 +02:00
{
protocol = MediaProtocol.Rtsp;
}
2015-08-27 21:59:42 +02:00
var mediaSource = new MediaSourceInfo
2015-07-20 20:32:55 +02:00
{
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-24 01:40:54 +02:00
},
RequiresOpening = false,
2016-05-03 06:17:57 +02:00
RequiresClosing = false,
2016-08-16 20:46:12 +02:00
ReadAtNativeFramerate = false
2015-07-20 20:32:55 +02:00
};
2015-07-23 15:23:22 +02:00
2015-08-27 21:59:42 +02:00
return new List<MediaSourceInfo> { mediaSource };
2015-07-23 15:23:22 +02:00
}
return new List<MediaSourceInfo>();
2015-07-24 01:40:54 +02:00
}
2015-10-30 17:40:12 +01:00
protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
2016-04-26 04:16:46 +02:00
public string ApplyDuration(string streamPath, TimeSpan duration)
{
return streamPath;
}
2015-07-20 20:32:55 +02:00
}
}