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

211 lines
7.9 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
2015-07-20 11:32:55 -07:00
using System.Collections.Generic;
using System.Globalization;
using System.IO;
2015-07-20 11:32:55 -07:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;
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;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.LiveTv;
using MediaBrowser.Model.MediaInfo;
2015-09-22 18:22:52 -07:00
using MediaBrowser.Model.Serialization;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
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-10-13 19:41:46 -07:00
private readonly IHttpClient _httpClient;
2016-10-18 11:35:27 -07:00
private readonly IServerApplicationHost _appHost;
2017-08-20 12:10:00 -07:00
private readonly INetworkManager _networkManager;
2018-09-12 10:26:21 -07:00
private readonly IMediaSourceManager _mediaSourceManager;
2019-07-07 07:39:35 -07:00
private readonly IStreamHelper _streamHelper;
public M3UTunerHost(
IServerConfigurationManager config,
IMediaSourceManager mediaSourceManager,
ILogger<M3UTunerHost> logger,
2019-07-07 07:39:35 -07:00
IJsonSerializer jsonSerializer,
IFileSystem fileSystem,
IHttpClient httpClient,
IServerApplicationHost appHost,
INetworkManager networkManager,
IStreamHelper streamHelper)
2019-02-06 12:38:42 -07:00
: base(config, logger, jsonSerializer, fileSystem)
2015-07-20 11:32:55 -07:00
{
2015-09-22 18:22:52 -07:00
_httpClient = httpClient;
2016-10-18 11:35:27 -07:00
_appHost = appHost;
2017-08-20 12:10:00 -07:00
_networkManager = networkManager;
2018-09-12 10:26:21 -07:00
_mediaSourceManager = mediaSourceManager;
2019-07-07 07:39:35 -07:00
_streamHelper = streamHelper;
2015-07-20 11:32:55 -07:00
}
public override string Type => "m3u";
2015-08-19 09:43:23 -07:00
public virtual string Name => "M3U Tuner";
2015-08-19 09:43:23 -07:00
2017-07-30 11:02:25 -07:00
private string GetFullChannelIdPrefix(TunerHostInfo info)
{
return ChannelIdPrefix + info.Url.GetMD5().ToString("N", CultureInfo.InvariantCulture);
2017-07-30 11:02:25 -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-07-30 11:02:25 -07:00
var channelIdPrefix = GetFullChannelIdPrefix(info);
return await new M3uParser(Logger, _httpClient, _appHost).Parse(info.Url, channelIdPrefix, info.Id, cancellationToken).ConfigureAwait(false);
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,
Id = i.Url.GetMD5().ToString("N", CultureInfo.InvariantCulture),
2015-08-19 09:43:23 -07:00
Url = i.Url
})
.ToList();
2015-08-19 12:25:18 -07:00
2015-07-20 11:32:55 -07:00
return Task.FromResult(list);
}
private static readonly string[] _disallowedSharedStreamExtensions =
2017-11-29 13:50:18 -07:00
{
".mkv",
".mp4",
".m3u8",
".mpd"
};
2018-09-12 10:26:21 -07:00
protected override async Task<ILiveStream> GetChannelStream(TunerHostInfo info, ChannelInfo channelInfo, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
2015-08-27 12:59:42 -07:00
{
var tunerCount = info.TunerCount;
if (tunerCount > 0)
{
2018-09-12 10:26:21 -07:00
var tunerHostId = info.Id;
var liveStreams = currentLiveStreams.Where(i => string.Equals(i.TunerHostId, tunerHostId, StringComparison.OrdinalIgnoreCase));
if (liveStreams.Count() >= tunerCount)
{
2018-09-12 10:26:21 -07:00
throw new LiveTvConflictException("M3U simultaneous stream limit has been reached.");
}
}
2018-09-12 10:26:21 -07:00
var sources = await GetChannelStreamMediaSources(info, channelInfo, cancellationToken).ConfigureAwait(false);
2015-08-27 12:59:42 -07:00
var mediaSource = sources[0];
2017-11-14 00:41:21 -07:00
if (mediaSource.Protocol == MediaProtocol.Http && !mediaSource.RequiresLooping)
{
2017-11-29 13:50:18 -07:00
var extension = Path.GetExtension(mediaSource.Path) ?? string.Empty;
if (!_disallowedSharedStreamExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
return new SharedHttpStream(mediaSource, info, streamId, FileSystem, _httpClient, Logger, Config, _appHost, _streamHelper);
2017-11-29 13:50:18 -07:00
}
2017-11-14 00:41:21 -07:00
}
return new LiveStream(mediaSource, info, FileSystem, Logger, Config, _streamHelper);
2015-08-27 12:59:42 -07:00
}
public async Task Validate(TunerHostInfo info)
{
2019-02-06 12:38:42 -07:00
using (var stream = await new M3uParser(Logger, _httpClient, _appHost).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
2015-08-27 12:59:42 -07:00
{
}
}
2018-09-12 10:26:21 -07:00
protected override Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, ChannelInfo channelInfo, CancellationToken cancellationToken)
2015-07-20 11:32:55 -07:00
{
2018-09-12 10:26:21 -07:00
return Task.FromResult(new List<MediaSourceInfo> { CreateMediaSourceInfo(info, channelInfo) });
2017-08-20 12:10:00 -07:00
}
protected virtual MediaSourceInfo CreateMediaSourceInfo(TunerHostInfo info, ChannelInfo channel)
{
var path = channel.Path;
2018-09-12 10:26:21 -07:00
var supportsDirectPlay = !info.EnableStreamLooping && info.TunerCount == 0;
var supportsDirectStream = !info.EnableStreamLooping;
var protocol = _mediaSourceManager.GetPathProtocol(path);
2017-08-20 12:10:00 -07:00
var isRemote = true;
if (Uri.TryCreate(path, UriKind.Absolute, out var uri))
2017-08-20 12:10:00 -07:00
{
isRemote = !_networkManager.IsInLocalNetwork(uri.Host);
}
2015-07-20 11:32:55 -07:00
2018-09-12 10:26:21 -07:00
var httpHeaders = new Dictionary<string, string>();
if (protocol == MediaProtocol.Http)
{
// Use user-defined user-agent. If there isn't one, make it look like a browser.
httpHeaders[HeaderNames.UserAgent] = string.IsNullOrWhiteSpace(info.UserAgent) ?
2018-09-12 10:26:21 -07:00
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.85 Safari/537.36" :
info.UserAgent;
}
2017-11-21 15:14:56 -07:00
2017-08-20 12:10:00 -07:00
var mediaSource = new MediaSourceInfo
{
Path = path,
Protocol = protocol,
MediaStreams = new List<MediaStream>
2015-07-20 11:32:55 -07:00
{
2017-08-20 12:10:00 -07:00
new MediaStream
2015-07-20 11:32:55 -07:00
{
2017-08-20 12:10:00 -07: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 16:40:54 -07:00
},
2017-08-20 12:10:00 -07: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
}
},
RequiresOpening = true,
RequiresClosing = true,
RequiresLooping = info.EnableStreamLooping,
2016-09-25 11:39:13 -07:00
2017-08-20 12:10:00 -07:00
ReadAtNativeFramerate = false,
2017-07-15 13:19:58 -07:00
Id = channel.Path.GetMD5().ToString("N", CultureInfo.InvariantCulture),
2017-08-20 12:10:00 -07:00
IsInfiniteStream = true,
IsRemote = isRemote,
2015-07-23 06:23:22 -07:00
2017-11-21 15:14:56 -07:00
IgnoreDts = true,
2018-09-12 10:26:21 -07:00
SupportsDirectPlay = supportsDirectPlay,
SupportsDirectStream = supportsDirectStream,
RequiredHttpHeaders = httpHeaders
2017-08-20 12:10:00 -07:00
};
2017-01-21 13:27:07 -07:00
2017-08-20 12:10:00 -07:00
mediaSource.InferTotalBitrate();
return mediaSource;
2015-07-23 16:40:54 -07:00
}
2015-10-30 09:40:12 -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
}
}