2021-05-20 15:28:18 -04:00
|
|
|
#nullable disable
|
|
|
|
|
2020-02-06 09:20:23 -05:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 14:54:44 -05:00
|
|
|
using System;
|
2015-07-20 14:32:55 -04:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 17:22:57 -05:00
|
|
|
using System.Globalization;
|
2019-01-13 14:22:00 -05:00
|
|
|
using System.IO;
|
2015-07-20 14:32:55 -04:00
|
|
|
using System.Linq;
|
2020-08-31 14:08:45 -04:00
|
|
|
using System.Net.Http;
|
2015-07-20 14:32:55 -04:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-12-20 07:31:07 -05:00
|
|
|
using Jellyfin.Extensions;
|
2019-01-13 14:22:00 -05:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2015-09-22 21:22:52 -04:00
|
|
|
using MediaBrowser.Common.Net;
|
2016-10-18 14:35:27 -04:00
|
|
|
using MediaBrowser.Controller;
|
2016-09-25 14:39:13 -04:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2019-01-13 14:22:00 -05:00
|
|
|
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;
|
2020-08-04 18:58:14 -04:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2019-01-13 14:22:00 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-03-05 13:20:28 -05:00
|
|
|
using Microsoft.Net.Http.Headers;
|
2015-07-20 14:32:55 -04:00
|
|
|
|
2016-11-03 19:35:19 -04:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
2016-02-19 01:20:18 -05:00
|
|
|
public class M3UTunerHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
2023-06-27 23:36:39 -04:00
|
|
|
private static readonly string[] _disallowedMimeTypes =
|
2021-05-28 08:33:54 -04:00
|
|
|
{
|
2023-06-27 23:36:39 -04:00
|
|
|
"video/x-matroska",
|
|
|
|
"video/mp4",
|
|
|
|
"application/vnd.apple.mpegurl",
|
|
|
|
"application/mpegurl",
|
|
|
|
"application/x-mpegurl",
|
|
|
|
"video/vnd.mpeg.dash.mpd"
|
2021-05-28 08:33:54 -04:00
|
|
|
};
|
|
|
|
|
2020-08-31 14:08:45 -04:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2016-10-18 14:35:27 -04:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2017-08-20 15:10:00 -04:00
|
|
|
private readonly INetworkManager _networkManager;
|
2018-09-12 13:26:21 -04:00
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2019-07-07 10:39:35 -04:00
|
|
|
private readonly IStreamHelper _streamHelper;
|
|
|
|
|
|
|
|
public M3UTunerHost(
|
|
|
|
IServerConfigurationManager config,
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
2020-05-12 16:03:15 -04:00
|
|
|
ILogger<M3UTunerHost> logger,
|
2019-07-07 10:39:35 -04:00
|
|
|
IFileSystem fileSystem,
|
2020-08-31 14:08:45 -04:00
|
|
|
IHttpClientFactory httpClientFactory,
|
2019-07-07 10:39:35 -04:00
|
|
|
IServerApplicationHost appHost,
|
|
|
|
INetworkManager networkManager,
|
2023-08-18 08:00:56 -04:00
|
|
|
IStreamHelper streamHelper)
|
|
|
|
: base(config, logger, fileSystem)
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
2020-08-31 14:08:45 -04:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2016-10-18 14:35:27 -04:00
|
|
|
_appHost = appHost;
|
2017-08-20 15:10:00 -04:00
|
|
|
_networkManager = networkManager;
|
2018-09-12 13:26:21 -04:00
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2019-07-07 10:39:35 -04:00
|
|
|
_streamHelper = streamHelper;
|
2015-07-20 14:32:55 -04:00
|
|
|
}
|
|
|
|
|
2019-01-06 15:50:43 -05:00
|
|
|
public override string Type => "m3u";
|
2015-08-19 12:43:23 -04:00
|
|
|
|
2019-01-06 15:50:43 -05:00
|
|
|
public virtual string Name => "M3U Tuner";
|
2015-08-19 12:43:23 -04:00
|
|
|
|
2017-07-30 14:02:25 -04:00
|
|
|
private string GetFullChannelIdPrefix(TunerHostInfo info)
|
|
|
|
{
|
2019-02-28 17:22:57 -05:00
|
|
|
return ChannelIdPrefix + info.Url.GetMD5().ToString("N", CultureInfo.InvariantCulture);
|
2017-07-30 14:02:25 -04:00
|
|
|
}
|
|
|
|
|
2021-09-03 12:46:34 -04:00
|
|
|
protected override async Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken)
|
2015-08-19 12:43:23 -04:00
|
|
|
{
|
2021-09-03 12:46:34 -04:00
|
|
|
var channelIdPrefix = GetFullChannelIdPrefix(tuner);
|
2017-07-30 14:02:25 -04:00
|
|
|
|
2021-05-28 08:33:54 -04:00
|
|
|
return await new M3uParser(Logger, _httpClientFactory)
|
2021-09-03 12:46:34 -04:00
|
|
|
.Parse(tuner, channelIdPrefix, cancellationToken)
|
2020-11-01 21:23:28 -05:00
|
|
|
.ConfigureAwait(false);
|
2015-07-20 14:32:55 -04:00
|
|
|
}
|
|
|
|
|
2015-08-19 12:43:23 -04:00
|
|
|
public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
2016-02-19 01:20:18 -05:00
|
|
|
var list = GetTunerHosts()
|
2015-08-19 12:43:23 -04:00
|
|
|
.Select(i => new LiveTvTunerInfo()
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
|
|
|
Name = Name,
|
|
|
|
SourceType = Type,
|
|
|
|
Status = LiveTvTunerStatus.Available,
|
2019-02-28 17:22:57 -05:00
|
|
|
Id = i.Url.GetMD5().ToString("N", CultureInfo.InvariantCulture),
|
2015-08-19 12:43:23 -04:00
|
|
|
Url = i.Url
|
|
|
|
})
|
|
|
|
.ToList();
|
2015-08-19 15:25:18 -04:00
|
|
|
|
2015-07-20 14:32:55 -04:00
|
|
|
return Task.FromResult(list);
|
|
|
|
}
|
|
|
|
|
2021-09-03 12:46:34 -04:00
|
|
|
protected override async Task<ILiveStream> GetChannelStream(TunerHostInfo tunerHost, ChannelInfo channel, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
|
2015-08-27 15:59:42 -04:00
|
|
|
{
|
2021-09-03 12:46:34 -04:00
|
|
|
var tunerCount = tunerHost.TunerCount;
|
2017-11-17 16:54:33 -05:00
|
|
|
|
|
|
|
if (tunerCount > 0)
|
|
|
|
{
|
2021-09-03 12:46:34 -04:00
|
|
|
var tunerHostId = tunerHost.Id;
|
2019-03-25 17:25:32 -04:00
|
|
|
var liveStreams = currentLiveStreams.Where(i => string.Equals(i.TunerHostId, tunerHostId, StringComparison.OrdinalIgnoreCase));
|
2017-11-17 16:54:33 -05:00
|
|
|
|
2019-03-25 17:25:32 -04:00
|
|
|
if (liveStreams.Count() >= tunerCount)
|
2017-11-17 16:54:33 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
throw new LiveTvConflictException("M3U simultaneous stream limit has been reached.");
|
2017-11-17 16:54:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 12:46:34 -04:00
|
|
|
var sources = await GetChannelStreamMediaSources(tunerHost, channel, cancellationToken).ConfigureAwait(false);
|
2015-08-27 15:59:42 -04:00
|
|
|
|
2019-03-25 17:25:32 -04:00
|
|
|
var mediaSource = sources[0];
|
2017-11-14 02:41:21 -05:00
|
|
|
|
|
|
|
if (mediaSource.Protocol == MediaProtocol.Http && !mediaSource.RequiresLooping)
|
|
|
|
{
|
2023-06-27 23:36:39 -04:00
|
|
|
using var message = new HttpRequestMessage(HttpMethod.Head, mediaSource.Path);
|
|
|
|
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
|
|
|
|
.SendAsync(message, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2017-11-29 15:50:18 -05:00
|
|
|
|
2023-06-27 23:36:39 -04:00
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
if (!_disallowedMimeTypes.Contains(response.Content.Headers.ContentType?.ToString(), StringComparison.OrdinalIgnoreCase))
|
2017-11-29 15:50:18 -05:00
|
|
|
{
|
2021-09-03 12:46:34 -04:00
|
|
|
return new SharedHttpStream(mediaSource, tunerHost, streamId, FileSystem, _httpClientFactory, Logger, Config, _appHost, _streamHelper);
|
2017-11-29 15:50:18 -05:00
|
|
|
}
|
2017-11-14 02:41:21 -05:00
|
|
|
}
|
|
|
|
|
2021-09-03 12:46:34 -04:00
|
|
|
return new LiveStream(mediaSource, tunerHost, FileSystem, Logger, Config, _streamHelper);
|
2015-08-27 15:59:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Validate(TunerHostInfo info)
|
|
|
|
{
|
2021-12-15 12:25:36 -05:00
|
|
|
using (await new M3uParser(Logger, _httpClientFactory).GetListingsStream(info, CancellationToken.None).ConfigureAwait(false))
|
2015-08-27 15:59:42 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 12:46:34 -04:00
|
|
|
protected override Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, ChannelInfo channel, CancellationToken cancellationToken)
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
2021-09-03 12:46:34 -04:00
|
|
|
return Task.FromResult(new List<MediaSourceInfo> { CreateMediaSourceInfo(tuner, channel) });
|
2017-08-20 15:10:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual MediaSourceInfo CreateMediaSourceInfo(TunerHostInfo info, ChannelInfo channel)
|
|
|
|
{
|
|
|
|
var path = channel.Path;
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
var supportsDirectPlay = !info.EnableStreamLooping && info.TunerCount == 0;
|
|
|
|
var supportsDirectStream = !info.EnableStreamLooping;
|
|
|
|
|
|
|
|
var protocol = _mediaSourceManager.GetPathProtocol(path);
|
2017-08-20 15:10:00 -04:00
|
|
|
|
|
|
|
var isRemote = true;
|
2019-01-13 15:46:33 -05:00
|
|
|
if (Uri.TryCreate(path, UriKind.Absolute, out var uri))
|
2017-08-20 15:10:00 -04:00
|
|
|
{
|
|
|
|
isRemote = !_networkManager.IsInLocalNetwork(uri.Host);
|
|
|
|
}
|
2015-07-20 14:32:55 -04:00
|
|
|
|
2018-09-12 13:26:21 -04: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.
|
2019-03-05 13:20:28 -05:00
|
|
|
httpHeaders[HeaderNames.UserAgent] = string.IsNullOrWhiteSpace(info.UserAgent) ?
|
2018-09-12 13:26:21 -04: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 17:14:56 -05:00
|
|
|
|
2017-08-20 15:10:00 -04:00
|
|
|
var mediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
Path = path,
|
|
|
|
Protocol = protocol,
|
2022-01-22 09:40:05 -05:00
|
|
|
MediaStreams = new MediaStream[]
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
2017-08-20 15:10:00 -04:00
|
|
|
new MediaStream
|
2015-07-20 14:32:55 -04:00
|
|
|
{
|
2017-08-20 15:10:00 -04: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 19:40:54 -04:00
|
|
|
},
|
2017-08-20 15:10:00 -04: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 14:39:13 -04:00
|
|
|
|
2017-08-20 15:10:00 -04:00
|
|
|
ReadAtNativeFramerate = false,
|
2017-07-15 16:19:58 -04:00
|
|
|
|
2019-02-28 17:22:57 -05:00
|
|
|
Id = channel.Path.GetMD5().ToString("N", CultureInfo.InvariantCulture),
|
2017-08-20 15:10:00 -04:00
|
|
|
IsInfiniteStream = true,
|
|
|
|
IsRemote = isRemote,
|
2015-07-23 09:23:22 -04:00
|
|
|
|
2022-06-12 18:51:08 -04:00
|
|
|
IgnoreDts = info.IgnoreDts,
|
2018-09-12 13:26:21 -04:00
|
|
|
SupportsDirectPlay = supportsDirectPlay,
|
|
|
|
SupportsDirectStream = supportsDirectStream,
|
|
|
|
|
|
|
|
RequiredHttpHeaders = httpHeaders
|
2017-08-20 15:10:00 -04:00
|
|
|
};
|
2017-01-21 15:27:07 -05:00
|
|
|
|
2017-08-20 15:10:00 -04:00
|
|
|
mediaSource.InferTotalBitrate();
|
|
|
|
|
|
|
|
return mediaSource;
|
2015-07-23 19:40:54 -04:00
|
|
|
}
|
2015-10-30 12:40:12 -04:00
|
|
|
|
2017-03-13 00:49:10 -04:00
|
|
|
public Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
|
2017-03-13 00:08:49 -04:00
|
|
|
{
|
|
|
|
return Task.FromResult(new List<TunerHostInfo>());
|
|
|
|
}
|
2015-07-20 14:32:55 -04:00
|
|
|
}
|
2018-12-13 08:18:25 -05:00
|
|
|
}
|