2015-07-20 11:32:55 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2016-02-24 11:45:11 -07:00
|
|
|
using MediaBrowser.Common.Extensions;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2016-09-25 11:39:13 -07:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.LiveTv;
|
2015-10-13 19:41:46 -07:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2015-10-30 09:40:12 -07:00
|
|
|
using MediaBrowser.Model.Configuration;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.LiveTv;
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2016-07-07 20:22:16 -07:00
|
|
|
using MediaBrowser.Model.Net;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2017-03-26 17:27:29 -07:00
|
|
|
using MediaBrowser.Model.System;
|
2019-01-13 12:22:00 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2016-02-18 23:20:18 -07:00
|
|
|
public class HdHomerunHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
private readonly IHttpClient _httpClient;
|
2016-09-25 11:39:13 -07:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2017-03-02 13:50:09 -07:00
|
|
|
private readonly ISocketFactory _socketFactory;
|
2017-03-02 22:53:21 -07:00
|
|
|
private readonly INetworkManager _networkManager;
|
2017-03-26 17:27:29 -07:00
|
|
|
private readonly IEnvironmentInfo _environment;
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2017-07-05 11:30:12 -07:00
|
|
|
public HdHomerunHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient, IServerApplicationHost appHost, ISocketFactory socketFactory, INetworkManager networkManager, IEnvironmentInfo environment) : base(config, logger, jsonSerializer, mediaEncoder, fileSystem)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
2016-09-25 11:39:13 -07:00
|
|
|
_appHost = appHost;
|
2017-03-02 13:50:09 -07:00
|
|
|
_socketFactory = socketFactory;
|
2017-03-02 22:53:21 -07:00
|
|
|
_networkManager = networkManager;
|
2017-03-26 17:27:29 -07:00
|
|
|
_environment = environment;
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
public string Name => "HD Homerun";
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
public override string Type => DeviceType;
|
2015-07-23 09:32:34 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
public static string DeviceType => "hdhomerun";
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
protected override string ChannelIdPrefix => "hdhr_";
|
2015-08-16 11:37:53 -07:00
|
|
|
|
2016-02-25 13:29:38 -07:00
|
|
|
private string GetChannelId(TunerHostInfo info, Channels i)
|
|
|
|
{
|
2016-11-03 16:35:19 -07:00
|
|
|
var id = ChannelIdPrefix + i.GuideNumber;
|
2016-02-25 13:29:38 -07:00
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2017-02-18 20:46:09 -07:00
|
|
|
private async Task<List<Channels>> GetLineup(TunerHostInfo info, CancellationToken cancellationToken)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2017-03-05 19:32:56 -07:00
|
|
|
var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
var options = new HttpRequestOptions
|
|
|
|
{
|
2017-03-05 19:32:56 -07:00
|
|
|
Url = model.LineupURL,
|
2016-10-06 11:55:01 -07:00
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
BufferContent = false
|
2015-07-20 11:32:55 -07:00
|
|
|
};
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var response = await _httpClient.SendAsync(options, "GET").ConfigureAwait(false))
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var stream = response.Content)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
var lineup = await JsonSerializer.DeserializeFromStreamAsync<List<Channels>>(stream).ConfigureAwait(false) ?? new List<Channels>();
|
2017-10-20 09:16:56 -07:00
|
|
|
|
|
|
|
if (info.ImportFavoritesOnly)
|
|
|
|
{
|
|
|
|
lineup = lineup.Where(i => i.Favorite).ToList();
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
|
2017-10-20 09:16:56 -07:00
|
|
|
return lineup.Where(i => !i.DRM).ToList();
|
|
|
|
}
|
2016-04-03 16:20:43 -07:00
|
|
|
}
|
|
|
|
}
|
2015-07-25 11:11:46 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
private class HdHomerunChannelInfo : ChannelInfo
|
|
|
|
{
|
2017-03-02 14:27:46 -07:00
|
|
|
public bool IsLegacyTuner { get; set; }
|
2017-03-02 14:24:46 -07:00
|
|
|
}
|
|
|
|
|
2017-02-23 12:13:26 -07:00
|
|
|
protected override async Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
|
2016-04-03 16:20:43 -07:00
|
|
|
{
|
|
|
|
var lineup = await GetLineup(info, cancellationToken).ConfigureAwait(false);
|
2015-07-25 11:11:46 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
return lineup.Select(i => new HdHomerunChannelInfo
|
2016-04-03 16:20:43 -07:00
|
|
|
{
|
|
|
|
Name = i.GuideName,
|
2016-11-03 16:35:19 -07:00
|
|
|
Number = i.GuideNumber,
|
2016-04-03 16:20:43 -07:00
|
|
|
Id = GetChannelId(info, i),
|
|
|
|
IsFavorite = i.Favorite,
|
2016-04-03 17:01:03 -07:00
|
|
|
TunerHostId = info.Id,
|
|
|
|
IsHD = i.HD == 1,
|
|
|
|
AudioCodec = i.AudioCodec,
|
2017-01-01 13:47:54 -07:00
|
|
|
VideoCodec = i.VideoCodec,
|
2017-03-02 14:24:46 -07:00
|
|
|
ChannelType = ChannelType.TV,
|
2017-03-02 22:53:21 -07:00
|
|
|
IsLegacyTuner = (i.URL ?? string.Empty).StartsWith("hdhomerun", StringComparison.OrdinalIgnoreCase),
|
2017-11-08 13:23:39 -07:00
|
|
|
Path = i.URL
|
2017-02-23 12:13:26 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
}).Cast<ChannelInfo>().ToList();
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2016-10-07 08:08:13 -07:00
|
|
|
private readonly Dictionary<string, DiscoverResponse> _modelCache = new Dictionary<string, DiscoverResponse>();
|
2017-03-02 13:50:09 -07:00
|
|
|
private async Task<DiscoverResponse> GetModelInfo(TunerHostInfo info, bool throwAllExceptions, CancellationToken cancellationToken)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
var cacheKey = info.Id;
|
|
|
|
|
2016-09-29 23:50:06 -07:00
|
|
|
lock (_modelCache)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!string.IsNullOrEmpty(cacheKey))
|
2016-09-29 23:50:06 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
DiscoverResponse response;
|
|
|
|
if (_modelCache.TryGetValue(cacheKey, out response))
|
2017-03-16 10:21:24 -07:00
|
|
|
{
|
|
|
|
return response;
|
|
|
|
}
|
2016-09-29 23:50:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:22:16 -07:00
|
|
|
try
|
2015-07-20 21:22:46 -07:00
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var response = await _httpClient.SendAsync(new HttpRequestOptions()
|
2016-07-07 20:22:16 -07:00
|
|
|
{
|
2017-11-08 13:23:39 -07:00
|
|
|
Url = string.Format("{0}/discover.json", GetApiUrl(info)),
|
2016-07-07 20:22:16 -07:00
|
|
|
CancellationToken = cancellationToken,
|
2018-09-12 10:26:21 -07:00
|
|
|
TimeoutMs = Convert.ToInt32(TimeSpan.FromSeconds(10).TotalMilliseconds),
|
2016-10-06 11:55:01 -07:00
|
|
|
BufferContent = false
|
2017-02-18 20:46:09 -07:00
|
|
|
|
2017-10-20 09:16:56 -07:00
|
|
|
}, "GET").ConfigureAwait(false))
|
2016-07-07 20:22:16 -07:00
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var stream = response.Content)
|
2016-09-29 23:50:06 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
var discoverResponse = await JsonSerializer.DeserializeFromStreamAsync<DiscoverResponse>(stream).ConfigureAwait(false);
|
2017-10-20 09:16:56 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!string.IsNullOrEmpty(cacheKey))
|
2017-03-02 23:38:05 -07:00
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
lock (_modelCache)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
_modelCache[cacheKey] = discoverResponse;
|
2017-10-20 09:16:56 -07:00
|
|
|
}
|
2017-03-02 23:38:05 -07:00
|
|
|
}
|
2016-09-29 23:50:06 -07:00
|
|
|
|
2017-10-20 09:16:56 -07:00
|
|
|
return discoverResponse;
|
|
|
|
}
|
2016-07-07 20:22:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (HttpException ex)
|
2015-07-20 21:22:46 -07:00
|
|
|
{
|
2017-03-02 13:50:09 -07:00
|
|
|
if (!throwAllExceptions && ex.StatusCode.HasValue && ex.StatusCode.Value == System.Net.HttpStatusCode.NotFound)
|
2016-07-07 20:22:16 -07:00
|
|
|
{
|
2016-09-29 23:50:06 -07:00
|
|
|
var defaultValue = "HDHR";
|
2017-03-02 13:50:09 -07:00
|
|
|
var response = new DiscoverResponse
|
|
|
|
{
|
|
|
|
ModelNumber = defaultValue
|
|
|
|
};
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!string.IsNullOrEmpty(cacheKey))
|
2016-09-29 23:50:06 -07:00
|
|
|
{
|
2017-03-02 23:38:05 -07:00
|
|
|
// HDHR4 doesn't have this api
|
|
|
|
lock (_modelCache)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
_modelCache[cacheKey] = response;
|
2017-03-02 23:38:05 -07:00
|
|
|
}
|
2016-09-29 23:50:06 -07:00
|
|
|
}
|
2017-03-02 13:50:09 -07:00
|
|
|
return response;
|
2016-07-07 20:22:16 -07:00
|
|
|
}
|
2015-07-20 21:22:46 -07:00
|
|
|
|
2016-07-07 20:22:16 -07:00
|
|
|
throw;
|
2016-02-28 14:31:54 -07:00
|
|
|
}
|
2015-07-23 19:48:10 -07:00
|
|
|
}
|
|
|
|
|
2017-11-08 13:23:39 -07:00
|
|
|
private async Task<List<LiveTvTunerInfo>> GetTunerInfosHttp(TunerHostInfo info, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
using (var stream = await _httpClient.Get(new HttpRequestOptions()
|
|
|
|
{
|
|
|
|
Url = string.Format("{0}/tuners.html", GetApiUrl(info)),
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
TimeoutMs = Convert.ToInt32(TimeSpan.FromSeconds(5).TotalMilliseconds),
|
|
|
|
BufferContent = false
|
|
|
|
}))
|
|
|
|
{
|
|
|
|
var tuners = new List<LiveTvTunerInfo>();
|
|
|
|
using (var sr = new StreamReader(stream, System.Text.Encoding.UTF8))
|
|
|
|
{
|
|
|
|
while (!sr.EndOfStream)
|
|
|
|
{
|
|
|
|
string line = StripXML(sr.ReadLine());
|
|
|
|
if (line.Contains("Channel"))
|
|
|
|
{
|
|
|
|
LiveTvTunerStatus status;
|
|
|
|
var index = line.IndexOf("Channel", StringComparison.OrdinalIgnoreCase);
|
|
|
|
var name = line.Substring(0, index - 1);
|
|
|
|
var currentChannel = line.Substring(index + 7);
|
|
|
|
if (currentChannel != "none") { status = LiveTvTunerStatus.LiveTv; } else { status = LiveTvTunerStatus.Available; }
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (let == '>')
|
|
|
|
{
|
|
|
|
inside = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!inside)
|
|
|
|
{
|
|
|
|
buffer[bufferIndex] = let;
|
|
|
|
bufferIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new string(buffer, 0, bufferIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<List<LiveTvTunerInfo>> GetTunerInfosUdp(TunerHostInfo info, CancellationToken cancellationToken)
|
2015-07-23 19:48:10 -07:00
|
|
|
{
|
2017-03-02 13:50:09 -07:00
|
|
|
var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
|
2015-07-23 19:48:10 -07:00
|
|
|
|
2017-03-02 13:50:09 -07:00
|
|
|
var tuners = new List<LiveTvTunerInfo>();
|
2017-02-18 20:46:09 -07:00
|
|
|
|
2017-11-08 13:23:39 -07:00
|
|
|
var uri = new Uri(GetApiUrl(info));
|
2017-03-02 13:56:25 -07:00
|
|
|
|
2017-10-23 12:14:11 -07:00
|
|
|
using (var manager = new HdHomerunManager(_socketFactory, Logger))
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2017-03-02 13:50:09 -07:00
|
|
|
// Legacy HdHomeruns are IPv4 only
|
2017-03-03 13:16:43 -07:00
|
|
|
var ipInfo = _networkManager.ParseIpAddress(uri.Host);
|
2017-03-02 13:50:09 -07:00
|
|
|
|
|
|
|
for (int i = 0; i < model.TunerCount; ++i)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
var name = string.Format("Tuner {0}", i + 1);
|
2017-03-02 13:50:09 -07:00
|
|
|
var currentChannel = "none"; /// @todo Get current channel and map back to Station Id
|
|
|
|
var isAvailable = await manager.CheckTunerAvailability(ipInfo, i, cancellationToken).ConfigureAwait(false);
|
|
|
|
LiveTvTunerStatus status = isAvailable ? LiveTvTunerStatus.Available : LiveTvTunerStatus.LiveTv;
|
|
|
|
tuners.Add(new LiveTvTunerInfo
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2017-03-02 13:50:09 -07:00
|
|
|
Name = name,
|
|
|
|
SourceType = string.IsNullOrWhiteSpace(model.ModelNumber) ? Name : model.ModelNumber,
|
|
|
|
ProgramName = currentChannel,
|
|
|
|
Status = status
|
|
|
|
});
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|
2017-03-02 13:50:09 -07:00
|
|
|
return tuners;
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2015-08-19 09:43:23 -07:00
|
|
|
public async Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var list = new List<LiveTvTunerInfo>();
|
|
|
|
|
|
|
|
foreach (var host in GetConfiguration().TunerHosts
|
2017-03-12 21:56:41 -07:00
|
|
|
.Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase)))
|
2015-08-19 09:43:23 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
list.AddRange(await GetTunerInfos(host, cancellationToken).ConfigureAwait(false));
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 05:11:26 -07:00
|
|
|
Logger.LogError(ex, "Error getting tuner info");
|
2015-08-19 09:43:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-11-08 13:23:39 -07:00
|
|
|
public async Task<List<LiveTvTunerInfo>> GetTunerInfos(TunerHostInfo info, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
// TODO Need faster way to determine UDP vs HTTP
|
|
|
|
var channels = await GetChannels(info, true, cancellationToken);
|
|
|
|
|
|
|
|
var hdHomerunChannelInfo = channels.FirstOrDefault() as HdHomerunChannelInfo;
|
|
|
|
|
|
|
|
if (hdHomerunChannelInfo == null || hdHomerunChannelInfo.IsLegacyTuner)
|
|
|
|
{
|
|
|
|
return await GetTunerInfosUdp(info, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await GetTunerInfosHttp(info, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static string GetApiUrl(TunerHostInfo info)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
|
|
|
var url = info.Url;
|
|
|
|
|
2015-08-23 19:08:20 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(url))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Invalid tuner info");
|
|
|
|
}
|
|
|
|
|
2015-07-20 11:32:55 -07:00
|
|
|
if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
url = "http://" + url;
|
|
|
|
}
|
|
|
|
|
2017-11-08 13:23:39 -07:00
|
|
|
return new Uri(url).AbsoluteUri.TrimEnd('/');
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private class Channels
|
|
|
|
{
|
|
|
|
public string GuideNumber { get; set; }
|
|
|
|
public string GuideName { get; set; }
|
2016-04-03 16:20:43 -07:00
|
|
|
public string VideoCodec { get; set; }
|
|
|
|
public string AudioCodec { get; set; }
|
2015-07-20 11:32:55 -07:00
|
|
|
public string URL { get; set; }
|
|
|
|
public bool Favorite { get; set; }
|
|
|
|
public bool DRM { get; set; }
|
2016-04-03 16:20:43 -07:00
|
|
|
public int HD { get; set; }
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2017-03-26 09:26:52 -07:00
|
|
|
protected EncodingOptions GetEncodingOptions()
|
|
|
|
{
|
|
|
|
return Config.GetConfiguration<EncodingOptions>("encoding");
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static string GetHdHrIdFromChannelId(string channelId)
|
2017-03-26 09:26:52 -07:00
|
|
|
{
|
|
|
|
return channelId.Split('_')[1];
|
|
|
|
}
|
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
private MediaSourceInfo GetMediaSource(TunerHostInfo info, string channelId, ChannelInfo channelInfo, string profile)
|
2015-07-20 11:32:55 -07:00
|
|
|
{
|
2015-07-23 19:48:10 -07:00
|
|
|
int? width = null;
|
|
|
|
int? height = null;
|
|
|
|
bool isInterlaced = true;
|
2016-04-03 16:20:43 -07:00
|
|
|
string videoCodec = null;
|
2017-03-02 14:24:46 -07:00
|
|
|
string audioCodec = null;
|
2015-10-30 09:40:12 -07:00
|
|
|
|
2015-07-23 19:48:10 -07:00
|
|
|
int? videoBitrate = null;
|
2016-04-14 12:12:00 -07:00
|
|
|
int? audioBitrate = null;
|
2015-07-23 19:48:10 -07:00
|
|
|
|
2017-10-06 23:13:26 -07:00
|
|
|
var isHd = channelInfo.IsHD ?? true;
|
|
|
|
|
2015-07-23 19:48:10 -07: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 09:12:33 -07:00
|
|
|
videoBitrate = 15000000;
|
2015-07-23 19:48:10 -07:00
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
else if (string.Equals(profile, "internet720", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
width = 1280;
|
|
|
|
height = 720;
|
|
|
|
isInterlaced = false;
|
|
|
|
videoCodec = "h264";
|
|
|
|
videoBitrate = 8000000;
|
|
|
|
}
|
2015-07-23 19:48:10 -07:00
|
|
|
else if (string.Equals(profile, "internet540", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2016-04-03 23:39:40 -07:00
|
|
|
width = 960;
|
2018-09-12 10:26:21 -07:00
|
|
|
height = 540;
|
2015-07-23 19:48:10 -07: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 07:52:01 -07: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-06 23:13:26 -07:00
|
|
|
if (isHd)
|
2017-09-27 07:52:01 -07:00
|
|
|
{
|
|
|
|
width = 1920;
|
|
|
|
height = 1080;
|
|
|
|
}
|
|
|
|
}
|
2015-07-23 19:48:10 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
if (channelInfo != null)
|
2016-04-03 16:20:43 -07:00
|
|
|
{
|
2016-09-18 13:38:38 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(videoCodec))
|
2016-04-03 16:20:43 -07:00
|
|
|
{
|
2017-03-02 14:24:46 -07:00
|
|
|
videoCodec = channelInfo.VideoCodec;
|
2016-09-18 13:38:38 -07:00
|
|
|
}
|
2017-03-02 14:24:46 -07:00
|
|
|
audioCodec = channelInfo.AudioCodec;
|
2016-04-03 16:20:43 -07:00
|
|
|
|
2016-09-18 13:38:38 -07:00
|
|
|
if (!videoBitrate.HasValue)
|
|
|
|
{
|
2017-10-06 23:13:26 -07:00
|
|
|
videoBitrate = isHd ? 15000000 : 2000000;
|
2016-04-03 16:20:43 -07:00
|
|
|
}
|
2017-10-06 23:13:26 -07:00
|
|
|
audioBitrate = isHd ? 448000 : 192000;
|
2016-04-03 16:20:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// normalize
|
|
|
|
if (string.Equals(videoCodec, "mpeg2", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
videoCodec = "mpeg2video";
|
|
|
|
}
|
|
|
|
|
2016-04-18 10:43:00 -07:00
|
|
|
string nal = null;
|
|
|
|
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
nal = "0";
|
|
|
|
}
|
|
|
|
|
2017-11-08 13:23:39 -07:00
|
|
|
var url = GetApiUrl(info);
|
2015-07-23 19:48:10 -07:00
|
|
|
|
2016-09-25 11:39:13 -07:00
|
|
|
var id = profile;
|
|
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
|
|
{
|
|
|
|
id = "native";
|
|
|
|
}
|
2017-03-30 10:58:38 -07:00
|
|
|
id += "_" + channelId.GetMD5().ToString("N") + "_" + url.GetMD5().ToString("N");
|
2016-09-25 11:39:13 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
var mediaSource = new MediaSourceInfo
|
|
|
|
{
|
|
|
|
Path = url,
|
|
|
|
Protocol = MediaProtocol.Udp,
|
|
|
|
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 = 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
|
|
|
|
}
|
|
|
|
},
|
|
|
|
RequiresOpening = true,
|
|
|
|
RequiresClosing = true,
|
|
|
|
BufferMs = 0,
|
|
|
|
Container = "ts",
|
|
|
|
Id = id,
|
|
|
|
SupportsDirectPlay = false,
|
|
|
|
SupportsDirectStream = true,
|
|
|
|
SupportsTranscoding = true,
|
2017-04-30 13:03:28 -07:00
|
|
|
IsInfiniteStream = true,
|
2017-05-19 09:39:40 -07:00
|
|
|
IgnoreDts = true,
|
2017-05-23 09:44:11 -07:00
|
|
|
//IgnoreIndex = true,
|
2017-05-31 21:51:43 -07:00
|
|
|
//ReadAtNativeFramerate = true
|
2017-03-02 14:24:46 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
mediaSource.InferTotalBitrate();
|
|
|
|
|
|
|
|
return mediaSource;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, ChannelInfo channelInfo, CancellationToken cancellationToken)
|
2015-07-23 16:40:54 -07:00
|
|
|
{
|
|
|
|
var list = new List<MediaSourceInfo>();
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var channelId = channelInfo.Id;
|
2016-02-24 11:45:11 -07:00
|
|
|
var hdhrId = GetHdHrIdFromChannelId(channelId);
|
2015-08-16 11:37:53 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
var hdHomerunChannelInfo = channelInfo as HdHomerunChannelInfo;
|
|
|
|
|
2017-03-02 14:27:46 -07:00
|
|
|
var isLegacyTuner = hdHomerunChannelInfo != null && hdHomerunChannelInfo.IsLegacyTuner;
|
2016-12-07 23:53:46 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
if (isLegacyTuner)
|
|
|
|
{
|
2017-03-26 09:26:52 -07:00
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "native"));
|
2017-03-02 14:24:46 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
2015-07-23 19:48:10 -07:00
|
|
|
{
|
2017-03-02 14:24:46 -07:00
|
|
|
var modelInfo = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
|
2016-09-29 23:50:06 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (modelInfo != null && modelInfo.SupportsTranscoding)
|
2016-09-29 23:50:06 -07:00
|
|
|
{
|
2017-03-02 14:24:46 -07:00
|
|
|
if (info.AllowHWTranscoding)
|
|
|
|
{
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "heavy"));
|
|
|
|
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet540"));
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet480"));
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet360"));
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet240"));
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "mobile"));
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "native"));
|
2016-09-29 23:50:06 -07:00
|
|
|
}
|
2015-07-23 19:48:10 -07:00
|
|
|
}
|
2017-03-02 14:24:46 -07:00
|
|
|
catch
|
|
|
|
{
|
2015-08-16 11:37:53 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
}
|
2015-07-23 19:48:10 -07:00
|
|
|
|
2017-03-02 14:24:46 -07:00
|
|
|
if (list.Count == 0)
|
|
|
|
{
|
|
|
|
list.Add(GetMediaSource(info, hdhrId, channelInfo, "native"));
|
|
|
|
}
|
2016-12-07 23:53:46 -07:00
|
|
|
}
|
|
|
|
|
2015-07-23 19:48:10 -07:00
|
|
|
return list;
|
2015-07-23 16:40:54 -07:00
|
|
|
}
|
|
|
|
|
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-07-23 16:40:54 -07:00
|
|
|
{
|
2016-09-25 11:39:13 -07:00
|
|
|
var profile = streamId.Split('_')[0];
|
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
Logger.LogInformation("GetChannelStream: channel id: {0}. stream id: {1} profile: {2}", channelInfo.Id, streamId, profile);
|
2015-08-16 11:37:53 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var hdhrId = GetHdHrIdFromChannelId(channelInfo.Id);
|
2017-03-02 14:24:46 -07:00
|
|
|
|
2017-03-02 14:27:46 -07:00
|
|
|
var hdhomerunChannel = channelInfo as HdHomerunChannelInfo;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
2017-03-26 09:26:52 -07:00
|
|
|
var modelInfo = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (!modelInfo.SupportsTranscoding)
|
|
|
|
{
|
|
|
|
profile = "native";
|
|
|
|
}
|
|
|
|
|
|
|
|
var mediaSource = GetMediaSource(info, hdhrId, channelInfo, profile);
|
|
|
|
|
2017-03-02 14:27:46 -07:00
|
|
|
if (hdhomerunChannel != null && hdhomerunChannel.IsLegacyTuner)
|
2017-03-12 21:08:49 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
return new HdHomerunUdpStream(mediaSource, info, streamId, new LegacyHdHomerunChannelCommands(hdhomerunChannel.Path), modelInfo.TunerCount, FileSystem, _httpClient, Logger, Config.ApplicationPaths, _appHost, _socketFactory, _networkManager);
|
2017-03-02 14:27:46 -07:00
|
|
|
}
|
2017-03-26 17:27:29 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var enableHttpStream = true;
|
2017-03-26 17:27:29 -07:00
|
|
|
if (enableHttpStream)
|
2017-03-02 14:27:46 -07:00
|
|
|
{
|
2017-03-26 17:27:29 -07:00
|
|
|
mediaSource.Protocol = MediaProtocol.Http;
|
2017-03-26 12:54:50 -07:00
|
|
|
|
2017-11-08 13:23:39 -07:00
|
|
|
var httpUrl = channelInfo.Path;
|
2017-03-26 17:27:29 -07:00
|
|
|
|
|
|
|
// If raw was used, the tuner doesn't support params
|
2017-05-21 21:54:02 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(profile) && !string.Equals(profile, "native", StringComparison.OrdinalIgnoreCase))
|
2017-03-26 12:54:50 -07:00
|
|
|
{
|
2017-03-26 17:27:29 -07:00
|
|
|
httpUrl += "?transcode=" + profile;
|
2017-03-26 12:54:50 -07:00
|
|
|
}
|
2017-03-26 17:27:29 -07:00
|
|
|
mediaSource.Path = httpUrl;
|
2017-03-26 12:54:50 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
return new SharedHttpStream(mediaSource, info, streamId, FileSystem, _httpClient, Logger, Config.ApplicationPaths, _appHost);
|
2017-03-02 14:27:46 -07:00
|
|
|
}
|
2017-03-26 17:27:29 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
return new HdHomerunUdpStream(mediaSource, info, streamId, new HdHomerunChannelCommands(hdhomerunChannel.Number, profile), modelInfo.TunerCount, FileSystem, _httpClient, Logger, Config.ApplicationPaths, _appHost, _socketFactory, _networkManager);
|
2015-07-23 16:40:54 -07:00
|
|
|
}
|
2015-07-23 06:23:22 -07:00
|
|
|
|
|
|
|
public async Task Validate(TunerHostInfo info)
|
|
|
|
{
|
2016-09-29 23:50:06 -07:00
|
|
|
lock (_modelCache)
|
|
|
|
{
|
|
|
|
_modelCache.Clear();
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:22:16 -07:00
|
|
|
try
|
2016-03-07 22:00:03 -07:00
|
|
|
{
|
2016-07-07 20:22:16 -07:00
|
|
|
// Test it by pulling down the lineup
|
2017-03-02 13:50:09 -07:00
|
|
|
var modelInfo = await GetModelInfo(info, true, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
info.DeviceId = modelInfo.DeviceID;
|
2016-07-07 20:22:16 -07:00
|
|
|
}
|
|
|
|
catch (HttpException ex)
|
2016-03-07 22:00:03 -07:00
|
|
|
{
|
2016-07-07 20:22:16 -07:00
|
|
|
if (ex.StatusCode.HasValue && ex.StatusCode.Value == System.Net.HttpStatusCode.NotFound)
|
|
|
|
{
|
|
|
|
// HDHR4 doesn't have this api
|
|
|
|
return;
|
|
|
|
}
|
2016-03-07 22:00:03 -07:00
|
|
|
|
2016-07-07 20:22:16 -07:00
|
|
|
throw;
|
2015-08-23 19:08:20 -07:00
|
|
|
}
|
2015-07-23 06:23:22 -07:00
|
|
|
}
|
2015-10-30 09:40:12 -07:00
|
|
|
|
2016-02-28 14:31:54 -07:00
|
|
|
public class DiscoverResponse
|
|
|
|
{
|
|
|
|
public string FriendlyName { get; set; }
|
|
|
|
public string ModelNumber { get; set; }
|
|
|
|
public string FirmwareName { get; set; }
|
|
|
|
public string FirmwareVersion { get; set; }
|
|
|
|
public string DeviceID { get; set; }
|
|
|
|
public string DeviceAuth { get; set; }
|
|
|
|
public string BaseURL { get; set; }
|
|
|
|
public string LineupURL { get; set; }
|
2017-03-02 13:50:09 -07:00
|
|
|
public int TunerCount { get; set; }
|
2017-03-16 10:21:24 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public bool SupportsTranscoding
|
2017-03-16 10:21:24 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
get
|
|
|
|
{
|
|
|
|
var model = ModelNumber ?? string.Empty;
|
|
|
|
|
|
|
|
if ((model.IndexOf("hdtc", StringComparison.OrdinalIgnoreCase) != -1))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-16 10:21:24 -07:00
|
|
|
}
|
2016-02-28 14:31:54 -07:00
|
|
|
}
|
2017-03-12 21:08:49 -07:00
|
|
|
|
2017-03-12 21:49:10 -07:00
|
|
|
public async Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
|
2017-03-12 21:08:49 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
lock (_modelCache)
|
|
|
|
{
|
|
|
|
_modelCache.Clear();
|
|
|
|
}
|
|
|
|
|
2017-03-12 21:49:10 -07:00
|
|
|
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(new CancellationTokenSource(discoveryDurationMs).Token, cancellationToken).Token;
|
2017-03-12 21:08:49 -07: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
|
|
|
|
{
|
2017-05-24 12:12:55 -07:00
|
|
|
await udpClient.SendToAsync(discBytes, 0, discBytes.Length, new IpEndPointInfo(new IpAddressInfo("255.255.255.255", IpAddressFamily.InterNetwork), 65001), cancellationToken);
|
|
|
|
var receiveBuffer = new byte[8192];
|
|
|
|
|
2017-03-12 21:08:49 -07:00
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
2017-05-24 12:12:55 -07:00
|
|
|
var response = await udpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);
|
2017-03-12 21:08:49 -07:00
|
|
|
var deviceIp = response.RemoteEndPoint.IpAddress.Address;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
if (info != null)
|
|
|
|
{
|
|
|
|
list.Add(info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// Socket timeout indicates all messages have been received.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<TunerHostInfo> TryGetTunerHostInfo(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var hostInfo = new TunerHostInfo
|
|
|
|
{
|
|
|
|
Type = Type,
|
|
|
|
Url = url
|
|
|
|
};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var modelInfo = await GetModelInfo(hostInfo, false, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
hostInfo.DeviceId = modelInfo.DeviceID;
|
2017-03-13 13:42:21 -07:00
|
|
|
hostInfo.FriendlyName = modelInfo.FriendlyName;
|
2017-03-12 21:08:49 -07:00
|
|
|
|
|
|
|
return hostInfo;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// logged at lower levels
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2015-07-20 11:32:55 -07:00
|
|
|
}
|
|
|
|
}
|