HdHomerunHost.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.LiveTv;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.LiveTv;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.MediaInfo;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.HdHomerun
  18. {
  19. public class HdHomerunHost : ITunerHost
  20. {
  21. private readonly IHttpClient _httpClient;
  22. private readonly ILogger _logger;
  23. private readonly IJsonSerializer _jsonSerializer;
  24. private readonly IConfigurationManager _config;
  25. public HdHomerunHost(IHttpClient httpClient, ILogger logger, IJsonSerializer jsonSerializer, IConfigurationManager config)
  26. {
  27. _httpClient = httpClient;
  28. _logger = logger;
  29. _jsonSerializer = jsonSerializer;
  30. _config = config;
  31. }
  32. public string Name
  33. {
  34. get { return "HD Homerun"; }
  35. }
  36. public string Type
  37. {
  38. get { return DeviceType; }
  39. }
  40. public static string DeviceType
  41. {
  42. get { return "hdhomerun"; }
  43. }
  44. public async Task<IEnumerable<ChannelInfo>> GetChannels(TunerHostInfo info, CancellationToken cancellationToken)
  45. {
  46. var options = new HttpRequestOptions
  47. {
  48. Url = string.Format("{0}/lineup.json", GetApiUrl(info, false)),
  49. CancellationToken = cancellationToken
  50. };
  51. using (var stream = await _httpClient.Get(options))
  52. {
  53. var root = _jsonSerializer.DeserializeFromStream<List<Channels>>(stream);
  54. if (root != null)
  55. {
  56. return root.Select(i => new ChannelInfo
  57. {
  58. Name = i.GuideName,
  59. Number = i.GuideNumber.ToString(CultureInfo.InvariantCulture),
  60. Id = i.GuideNumber.ToString(CultureInfo.InvariantCulture),
  61. IsFavorite = i.Favorite
  62. });
  63. }
  64. return new List<ChannelInfo>();
  65. }
  66. }
  67. private async Task<string> GetModelInfo(TunerHostInfo info, CancellationToken cancellationToken)
  68. {
  69. string model = null;
  70. using (var stream = await _httpClient.Get(new HttpRequestOptions()
  71. {
  72. Url = string.Format("{0}/", GetApiUrl(info, false)),
  73. CancellationToken = cancellationToken,
  74. CacheLength = TimeSpan.FromDays(1),
  75. CacheMode = CacheMode.Unconditional
  76. }))
  77. {
  78. using (var sr = new StreamReader(stream, System.Text.Encoding.UTF8))
  79. {
  80. while (!sr.EndOfStream)
  81. {
  82. string line = StripXML(sr.ReadLine());
  83. if (line.StartsWith("Model:")) { model = line.Replace("Model: ", ""); }
  84. //if (line.StartsWith("Device ID:")) { deviceID = line.Replace("Device ID: ", ""); }
  85. //if (line.StartsWith("Firmware:")) { firmware = line.Replace("Firmware: ", ""); }
  86. }
  87. }
  88. }
  89. return null;
  90. }
  91. public async Task<List<LiveTvTunerInfo>> GetTunerInfos(TunerHostInfo info, CancellationToken cancellationToken)
  92. {
  93. string model = await GetModelInfo(info, cancellationToken).ConfigureAwait(false);
  94. using (var stream = await _httpClient.Get(new HttpRequestOptions()
  95. {
  96. Url = string.Format("{0}/tuners.html", GetApiUrl(info, false)),
  97. CancellationToken = cancellationToken
  98. }))
  99. {
  100. var tuners = new List<LiveTvTunerInfo>();
  101. using (var sr = new StreamReader(stream, System.Text.Encoding.UTF8))
  102. {
  103. while (!sr.EndOfStream)
  104. {
  105. string line = StripXML(sr.ReadLine());
  106. if (line.Contains("Channel"))
  107. {
  108. LiveTvTunerStatus status;
  109. var index = line.IndexOf("Channel", StringComparison.OrdinalIgnoreCase);
  110. var name = line.Substring(0, index - 1);
  111. var currentChannel = line.Substring(index + 7);
  112. if (currentChannel != "none") { status = LiveTvTunerStatus.LiveTv; } else { status = LiveTvTunerStatus.Available; }
  113. tuners.Add(new LiveTvTunerInfo()
  114. {
  115. Name = name,
  116. SourceType = string.IsNullOrWhiteSpace(model) ? Name : model,
  117. ProgramName = currentChannel,
  118. Status = status
  119. });
  120. }
  121. }
  122. }
  123. return tuners;
  124. }
  125. }
  126. private string GetApiUrl(TunerHostInfo info, bool isPlayback)
  127. {
  128. var url = info.Url;
  129. if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  130. {
  131. url = "http://" + url;
  132. }
  133. var uri = new Uri(url);
  134. if (isPlayback)
  135. {
  136. var builder = new UriBuilder(uri);
  137. builder.Port = 5004;
  138. uri = builder.Uri;
  139. }
  140. return uri.AbsoluteUri.TrimEnd('/');
  141. }
  142. private static string StripXML(string source)
  143. {
  144. char[] buffer = new char[source.Length];
  145. int bufferIndex = 0;
  146. bool inside = false;
  147. for (int i = 0; i < source.Length; i++)
  148. {
  149. char let = source[i];
  150. if (let == '<')
  151. {
  152. inside = true;
  153. continue;
  154. }
  155. if (let == '>')
  156. {
  157. inside = false;
  158. continue;
  159. }
  160. if (!inside)
  161. {
  162. buffer[bufferIndex] = let;
  163. bufferIndex++;
  164. }
  165. }
  166. return new string(buffer, 0, bufferIndex);
  167. }
  168. private class Channels
  169. {
  170. public string GuideNumber { get; set; }
  171. public string GuideName { get; set; }
  172. public string URL { get; set; }
  173. public bool Favorite { get; set; }
  174. public bool DRM { get; set; }
  175. }
  176. private LiveTvOptions GetConfiguration()
  177. {
  178. return _config.GetConfiguration<LiveTvOptions>("livetv");
  179. }
  180. public List<TunerHostInfo> GetTunerHosts()
  181. {
  182. return GetConfiguration().TunerHosts.Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase)).ToList();
  183. }
  184. private MediaSourceInfo GetMediaSource(TunerHostInfo info, string channelId, string profile)
  185. {
  186. int? width = null;
  187. int? height = null;
  188. bool isInterlaced = true;
  189. var videoCodec = "mpeg2video";
  190. int? videoBitrate = null;
  191. if (string.Equals(profile, "mobile", StringComparison.OrdinalIgnoreCase))
  192. {
  193. width = 1280;
  194. height = 720;
  195. isInterlaced = false;
  196. videoCodec = "h264";
  197. videoBitrate = 2000000;
  198. }
  199. else if (string.Equals(profile, "heavy", StringComparison.OrdinalIgnoreCase))
  200. {
  201. width = 1920;
  202. height = 1080;
  203. isInterlaced = false;
  204. videoCodec = "h264";
  205. videoBitrate = 8000000;
  206. }
  207. else if (string.Equals(profile, "internet720", StringComparison.OrdinalIgnoreCase))
  208. {
  209. width = 1280;
  210. height = 720;
  211. isInterlaced = false;
  212. videoCodec = "h264";
  213. videoBitrate = 5000000;
  214. }
  215. else if (string.Equals(profile, "internet540", StringComparison.OrdinalIgnoreCase))
  216. {
  217. width = 1280;
  218. height = 720;
  219. isInterlaced = false;
  220. videoCodec = "h264";
  221. videoBitrate = 2500000;
  222. }
  223. else if (string.Equals(profile, "internet480", StringComparison.OrdinalIgnoreCase))
  224. {
  225. width = 848;
  226. height = 480;
  227. isInterlaced = false;
  228. videoCodec = "h264";
  229. videoBitrate = 2000000;
  230. }
  231. else if (string.Equals(profile, "internet360", StringComparison.OrdinalIgnoreCase))
  232. {
  233. width = 640;
  234. height = 360;
  235. isInterlaced = false;
  236. videoCodec = "h264";
  237. videoBitrate = 1500000;
  238. }
  239. else if (string.Equals(profile, "internet240", StringComparison.OrdinalIgnoreCase))
  240. {
  241. width = 432;
  242. height = 240;
  243. isInterlaced = false;
  244. videoCodec = "h264";
  245. videoBitrate = 1000000;
  246. }
  247. var url = GetApiUrl(info, true) + "/auto/v" + channelId;
  248. if (!string.IsNullOrWhiteSpace(profile) && !string.Equals(profile, "native", StringComparison.OrdinalIgnoreCase))
  249. {
  250. url += "?transcode=" + profile;
  251. }
  252. var mediaSource = new MediaSourceInfo
  253. {
  254. Path = url,
  255. Protocol = MediaProtocol.Http,
  256. MediaStreams = new List<MediaStream>
  257. {
  258. new MediaStream
  259. {
  260. Type = MediaStreamType.Video,
  261. // Set the index to -1 because we don't know the exact index of the video stream within the container
  262. Index = -1,
  263. IsInterlaced = isInterlaced,
  264. Codec = videoCodec,
  265. Width = width,
  266. Height = height,
  267. BitRate = videoBitrate
  268. },
  269. new MediaStream
  270. {
  271. Type = MediaStreamType.Audio,
  272. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  273. Index = -1,
  274. Codec = "ac3",
  275. BitRate = 128000
  276. }
  277. },
  278. RequiresOpening = false,
  279. RequiresClosing = false,
  280. BufferMs = 1000
  281. };
  282. return mediaSource;
  283. }
  284. public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
  285. {
  286. var list = new List<MediaSourceInfo>();
  287. list.Add(GetMediaSource(info, channelId, "native"));
  288. try
  289. {
  290. string model = await GetModelInfo(info, cancellationToken).ConfigureAwait(false);
  291. model = model ?? string.Empty;
  292. if (model.IndexOf("hdtc", StringComparison.OrdinalIgnoreCase) != -1)
  293. {
  294. list.Insert(0, GetMediaSource(info, channelId, "heavy"));
  295. list.Add(GetMediaSource(info, channelId, "internet480"));
  296. list.Add(GetMediaSource(info, channelId, "internet360"));
  297. list.Add(GetMediaSource(info, channelId, "internet240"));
  298. list.Add(GetMediaSource(info, channelId, "mobile"));
  299. }
  300. }
  301. catch (Exception ex)
  302. {
  303. }
  304. return list;
  305. }
  306. public async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  307. {
  308. return GetMediaSource(info, channelId, streamId);
  309. }
  310. public async Task Validate(TunerHostInfo info)
  311. {
  312. await GetChannels(info, CancellationToken.None).ConfigureAwait(false);
  313. }
  314. }
  315. }