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

177 lines
6.8 KiB
C#
Raw Normal View History

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