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

186 lines
6.9 KiB
C#
Raw Normal View History

2017-07-30 18:02:25 +00:00
using MediaBrowser.Common.Extensions;
2015-07-20 18:32:55 +00:00
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.Linq;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2015-09-23 01:22:52 +00:00
using MediaBrowser.Common.Net;
2016-10-18 18:35:27 +00:00
using MediaBrowser.Controller;
2016-09-25 18:39:13 +00:00
using MediaBrowser.Controller.Configuration;
2015-10-14 02:41:46 +00:00
using MediaBrowser.Controller.MediaEncoding;
2015-09-23 01:22:52 +00:00
using MediaBrowser.Model.Serialization;
2017-05-22 04:54:02 +00:00
using MediaBrowser.Model.System;
2015-07-20 18:32:55 +00:00
2016-11-03 23:35:19 +00:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts
2015-07-20 18:32:55 +00:00
{
2016-02-19 06:20:18 +00:00
public class M3UTunerHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
2015-07-20 18:32:55 +00:00
{
2015-10-14 02:41:46 +00:00
private readonly IHttpClient _httpClient;
2016-10-18 18:35:27 +00:00
private readonly IServerApplicationHost _appHost;
2017-05-22 04:54:02 +00:00
private readonly IEnvironmentInfo _environment;
2017-08-20 19:10:00 +00:00
private readonly INetworkManager _networkManager;
2015-09-23 01:22:52 +00:00
2017-08-20 19:10:00 +00:00
public M3UTunerHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient, IServerApplicationHost appHost, IEnvironmentInfo environment, INetworkManager networkManager) : base(config, logger, jsonSerializer, mediaEncoder, fileSystem)
2015-07-20 18:32:55 +00:00
{
2015-09-23 01:22:52 +00:00
_httpClient = httpClient;
2016-10-18 18:35:27 +00:00
_appHost = appHost;
2017-05-22 04:54:02 +00:00
_environment = environment;
2017-08-20 19:10:00 +00:00
_networkManager = networkManager;
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
}
2017-08-20 19:10:00 +00:00
public virtual 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
}
2017-07-30 18:02:25 +00:00
private string GetFullChannelIdPrefix(TunerHostInfo info)
{
return ChannelIdPrefix + info.Url.GetMD5().ToString("N");
}
2017-02-23 19:13:26 +00:00
protected override async Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
2015-08-19 16:43:23 +00:00
{
2017-07-30 18:02:25 +00:00
var channelIdPrefix = GetFullChannelIdPrefix(info);
var result = await new M3uParser(Logger, FileSystem, _httpClient, _appHost).Parse(info.Url, channelIdPrefix, info.Id, cancellationToken).ConfigureAwait(false);
2017-02-23 19:13:26 +00:00
return result.Cast<ChannelInfo>().ToList();
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
{
2016-02-19 06:20:18 +00:00
var list = GetTunerHosts()
2015-08-19 16:43:23 +00:00
.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);
}
2017-09-28 17:02:49 +00:00
protected override async Task<ILiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
2015-08-27 19:59:42 +00:00
{
var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
2017-09-28 17:02:49 +00:00
var liveStream = new LiveStream(sources.First(), _environment, FileSystem, Logger, Config.ApplicationPaths);
2016-09-25 18:39:13 +00:00
return liveStream;
2015-08-27 19:59:42 +00:00
}
public async Task Validate(TunerHostInfo info)
{
2017-07-05 18:30:12 +00:00
using (var stream = await new M3uParser(Logger, FileSystem, _httpClient, _appHost).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
2015-08-27 19:59:42 +00:00
{
2015-09-23 01:22:52 +00:00
2015-08-27 19:59:42 +00:00
}
}
protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
2015-07-20 18:32:55 +00:00
{
2015-08-19 17:58:41 +00:00
var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
2017-08-20 19:10:00 +00:00
var channel = channels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
2015-07-20 18:32:55 +00:00
if (channel != null)
{
2017-08-20 19:10:00 +00:00
return new List<MediaSourceInfo> { CreateMediaSourceInfo(info, channel) };
}
return new List<MediaSourceInfo>();
}
protected virtual MediaSourceInfo CreateMediaSourceInfo(TunerHostInfo info, ChannelInfo channel)
{
var path = channel.Path;
MediaProtocol protocol = MediaProtocol.File;
if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
protocol = MediaProtocol.Http;
}
else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
{
protocol = MediaProtocol.Rtmp;
}
else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
{
protocol = MediaProtocol.Rtsp;
}
else if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
{
protocol = MediaProtocol.Udp;
}
else if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
{
protocol = MediaProtocol.Rtmp;
}
Uri uri;
var isRemote = true;
if (Uri.TryCreate(path, UriKind.Absolute, out uri))
{
isRemote = !_networkManager.IsInLocalNetwork(uri.Host);
}
2015-07-20 18:32:55 +00:00
2017-08-20 19:10:00 +00:00
var mediaSource = new MediaSourceInfo
{
Path = path,
Protocol = protocol,
MediaStreams = new List<MediaStream>
2015-07-20 18:32:55 +00:00
{
2017-08-20 19:10:00 +00:00
new MediaStream
2015-07-20 18:32:55 +00:00
{
2017-08-20 19:10:00 +00:00
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
2015-07-23 23:40:54 +00:00
},
2017-08-20 19:10:00 +00:00
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
2016-05-03 04:17:57 +00:00
2017-08-20 19:10:00 +00:00
}
},
RequiresOpening = true,
RequiresClosing = true,
RequiresLooping = info.EnableStreamLooping,
2016-09-25 18:39:13 +00:00
2017-08-20 19:10:00 +00:00
ReadAtNativeFramerate = false,
2017-07-15 20:19:58 +00:00
2017-08-20 19:10:00 +00:00
Id = channel.Path.GetMD5().ToString("N"),
IsInfiniteStream = true,
IsRemote = isRemote,
2015-07-23 13:23:22 +00:00
2017-08-20 19:10:00 +00:00
IgnoreDts = true
};
2017-01-21 20:27:07 +00:00
2017-08-20 19:10:00 +00:00
mediaSource.InferTotalBitrate();
return mediaSource;
2015-07-23 23:40:54 +00:00
}
2015-10-30 16:40:12 +00:00
protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
{
return Task.FromResult(true);
}
2017-03-13 04:08:49 +00:00
2017-03-13 04:49:10 +00:00
public Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
2017-03-13 04:08:49 +00:00
{
return Task.FromResult(new List<TunerHostInfo>());
}
2015-07-20 18:32:55 +00:00
}
2017-01-25 20:14:47 +00:00
}