jellyfin/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunHost.cs

722 lines
27 KiB
C#
Raw Normal View History

#nullable disable
#pragma warning disable CS1591
2015-07-20 20:32:55 +02:00
using System;
using System.Collections.Generic;
using System.Globalization;
2015-07-20 20:32:55 +02:00
using System.IO;
using System.Linq;
using System.Net;
2019-07-07 16:39:35 +02:00
using System.Net.Http;
2020-08-05 00:58:14 +02:00
using System.Text.Json;
2015-07-20 20:32:55 +02:00
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Extensions;
using Jellyfin.Extensions.Json;
using Jellyfin.Extensions.Json.Converters;
2016-02-24 19:45:11 +01:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
2016-09-25 20:39:13 +02:00
using MediaBrowser.Controller;
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;
2016-07-08 05:22:16 +02:00
using MediaBrowser.Model.Net;
2020-08-05 00:58:14 +02:00
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
2015-07-20 20:32:55 +02:00
2016-11-04 00:35:19 +01:00
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
2015-07-20 20:32:55 +02:00
{
2016-02-19 07:20:18 +01:00
public class HdHomerunHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
2015-07-20 20:32:55 +02:00
{
private readonly IHttpClientFactory _httpClientFactory;
2016-09-25 20:39:13 +02:00
private readonly IServerApplicationHost _appHost;
2017-03-02 21:50:09 +01:00
private readonly ISocketFactory _socketFactory;
2019-07-07 16:39:35 +02:00
private readonly IStreamHelper _streamHelper;
2015-07-20 20:32:55 +02:00
2020-12-09 01:08:41 +01:00
private readonly JsonSerializerOptions _jsonOptions;
2020-08-19 18:02:34 +02:00
private readonly Dictionary<string, DiscoverResponse> _modelCache = new Dictionary<string, DiscoverResponse>();
2019-02-06 20:38:42 +01:00
public HdHomerunHost(
IServerConfigurationManager config,
ILogger<HdHomerunHost> logger,
2019-02-06 20:38:42 +01:00
IFileSystem fileSystem,
IHttpClientFactory httpClientFactory,
2019-02-06 20:38:42 +01:00
IServerApplicationHost appHost,
ISocketFactory socketFactory,
2020-08-05 00:58:14 +02:00
IStreamHelper streamHelper,
IMemoryCache memoryCache)
: base(config, logger, fileSystem, memoryCache)
2015-07-20 20:32:55 +02:00
{
_httpClientFactory = httpClientFactory;
2016-09-25 20:39:13 +02:00
_appHost = appHost;
2017-03-02 21:50:09 +01:00
_socketFactory = socketFactory;
2019-07-07 16:39:35 +02:00
_streamHelper = streamHelper;
2020-12-09 01:08:41 +01:00
_jsonOptions = new JsonSerializerOptions(JsonDefaults.Options);
_jsonOptions.Converters.Add(new JsonBoolNumberConverter());
2015-07-20 20:32:55 +02:00
}
public string Name => "HD Homerun";
2015-07-20 20:32:55 +02:00
2019-07-07 16:39:35 +02:00
public override string Type => "hdhomerun";
2015-07-20 20:32:55 +02:00
protected override string ChannelIdPrefix => "hdhr_";
2015-08-16 20:37:53 +02:00
2021-09-19 20:53:31 +02:00
private string GetChannelId(Channels i)
2019-07-07 16:39:35 +02:00
=> ChannelIdPrefix + i.GuideNumber;
2016-02-25 21:29:38 +01:00
2020-12-09 01:08:41 +01:00
internal async Task<List<Channels>> GetLineup(TunerHostInfo info, CancellationToken cancellationToken)
2015-07-20 20:32:55 +02:00
{
2017-03-06 03:32:56 +01:00
var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
2021-05-06 23:54:29 +02:00
using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(model.LineupURL ?? model.BaseURL + "/lineup.json", HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
2020-11-17 19:43:00 +01:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
2020-12-09 01:08:41 +01:00
var lineup = await JsonSerializer.DeserializeAsync<List<Channels>>(stream, _jsonOptions, cancellationToken)
2020-08-05 00:58:14 +02:00
.ConfigureAwait(false) ?? new List<Channels>();
2019-07-07 16:39:35 +02:00
2020-08-05 00:58:14 +02:00
if (info.ImportFavoritesOnly)
{
lineup = lineup.Where(i => i.Favorite).ToList();
2016-04-04 01:20:43 +02:00
}
2020-08-05 00:58:14 +02:00
return lineup.Where(i => !i.DRM).ToList();
2016-04-04 01:20:43 +02:00
}
2015-07-25 20:11:46 +02:00
2021-09-03 18:46:34 +02:00
protected override async Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken)
2016-04-04 01:20:43 +02:00
{
2021-09-03 18:46:34 +02:00
var lineup = await GetLineup(tuner, cancellationToken).ConfigureAwait(false);
2015-07-25 20:11:46 +02:00
2017-03-02 22:24:46 +01:00
return lineup.Select(i => new HdHomerunChannelInfo
2016-04-04 01:20:43 +02:00
{
Name = i.GuideName,
2016-11-04 00:35:19 +01:00
Number = i.GuideNumber,
2021-09-19 20:53:31 +02:00
Id = GetChannelId(i),
2016-04-04 01:20:43 +02:00
IsFavorite = i.Favorite,
2021-09-03 18:46:34 +02:00
TunerHostId = tuner.Id,
2020-12-09 01:08:41 +01:00
IsHD = i.HD,
2016-04-04 02:01:03 +02:00
AudioCodec = i.AudioCodec,
2017-01-01 21:47:54 +01:00
VideoCodec = i.VideoCodec,
2017-03-02 22:24:46 +01:00
ChannelType = ChannelType.TV,
2017-03-03 06:53:21 +01:00
IsLegacyTuner = (i.URL ?? string.Empty).StartsWith("hdhomerun", StringComparison.OrdinalIgnoreCase),
Path = i.URL
2017-03-02 22:24:46 +01:00
}).Cast<ChannelInfo>().ToList();
2015-07-20 20:32:55 +02:00
}
internal async Task<DiscoverResponse> GetModelInfo(TunerHostInfo info, bool throwAllExceptions, CancellationToken cancellationToken)
2015-07-20 20:32:55 +02:00
{
2018-09-12 19:26:21 +02:00
var cacheKey = info.Id;
2016-09-30 08:50:06 +02:00
lock (_modelCache)
{
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(cacheKey))
2016-09-30 08:50:06 +02:00
{
if (_modelCache.TryGetValue(cacheKey, out DiscoverResponse response))
2017-03-16 18:21:24 +01:00
{
return response;
}
2016-09-30 08:50:06 +02:00
}
}
2016-07-08 05:22:16 +02:00
try
2015-07-21 06:22:46 +02:00
{
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(GetApiUrl(info) + "/discover.json", HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);
2020-12-08 16:28:19 +01:00
response.EnsureSuccessStatusCode();
2020-11-17 19:43:00 +01:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
2020-12-09 01:08:41 +01:00
var discoverResponse = await JsonSerializer.DeserializeAsync<DiscoverResponse>(stream, _jsonOptions, cancellationToken)
2020-08-05 00:58:14 +02:00
.ConfigureAwait(false);
2017-10-20 18:16:56 +02:00
2020-08-05 00:58:14 +02:00
if (!string.IsNullOrEmpty(cacheKey))
{
lock (_modelCache)
2019-07-07 16:39:35 +02:00
{
2020-08-05 00:58:14 +02:00
_modelCache[cacheKey] = discoverResponse;
2017-10-20 18:16:56 +02:00
}
2016-07-08 05:22:16 +02:00
}
2020-08-05 00:58:14 +02:00
return discoverResponse;
2016-07-08 05:22:16 +02:00
}
2020-11-14 22:30:34 +01:00
catch (HttpRequestException ex)
2015-07-21 06:22:46 +02:00
{
2020-08-05 00:58:14 +02:00
if (!throwAllExceptions && ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
2016-07-08 05:22:16 +02:00
{
2020-08-19 17:52:14 +02:00
const string DefaultValue = "HDHR";
2017-03-02 21:50:09 +01:00
var response = new DiscoverResponse
{
2020-08-19 17:52:14 +02:00
ModelNumber = DefaultValue
2017-03-02 21:50:09 +01:00
};
2018-09-12 19:26:21 +02:00
if (!string.IsNullOrEmpty(cacheKey))
2016-09-30 08:50:06 +02:00
{
2017-03-03 07:38:05 +01:00
// HDHR4 doesn't have this api
lock (_modelCache)
{
2018-09-12 19:26:21 +02:00
_modelCache[cacheKey] = response;
2017-03-03 07:38:05 +01:00
}
2016-09-30 08:50:06 +02:00
}
2020-06-15 23:43:52 +02:00
2017-03-02 21:50:09 +01:00
return response;
2016-07-08 05:22:16 +02:00
}
2015-07-21 06:22:46 +02:00
2016-07-08 05:22:16 +02:00
throw;
2016-02-28 22:31:54 +01:00
}
2015-07-24 04:48:10 +02:00
}
private async Task<List<LiveTvTunerInfo>> GetTunerInfosHttp(TunerHostInfo info, CancellationToken cancellationToken)
{
var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}/tuners.html", GetApiUrl(info)), HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);
2020-11-17 19:43:00 +01:00
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
using var sr = new StreamReader(stream, System.Text.Encoding.UTF8);
var tuners = new List<LiveTvTunerInfo>();
2021-01-08 23:57:27 +01:00
await foreach (var line in sr.ReadAllLinesAsync().ConfigureAwait(false))
{
2021-01-08 23:57:27 +01:00
string stripedLine = StripXML(line);
if (stripedLine.Contains("Channel", StringComparison.Ordinal))
{
LiveTvTunerStatus status;
2021-01-08 23:57:27 +01:00
var index = stripedLine.IndexOf("Channel", StringComparison.OrdinalIgnoreCase);
var name = stripedLine.Substring(0, index - 1);
var currentChannel = stripedLine.Substring(index + 7);
if (string.Equals(currentChannel, "none", StringComparison.Ordinal))
{
status = LiveTvTunerStatus.LiveTv;
}
else
{
status = LiveTvTunerStatus.Available;
}
2019-07-07 16:39:35 +02:00
tuners.Add(new LiveTvTunerInfo
{
Name = name,
SourceType = string.IsNullOrWhiteSpace(model.ModelNumber) ? Name : model.ModelNumber,
ProgramName = currentChannel,
Status = status
});
}
}
return tuners;
}
private static string StripXML(string source)
{
if (string.IsNullOrEmpty(source))
{
return string.Empty;
}
char[] buffer = new char[source.Length];
int bufferIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
2020-06-15 23:43:52 +02:00
if (let == '>')
{
inside = false;
continue;
}
2020-06-15 23:43:52 +02:00
if (!inside)
{
2020-12-02 15:38:52 +01:00
buffer[bufferIndex++] = let;
}
}
2019-07-07 16:39:35 +02:00
return new string(buffer, 0, bufferIndex);
}
private async Task<List<LiveTvTunerInfo>> GetTunerInfosUdp(TunerHostInfo info, CancellationToken cancellationToken)
2015-07-24 04:48:10 +02:00
{
2017-03-02 21:50:09 +01:00
var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
2015-07-24 04:48:10 +02:00
2021-09-19 20:53:31 +02:00
var tuners = new List<LiveTvTunerInfo>(model.TunerCount);
var uri = new Uri(GetApiUrl(info));
2017-03-02 21:56:25 +01:00
2019-07-07 16:39:35 +02:00
using (var manager = new HdHomerunManager())
2015-07-20 20:32:55 +02:00
{
2017-03-02 21:50:09 +01:00
// Legacy HdHomeruns are IPv4 only
var ipInfo = IPAddress.Parse(uri.Host);
2017-03-02 21:50:09 +01:00
2021-09-19 20:53:31 +02:00
for (int i = 0; i < model.TunerCount; i++)
2015-07-20 20:32:55 +02:00
{
var name = string.Format(CultureInfo.InvariantCulture, "Tuner {0}", i + 1);
2021-09-19 20:53:31 +02:00
var currentChannel = "none"; // TODO: Get current channel and map back to Station Id
2017-03-02 21:50:09 +01:00
var isAvailable = await manager.CheckTunerAvailability(ipInfo, i, cancellationToken).ConfigureAwait(false);
2019-01-13 21:37:13 +01:00
var status = isAvailable ? LiveTvTunerStatus.Available : LiveTvTunerStatus.LiveTv;
2017-03-02 21:50:09 +01:00
tuners.Add(new LiveTvTunerInfo
2015-07-20 20:32:55 +02:00
{
2017-03-02 21:50:09 +01:00
Name = name,
SourceType = string.IsNullOrWhiteSpace(model.ModelNumber) ? Name : model.ModelNumber,
ProgramName = currentChannel,
Status = status
});
2015-07-20 20:32:55 +02:00
}
}
2019-07-07 16:39:35 +02:00
2017-03-02 21:50:09 +01:00
return tuners;
2015-07-20 20:32:55 +02:00
}
2015-08-19 18:43:23 +02:00
public async Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
{
var list = new List<LiveTvTunerInfo>();
foreach (var host in GetConfiguration().TunerHosts
2017-03-13 05:56:41 +01:00
.Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase)))
2015-08-19 18:43:23 +02:00
{
try
{
list.AddRange(await GetTunerInfos(host, cancellationToken).ConfigureAwait(false));
}
catch (Exception ex)
{
2018-12-20 13:11:26 +01:00
Logger.LogError(ex, "Error getting tuner info");
2015-08-19 18:43:23 +02:00
}
}
return list;
}
public async Task<List<LiveTvTunerInfo>> GetTunerInfos(TunerHostInfo info, CancellationToken cancellationToken)
{
// TODO Need faster way to determine UDP vs HTTP
2019-10-25 12:47:20 +02:00
var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
var hdHomerunChannelInfo = channels.FirstOrDefault() as HdHomerunChannelInfo;
2022-12-05 15:00:20 +01:00
if (hdHomerunChannelInfo is null || hdHomerunChannelInfo.IsLegacyTuner)
{
return await GetTunerInfosUdp(info, cancellationToken).ConfigureAwait(false);
}
return await GetTunerInfosHttp(info, cancellationToken).ConfigureAwait(false);
}
private static string GetApiUrl(TunerHostInfo info)
2015-07-20 20:32:55 +02:00
{
var url = info.Url;
2015-08-24 04:08:20 +02:00
if (string.IsNullOrWhiteSpace(url))
{
throw new ArgumentException("Invalid tuner info");
}
2015-07-20 20:32:55 +02:00
if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
url = "http://" + url;
}
return new Uri(url).AbsoluteUri.TrimEnd('/');
2015-07-20 20:32:55 +02:00
}
private static string GetHdHrIdFromChannelId(string channelId)
2017-03-26 18:26:52 +02:00
{
return channelId.Split('_')[1];
}
2017-03-02 22:24:46 +01:00
private MediaSourceInfo GetMediaSource(TunerHostInfo info, string channelId, ChannelInfo channelInfo, string profile)
2015-07-20 20:32:55 +02:00
{
2015-07-24 04:48:10 +02:00
int? width = null;
int? height = null;
bool isInterlaced = true;
2016-04-04 01:20:43 +02:00
string videoCodec = null;
2015-10-30 17:40:12 +01:00
2015-07-24 04:48:10 +02:00
int? videoBitrate = null;
2017-10-07 08:13:26 +02:00
var isHd = channelInfo.IsHD ?? true;
2015-07-24 04:48:10 +02:00
if (string.Equals(profile, "mobile", StringComparison.OrdinalIgnoreCase))
{
width = 1280;
height = 720;
isInterlaced = false;
videoCodec = "h264";
videoBitrate = 2000000;
}
else if (string.Equals(profile, "heavy", StringComparison.OrdinalIgnoreCase))
{
width = 1920;
height = 1080;
isInterlaced = false;
videoCodec = "h264";
2015-12-29 17:12:33 +01:00
videoBitrate = 15000000;
2015-07-24 04:48:10 +02:00
}
2018-09-12 19:26:21 +02:00
else if (string.Equals(profile, "internet720", StringComparison.OrdinalIgnoreCase))
{
width = 1280;
height = 720;
isInterlaced = false;
videoCodec = "h264";
videoBitrate = 8000000;
}
2015-07-24 04:48:10 +02:00
else if (string.Equals(profile, "internet540", StringComparison.OrdinalIgnoreCase))
{
width = 960;
2018-09-12 19:26:21 +02:00
height = 540;
2015-07-24 04:48:10 +02:00
isInterlaced = false;
videoCodec = "h264";
videoBitrate = 2500000;
}
else if (string.Equals(profile, "internet480", StringComparison.OrdinalIgnoreCase))
{
width = 848;
height = 480;
isInterlaced = false;
videoCodec = "h264";
videoBitrate = 2000000;
}
else if (string.Equals(profile, "internet360", StringComparison.OrdinalIgnoreCase))
{
width = 640;
height = 360;
isInterlaced = false;
videoCodec = "h264";
videoBitrate = 1500000;
}
else if (string.Equals(profile, "internet240", StringComparison.OrdinalIgnoreCase))
{
width = 432;
height = 240;
isInterlaced = false;
videoCodec = "h264";
videoBitrate = 1000000;
}
2017-09-27 16:52:01 +02:00
else
{
// This is for android tv's 1200 condition. Remove once not needed anymore so that we can avoid possible side effects of dummying up this data
2017-10-07 08:13:26 +02:00
if (isHd)
2017-09-27 16:52:01 +02:00
{
width = 1920;
height = 1080;
}
}
2015-07-24 04:48:10 +02:00
if (string.IsNullOrWhiteSpace(videoCodec))
2016-04-04 01:20:43 +02:00
{
videoCodec = channelInfo.VideoCodec;
}
2019-07-07 16:39:35 +02:00
string audioCodec = channelInfo.AudioCodec;
2016-04-04 01:20:43 +02:00
videoBitrate ??= isHd ? 15000000 : 2000000;
2019-07-07 16:39:35 +02:00
int? audioBitrate = isHd ? 448000 : 192000;
2016-04-04 01:20:43 +02:00
// normalize
if (string.Equals(videoCodec, "mpeg2", StringComparison.OrdinalIgnoreCase))
{
videoCodec = "mpeg2video";
}
2016-04-18 19:43:00 +02:00
string nal = null;
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
{
nal = "0";
}
var url = GetApiUrl(info);
2015-07-24 04:48:10 +02:00
2016-09-25 20:39:13 +02:00
var id = profile;
if (string.IsNullOrWhiteSpace(id))
{
id = "native";
}
2019-07-07 16:39:35 +02:00
id += "_" + channelId.GetMD5().ToString("N", CultureInfo.InvariantCulture) + "_" + url.GetMD5().ToString("N", CultureInfo.InvariantCulture);
2016-09-25 20:39:13 +02:00
2017-03-02 22:24:46 +01:00
var mediaSource = new MediaSourceInfo
{
Path = url,
Protocol = MediaProtocol.Udp,
2022-01-22 15:40:05 +01:00
MediaStreams = new MediaStream[]
2021-09-19 20:53:31 +02:00
{
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 = isInterlaced,
Codec = videoCodec,
Width = width,
Height = height,
BitRate = videoBitrate,
NalLengthSize = nal
},
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,
Codec = audioCodec,
BitRate = audioBitrate
}
},
2017-03-02 22:24:46 +01:00
RequiresOpening = true,
RequiresClosing = true,
BufferMs = 0,
Container = "ts",
Id = id,
SupportsDirectPlay = false,
SupportsDirectStream = true,
SupportsTranscoding = true,
2017-04-30 22:03:28 +02:00
IsInfiniteStream = true,
2017-05-19 18:39:40 +02:00
IgnoreDts = true,
2020-06-14 11:11:11 +02:00
// IgnoreIndex = true,
// ReadAtNativeFramerate = true
2017-03-02 22:24:46 +01:00
};
mediaSource.InferTotalBitrate();
return mediaSource;
}
2021-09-03 18:46:34 +02:00
protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, ChannelInfo channel, CancellationToken cancellationToken)
2015-07-24 01:40:54 +02:00
{
var list = new List<MediaSourceInfo>();
2021-09-03 18:46:34 +02:00
var channelId = channel.Id;
2016-02-24 19:45:11 +01:00
var hdhrId = GetHdHrIdFromChannelId(channelId);
2015-08-16 20:37:53 +02:00
2021-09-03 18:46:34 +02:00
if (channel is HdHomerunChannelInfo hdHomerunChannelInfo && hdHomerunChannelInfo.IsLegacyTuner)
2017-03-02 22:24:46 +01:00
{
2021-09-03 18:46:34 +02:00
list.Add(GetMediaSource(tuner, hdhrId, channel, "native"));
2017-03-02 22:24:46 +01:00
}
else
{
2021-09-03 18:46:34 +02:00
var modelInfo = await GetModelInfo(tuner, false, cancellationToken).ConfigureAwait(false);
2016-09-30 08:50:06 +02:00
2022-12-05 15:01:13 +01:00
if (modelInfo is not null && modelInfo.SupportsTranscoding)
2019-07-07 16:39:35 +02:00
{
2021-09-03 18:46:34 +02:00
if (tuner.AllowHWTranscoding)
2016-09-30 08:50:06 +02:00
{
2021-09-03 18:46:34 +02:00
list.Add(GetMediaSource(tuner, hdhrId, channel, "heavy"));
2018-09-12 19:26:21 +02:00
2021-09-03 18:46:34 +02:00
list.Add(GetMediaSource(tuner, hdhrId, channel, "internet540"));
list.Add(GetMediaSource(tuner, hdhrId, channel, "internet480"));
list.Add(GetMediaSource(tuner, hdhrId, channel, "internet360"));
list.Add(GetMediaSource(tuner, hdhrId, channel, "internet240"));
list.Add(GetMediaSource(tuner, hdhrId, channel, "mobile"));
2016-09-30 08:50:06 +02:00
}
2015-08-16 20:37:53 +02:00
2021-09-03 18:46:34 +02:00
list.Add(GetMediaSource(tuner, hdhrId, channel, "native"));
2017-03-02 22:24:46 +01:00
}
2015-07-24 04:48:10 +02:00
2017-03-02 22:24:46 +01:00
if (list.Count == 0)
{
2021-09-03 18:46:34 +02:00
list.Add(GetMediaSource(tuner, hdhrId, channel, "native"));
2017-03-02 22:24:46 +01:00
}
2016-12-08 07:53:46 +01:00
}
2015-07-24 04:48:10 +02:00
return list;
2015-07-24 01:40:54 +02:00
}
2021-09-03 18:46:34 +02:00
protected override async Task<ILiveStream> GetChannelStream(TunerHostInfo tunerHost, ChannelInfo channel, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
2015-07-24 01:40:54 +02:00
{
2021-09-03 18:46:34 +02:00
var tunerCount = tunerHost.TunerCount;
if (tunerCount > 0)
{
2021-09-03 18:46:34 +02:00
var tunerHostId = tunerHost.Id;
var liveStreams = currentLiveStreams.Where(i => string.Equals(i.TunerHostId, tunerHostId, StringComparison.OrdinalIgnoreCase));
if (liveStreams.Count() >= tunerCount)
{
throw new LiveTvConflictException("HDHomeRun simultaneous stream limit has been reached.");
}
}
2021-09-19 20:53:31 +02:00
var profile = streamId.AsSpan().LeftPart('_').ToString();
2016-09-25 20:39:13 +02:00
2021-09-03 18:46:34 +02:00
Logger.LogInformation("GetChannelStream: channel id: {0}. stream id: {1} profile: {2}", channel.Id, streamId, profile);
2015-08-16 20:37:53 +02:00
2021-09-03 18:46:34 +02:00
var hdhrId = GetHdHrIdFromChannelId(channel.Id);
2017-03-02 22:24:46 +01:00
2021-09-03 18:46:34 +02:00
var hdhomerunChannel = channel as HdHomerunChannelInfo;
2016-09-25 20:39:13 +02:00
2021-09-03 18:46:34 +02:00
var modelInfo = await GetModelInfo(tunerHost, false, cancellationToken).ConfigureAwait(false);
2017-03-26 18:26:52 +02:00
2018-09-12 19:26:21 +02:00
if (!modelInfo.SupportsTranscoding)
{
profile = "native";
}
2021-09-03 18:46:34 +02:00
var mediaSource = GetMediaSource(tunerHost, hdhrId, channel, profile);
2018-09-12 19:26:21 +02:00
2022-12-05 15:01:13 +01:00
if (hdhomerunChannel is not null && hdhomerunChannel.IsLegacyTuner)
2017-03-13 05:08:49 +01:00
{
2019-07-07 16:39:35 +02:00
return new HdHomerunUdpStream(
mediaSource,
2021-09-03 18:46:34 +02:00
tunerHost,
2019-07-07 16:39:35 +02:00
streamId,
new LegacyHdHomerunChannelCommands(hdhomerunChannel.Path),
modelInfo.TunerCount,
FileSystem,
Logger,
Config,
2019-07-07 16:39:35 +02:00
_appHost,
_streamHelper);
2017-03-02 22:27:46 +01:00
}
2017-03-27 02:27:29 +02:00
2018-09-12 19:26:21 +02:00
var enableHttpStream = true;
2017-03-27 02:27:29 +02:00
if (enableHttpStream)
2017-03-02 22:27:46 +01:00
{
2017-03-27 02:27:29 +02:00
mediaSource.Protocol = MediaProtocol.Http;
2017-03-26 21:54:50 +02:00
2021-09-03 18:46:34 +02:00
var httpUrl = channel.Path;
2017-03-27 02:27:29 +02:00
// If raw was used, the tuner doesn't support params
2017-05-22 06:54:02 +02:00
if (!string.IsNullOrWhiteSpace(profile) && !string.Equals(profile, "native", StringComparison.OrdinalIgnoreCase))
2017-03-26 21:54:50 +02:00
{
2017-03-27 02:27:29 +02:00
httpUrl += "?transcode=" + profile;
2017-03-26 21:54:50 +02:00
}
2019-08-16 19:00:32 +02:00
2017-03-27 02:27:29 +02:00
mediaSource.Path = httpUrl;
2017-03-26 21:54:50 +02:00
2019-08-16 19:00:32 +02:00
return new SharedHttpStream(
mediaSource,
2021-09-03 18:46:34 +02:00
tunerHost,
2019-08-16 19:00:32 +02:00
streamId,
FileSystem,
_httpClientFactory,
2019-08-16 19:00:32 +02:00
Logger,
Config,
2019-08-16 19:00:32 +02:00
_appHost,
_streamHelper);
2019-07-07 16:39:35 +02:00
}
return new HdHomerunUdpStream(
mediaSource,
2021-09-03 18:46:34 +02:00
tunerHost,
2019-07-07 16:39:35 +02:00
streamId,
new HdHomerunChannelCommands(hdhomerunChannel.Number, profile),
modelInfo.TunerCount,
FileSystem,
Logger,
Config,
2019-07-07 16:39:35 +02:00
_appHost,
_streamHelper);
2015-07-24 01:40:54 +02:00
}
2015-07-23 15:23:22 +02:00
public async Task Validate(TunerHostInfo info)
{
2016-09-30 08:50:06 +02:00
lock (_modelCache)
{
_modelCache.Clear();
}
2016-07-08 05:22:16 +02:00
try
2016-03-08 06:00:03 +01:00
{
2016-07-08 05:22:16 +02:00
// Test it by pulling down the lineup
2017-03-02 21:50:09 +01:00
var modelInfo = await GetModelInfo(info, true, CancellationToken.None).ConfigureAwait(false);
info.DeviceId = modelInfo.DeviceID;
2016-07-08 05:22:16 +02:00
}
2020-11-14 22:30:34 +01:00
catch (HttpRequestException ex)
2016-03-08 06:00:03 +01:00
{
2021-12-15 18:25:36 +01:00
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
2016-07-08 05:22:16 +02:00
{
// HDHR4 doesn't have this api
return;
}
2016-03-08 06:00:03 +01:00
2016-07-08 05:22:16 +02:00
throw;
2015-08-24 04:08:20 +02:00
}
2015-07-23 15:23:22 +02:00
}
2015-10-30 17:40:12 +01:00
2017-03-13 05:49:10 +01:00
public async Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
2017-03-13 05:08:49 +01:00
{
2018-09-12 19:26:21 +02:00
lock (_modelCache)
{
_modelCache.Clear();
}
2021-04-30 15:27:07 +02:00
using var timedCancellationToken = new CancellationTokenSource(discoveryDurationMs);
using var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timedCancellationToken.Token, cancellationToken);
cancellationToken = linkedCancellationTokenSource.Token;
2017-03-13 05:08:49 +01:00
var list = new List<TunerHostInfo>();
// Create udp broadcast discovery message
byte[] discBytes = { 0, 2, 0, 12, 1, 4, 255, 255, 255, 255, 2, 4, 255, 255, 255, 255, 115, 204, 125, 143 };
using (var udpClient = _socketFactory.CreateUdpBroadcastSocket(0))
{
// Need a way to set the Receive timeout on the socket otherwise this might never timeout?
try
{
2020-08-07 19:26:28 +02:00
await udpClient.SendToAsync(discBytes, 0, discBytes.Length, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 65001), cancellationToken).ConfigureAwait(false);
2017-05-24 21:12:55 +02:00
var receiveBuffer = new byte[8192];
2017-03-13 05:08:49 +01:00
while (!cancellationToken.IsCancellationRequested)
{
2017-05-24 21:12:55 +02:00
var response = await udpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);
var deviceIp = response.RemoteEndPoint.Address.ToString();
2017-03-13 05:08:49 +01:00
// check to make sure we have enough bytes received to be a valid message and make sure the 2nd byte is the discover reply byte
if (response.ReceivedBytes > 13 && response.Buffer[1] == 3)
{
var deviceAddress = "http://" + deviceIp;
var info = await TryGetTunerHostInfo(deviceAddress, cancellationToken).ConfigureAwait(false);
2022-12-05 15:01:13 +01:00
if (info is not null)
2017-03-13 05:08:49 +01:00
{
list.Add(info);
}
}
}
}
catch (OperationCanceledException)
{
}
2019-07-07 16:39:35 +02:00
catch (Exception ex)
2017-03-13 05:08:49 +01:00
{
// Socket timeout indicates all messages have been received.
2019-07-07 16:39:35 +02:00
Logger.LogError(ex, "Error while sending discovery message");
2017-03-13 05:08:49 +01:00
}
}
return list;
}
internal async Task<TunerHostInfo> TryGetTunerHostInfo(string url, CancellationToken cancellationToken)
2017-03-13 05:08:49 +01:00
{
var hostInfo = new TunerHostInfo
{
Type = Type,
Url = url
};
2019-07-07 16:39:35 +02:00
var modelInfo = await GetModelInfo(hostInfo, false, cancellationToken).ConfigureAwait(false);
2017-03-13 05:08:49 +01:00
2019-07-07 16:39:35 +02:00
hostInfo.DeviceId = modelInfo.DeviceID;
hostInfo.FriendlyName = modelInfo.FriendlyName;
hostInfo.TunerCount = modelInfo.TunerCount;
2017-03-13 05:08:49 +01:00
2019-07-07 16:39:35 +02:00
return hostInfo;
2017-03-13 05:08:49 +01:00
}
private class HdHomerunChannelInfo : ChannelInfo
{
public bool IsLegacyTuner { get; set; }
}
2015-07-20 20:32:55 +02:00
}
}