HdHomerunHost.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Text.Json;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using MediaBrowser.Common.Configuration;
  13. using MediaBrowser.Common.Extensions;
  14. using MediaBrowser.Common.Net;
  15. using MediaBrowser.Controller;
  16. using MediaBrowser.Controller.Configuration;
  17. using MediaBrowser.Controller.Library;
  18. using MediaBrowser.Controller.LiveTv;
  19. using MediaBrowser.Model.Configuration;
  20. using MediaBrowser.Model.Dto;
  21. using MediaBrowser.Model.Entities;
  22. using MediaBrowser.Model.IO;
  23. using MediaBrowser.Model.LiveTv;
  24. using MediaBrowser.Model.MediaInfo;
  25. using MediaBrowser.Model.Net;
  26. using Microsoft.Extensions.Caching.Memory;
  27. using Microsoft.Extensions.Logging;
  28. namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
  29. {
  30. public class HdHomerunHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
  31. {
  32. private readonly IHttpClientFactory _httpClientFactory;
  33. private readonly IServerApplicationHost _appHost;
  34. private readonly ISocketFactory _socketFactory;
  35. private readonly INetworkManager _networkManager;
  36. private readonly IStreamHelper _streamHelper;
  37. private readonly Dictionary<string, DiscoverResponse> _modelCache = new Dictionary<string, DiscoverResponse>();
  38. public HdHomerunHost(
  39. IServerConfigurationManager config,
  40. ILogger<HdHomerunHost> logger,
  41. IFileSystem fileSystem,
  42. IHttpClientFactory httpClientFactory,
  43. IServerApplicationHost appHost,
  44. ISocketFactory socketFactory,
  45. INetworkManager networkManager,
  46. IStreamHelper streamHelper,
  47. IMemoryCache memoryCache)
  48. : base(config, logger, fileSystem, memoryCache)
  49. {
  50. _httpClientFactory = httpClientFactory;
  51. _appHost = appHost;
  52. _socketFactory = socketFactory;
  53. _networkManager = networkManager;
  54. _streamHelper = streamHelper;
  55. }
  56. public string Name => "HD Homerun";
  57. public override string Type => "hdhomerun";
  58. protected override string ChannelIdPrefix => "hdhr_";
  59. private string GetChannelId(TunerHostInfo info, Channels i)
  60. => ChannelIdPrefix + i.GuideNumber;
  61. private async Task<List<Channels>> GetLineup(TunerHostInfo info, CancellationToken cancellationToken)
  62. {
  63. var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
  64. using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(model.LineupURL, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
  65. await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
  66. var lineup = await JsonSerializer.DeserializeAsync<List<Channels>>(stream, cancellationToken: cancellationToken)
  67. .ConfigureAwait(false) ?? new List<Channels>();
  68. if (info.ImportFavoritesOnly)
  69. {
  70. lineup = lineup.Where(i => i.Favorite).ToList();
  71. }
  72. return lineup.Where(i => !i.DRM).ToList();
  73. }
  74. private class HdHomerunChannelInfo : ChannelInfo
  75. {
  76. public bool IsLegacyTuner { get; set; }
  77. }
  78. protected override async Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
  79. {
  80. var lineup = await GetLineup(info, cancellationToken).ConfigureAwait(false);
  81. return lineup.Select(i => new HdHomerunChannelInfo
  82. {
  83. Name = i.GuideName,
  84. Number = i.GuideNumber,
  85. Id = GetChannelId(info, i),
  86. IsFavorite = i.Favorite,
  87. TunerHostId = info.Id,
  88. IsHD = i.HD == 1,
  89. AudioCodec = i.AudioCodec,
  90. VideoCodec = i.VideoCodec,
  91. ChannelType = ChannelType.TV,
  92. IsLegacyTuner = (i.URL ?? string.Empty).StartsWith("hdhomerun", StringComparison.OrdinalIgnoreCase),
  93. Path = i.URL
  94. }).Cast<ChannelInfo>().ToList();
  95. }
  96. private async Task<DiscoverResponse> GetModelInfo(TunerHostInfo info, bool throwAllExceptions, CancellationToken cancellationToken)
  97. {
  98. var cacheKey = info.Id;
  99. lock (_modelCache)
  100. {
  101. if (!string.IsNullOrEmpty(cacheKey))
  102. {
  103. if (_modelCache.TryGetValue(cacheKey, out DiscoverResponse response))
  104. {
  105. return response;
  106. }
  107. }
  108. }
  109. try
  110. {
  111. using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  112. .GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}/discover.json", GetApiUrl(info)), HttpCompletionOption.ResponseHeadersRead, cancellationToken)
  113. .ConfigureAwait(false);
  114. await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
  115. var discoverResponse = await JsonSerializer.DeserializeAsync<DiscoverResponse>(stream, cancellationToken: cancellationToken)
  116. .ConfigureAwait(false);
  117. if (!string.IsNullOrEmpty(cacheKey))
  118. {
  119. lock (_modelCache)
  120. {
  121. _modelCache[cacheKey] = discoverResponse;
  122. }
  123. }
  124. return discoverResponse;
  125. }
  126. catch (HttpRequestException ex)
  127. {
  128. if (!throwAllExceptions && ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
  129. {
  130. const string DefaultValue = "HDHR";
  131. var response = new DiscoverResponse
  132. {
  133. ModelNumber = DefaultValue
  134. };
  135. if (!string.IsNullOrEmpty(cacheKey))
  136. {
  137. // HDHR4 doesn't have this api
  138. lock (_modelCache)
  139. {
  140. _modelCache[cacheKey] = response;
  141. }
  142. }
  143. return response;
  144. }
  145. throw;
  146. }
  147. }
  148. private async Task<List<LiveTvTunerInfo>> GetTunerInfosHttp(TunerHostInfo info, CancellationToken cancellationToken)
  149. {
  150. var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
  151. using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
  152. .GetAsync(string.Format(CultureInfo.InvariantCulture, "{0}/tuners.html", GetApiUrl(info)), HttpCompletionOption.ResponseHeadersRead, cancellationToken)
  153. .ConfigureAwait(false);
  154. await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
  155. using var sr = new StreamReader(stream, System.Text.Encoding.UTF8);
  156. var tuners = new List<LiveTvTunerInfo>();
  157. while (!sr.EndOfStream)
  158. {
  159. string line = StripXML(sr.ReadLine());
  160. if (line.Contains("Channel", StringComparison.Ordinal))
  161. {
  162. LiveTvTunerStatus status;
  163. var index = line.IndexOf("Channel", StringComparison.OrdinalIgnoreCase);
  164. var name = line.Substring(0, index - 1);
  165. var currentChannel = line.Substring(index + 7);
  166. if (currentChannel != "none")
  167. {
  168. status = LiveTvTunerStatus.LiveTv;
  169. }
  170. else
  171. {
  172. status = LiveTvTunerStatus.Available;
  173. }
  174. tuners.Add(new LiveTvTunerInfo
  175. {
  176. Name = name,
  177. SourceType = string.IsNullOrWhiteSpace(model.ModelNumber) ? Name : model.ModelNumber,
  178. ProgramName = currentChannel,
  179. Status = status
  180. });
  181. }
  182. }
  183. return tuners;
  184. }
  185. private static string StripXML(string source)
  186. {
  187. if (string.IsNullOrEmpty(source))
  188. {
  189. return string.Empty;
  190. }
  191. char[] buffer = new char[source.Length];
  192. int bufferIndex = 0;
  193. bool inside = false;
  194. for (int i = 0; i < source.Length; i++)
  195. {
  196. char let = source[i];
  197. if (let == '<')
  198. {
  199. inside = true;
  200. continue;
  201. }
  202. if (let == '>')
  203. {
  204. inside = false;
  205. continue;
  206. }
  207. if (!inside)
  208. {
  209. buffer[bufferIndex] = let;
  210. bufferIndex++;
  211. }
  212. }
  213. return new string(buffer, 0, bufferIndex);
  214. }
  215. private async Task<List<LiveTvTunerInfo>> GetTunerInfosUdp(TunerHostInfo info, CancellationToken cancellationToken)
  216. {
  217. var model = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
  218. var tuners = new List<LiveTvTunerInfo>();
  219. var uri = new Uri(GetApiUrl(info));
  220. using (var manager = new HdHomerunManager())
  221. {
  222. // Legacy HdHomeruns are IPv4 only
  223. var ipInfo = IPAddress.Parse(uri.Host);
  224. for (int i = 0; i < model.TunerCount; ++i)
  225. {
  226. var name = string.Format(CultureInfo.InvariantCulture, "Tuner {0}", i + 1);
  227. var currentChannel = "none"; // @todo Get current channel and map back to Station Id
  228. var isAvailable = await manager.CheckTunerAvailability(ipInfo, i, cancellationToken).ConfigureAwait(false);
  229. var status = isAvailable ? LiveTvTunerStatus.Available : LiveTvTunerStatus.LiveTv;
  230. tuners.Add(new LiveTvTunerInfo
  231. {
  232. Name = name,
  233. SourceType = string.IsNullOrWhiteSpace(model.ModelNumber) ? Name : model.ModelNumber,
  234. ProgramName = currentChannel,
  235. Status = status
  236. });
  237. }
  238. }
  239. return tuners;
  240. }
  241. public async Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
  242. {
  243. var list = new List<LiveTvTunerInfo>();
  244. foreach (var host in GetConfiguration().TunerHosts
  245. .Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase)))
  246. {
  247. try
  248. {
  249. list.AddRange(await GetTunerInfos(host, cancellationToken).ConfigureAwait(false));
  250. }
  251. catch (Exception ex)
  252. {
  253. Logger.LogError(ex, "Error getting tuner info");
  254. }
  255. }
  256. return list;
  257. }
  258. public async Task<List<LiveTvTunerInfo>> GetTunerInfos(TunerHostInfo info, CancellationToken cancellationToken)
  259. {
  260. // TODO Need faster way to determine UDP vs HTTP
  261. var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
  262. var hdHomerunChannelInfo = channels.FirstOrDefault() as HdHomerunChannelInfo;
  263. if (hdHomerunChannelInfo == null || hdHomerunChannelInfo.IsLegacyTuner)
  264. {
  265. return await GetTunerInfosUdp(info, cancellationToken).ConfigureAwait(false);
  266. }
  267. return await GetTunerInfosHttp(info, cancellationToken).ConfigureAwait(false);
  268. }
  269. private static string GetApiUrl(TunerHostInfo info)
  270. {
  271. var url = info.Url;
  272. if (string.IsNullOrWhiteSpace(url))
  273. {
  274. throw new ArgumentException("Invalid tuner info");
  275. }
  276. if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  277. {
  278. url = "http://" + url;
  279. }
  280. return new Uri(url).AbsoluteUri.TrimEnd('/');
  281. }
  282. private class Channels
  283. {
  284. public string GuideNumber { get; set; }
  285. public string GuideName { get; set; }
  286. public string VideoCodec { get; set; }
  287. public string AudioCodec { get; set; }
  288. public string URL { get; set; }
  289. public bool Favorite { get; set; }
  290. public bool DRM { get; set; }
  291. public int HD { get; set; }
  292. }
  293. protected EncodingOptions GetEncodingOptions()
  294. {
  295. return Config.GetConfiguration<EncodingOptions>("encoding");
  296. }
  297. private static string GetHdHrIdFromChannelId(string channelId)
  298. {
  299. return channelId.Split('_')[1];
  300. }
  301. private MediaSourceInfo GetMediaSource(TunerHostInfo info, string channelId, ChannelInfo channelInfo, string profile)
  302. {
  303. int? width = null;
  304. int? height = null;
  305. bool isInterlaced = true;
  306. string videoCodec = null;
  307. int? videoBitrate = null;
  308. var isHd = channelInfo.IsHD ?? true;
  309. if (string.Equals(profile, "mobile", StringComparison.OrdinalIgnoreCase))
  310. {
  311. width = 1280;
  312. height = 720;
  313. isInterlaced = false;
  314. videoCodec = "h264";
  315. videoBitrate = 2000000;
  316. }
  317. else if (string.Equals(profile, "heavy", StringComparison.OrdinalIgnoreCase))
  318. {
  319. width = 1920;
  320. height = 1080;
  321. isInterlaced = false;
  322. videoCodec = "h264";
  323. videoBitrate = 15000000;
  324. }
  325. else if (string.Equals(profile, "internet720", StringComparison.OrdinalIgnoreCase))
  326. {
  327. width = 1280;
  328. height = 720;
  329. isInterlaced = false;
  330. videoCodec = "h264";
  331. videoBitrate = 8000000;
  332. }
  333. else if (string.Equals(profile, "internet540", StringComparison.OrdinalIgnoreCase))
  334. {
  335. width = 960;
  336. height = 540;
  337. isInterlaced = false;
  338. videoCodec = "h264";
  339. videoBitrate = 2500000;
  340. }
  341. else if (string.Equals(profile, "internet480", StringComparison.OrdinalIgnoreCase))
  342. {
  343. width = 848;
  344. height = 480;
  345. isInterlaced = false;
  346. videoCodec = "h264";
  347. videoBitrate = 2000000;
  348. }
  349. else if (string.Equals(profile, "internet360", StringComparison.OrdinalIgnoreCase))
  350. {
  351. width = 640;
  352. height = 360;
  353. isInterlaced = false;
  354. videoCodec = "h264";
  355. videoBitrate = 1500000;
  356. }
  357. else if (string.Equals(profile, "internet240", StringComparison.OrdinalIgnoreCase))
  358. {
  359. width = 432;
  360. height = 240;
  361. isInterlaced = false;
  362. videoCodec = "h264";
  363. videoBitrate = 1000000;
  364. }
  365. else
  366. {
  367. // 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
  368. if (isHd)
  369. {
  370. width = 1920;
  371. height = 1080;
  372. }
  373. }
  374. if (string.IsNullOrWhiteSpace(videoCodec))
  375. {
  376. videoCodec = channelInfo.VideoCodec;
  377. }
  378. string audioCodec = channelInfo.AudioCodec;
  379. if (!videoBitrate.HasValue)
  380. {
  381. videoBitrate = isHd ? 15000000 : 2000000;
  382. }
  383. int? audioBitrate = isHd ? 448000 : 192000;
  384. // normalize
  385. if (string.Equals(videoCodec, "mpeg2", StringComparison.OrdinalIgnoreCase))
  386. {
  387. videoCodec = "mpeg2video";
  388. }
  389. string nal = null;
  390. if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
  391. {
  392. nal = "0";
  393. }
  394. var url = GetApiUrl(info);
  395. var id = profile;
  396. if (string.IsNullOrWhiteSpace(id))
  397. {
  398. id = "native";
  399. }
  400. id += "_" + channelId.GetMD5().ToString("N", CultureInfo.InvariantCulture) + "_" + url.GetMD5().ToString("N", CultureInfo.InvariantCulture);
  401. var mediaSource = new MediaSourceInfo
  402. {
  403. Path = url,
  404. Protocol = MediaProtocol.Udp,
  405. MediaStreams = new List<MediaStream>
  406. {
  407. new MediaStream
  408. {
  409. Type = MediaStreamType.Video,
  410. // Set the index to -1 because we don't know the exact index of the video stream within the container
  411. Index = -1,
  412. IsInterlaced = isInterlaced,
  413. Codec = videoCodec,
  414. Width = width,
  415. Height = height,
  416. BitRate = videoBitrate,
  417. NalLengthSize = nal
  418. },
  419. new MediaStream
  420. {
  421. Type = MediaStreamType.Audio,
  422. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  423. Index = -1,
  424. Codec = audioCodec,
  425. BitRate = audioBitrate
  426. }
  427. },
  428. RequiresOpening = true,
  429. RequiresClosing = true,
  430. BufferMs = 0,
  431. Container = "ts",
  432. Id = id,
  433. SupportsDirectPlay = false,
  434. SupportsDirectStream = true,
  435. SupportsTranscoding = true,
  436. IsInfiniteStream = true,
  437. IgnoreDts = true,
  438. // IgnoreIndex = true,
  439. // ReadAtNativeFramerate = true
  440. };
  441. mediaSource.InferTotalBitrate();
  442. return mediaSource;
  443. }
  444. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, ChannelInfo channelInfo, CancellationToken cancellationToken)
  445. {
  446. var list = new List<MediaSourceInfo>();
  447. var channelId = channelInfo.Id;
  448. var hdhrId = GetHdHrIdFromChannelId(channelId);
  449. var hdHomerunChannelInfo = channelInfo as HdHomerunChannelInfo;
  450. var isLegacyTuner = hdHomerunChannelInfo != null && hdHomerunChannelInfo.IsLegacyTuner;
  451. if (isLegacyTuner)
  452. {
  453. list.Add(GetMediaSource(info, hdhrId, channelInfo, "native"));
  454. }
  455. else
  456. {
  457. var modelInfo = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
  458. if (modelInfo != null && modelInfo.SupportsTranscoding)
  459. {
  460. if (info.AllowHWTranscoding)
  461. {
  462. list.Add(GetMediaSource(info, hdhrId, channelInfo, "heavy"));
  463. list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet540"));
  464. list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet480"));
  465. list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet360"));
  466. list.Add(GetMediaSource(info, hdhrId, channelInfo, "internet240"));
  467. list.Add(GetMediaSource(info, hdhrId, channelInfo, "mobile"));
  468. }
  469. list.Add(GetMediaSource(info, hdhrId, channelInfo, "native"));
  470. }
  471. if (list.Count == 0)
  472. {
  473. list.Add(GetMediaSource(info, hdhrId, channelInfo, "native"));
  474. }
  475. }
  476. return list;
  477. }
  478. protected override async Task<ILiveStream> GetChannelStream(TunerHostInfo info, ChannelInfo channelInfo, string streamId, List<ILiveStream> currentLiveStreams, CancellationToken cancellationToken)
  479. {
  480. var tunerCount = info.TunerCount;
  481. if (tunerCount > 0)
  482. {
  483. var tunerHostId = info.Id;
  484. var liveStreams = currentLiveStreams.Where(i => string.Equals(i.TunerHostId, tunerHostId, StringComparison.OrdinalIgnoreCase));
  485. if (liveStreams.Count() >= tunerCount)
  486. {
  487. throw new LiveTvConflictException("HDHomeRun simultaneous stream limit has been reached.");
  488. }
  489. }
  490. var profile = streamId.Split('_')[0];
  491. Logger.LogInformation("GetChannelStream: channel id: {0}. stream id: {1} profile: {2}", channelInfo.Id, streamId, profile);
  492. var hdhrId = GetHdHrIdFromChannelId(channelInfo.Id);
  493. var hdhomerunChannel = channelInfo as HdHomerunChannelInfo;
  494. var modelInfo = await GetModelInfo(info, false, cancellationToken).ConfigureAwait(false);
  495. if (!modelInfo.SupportsTranscoding)
  496. {
  497. profile = "native";
  498. }
  499. var mediaSource = GetMediaSource(info, hdhrId, channelInfo, profile);
  500. if (hdhomerunChannel != null && hdhomerunChannel.IsLegacyTuner)
  501. {
  502. return new HdHomerunUdpStream(
  503. mediaSource,
  504. info,
  505. streamId,
  506. new LegacyHdHomerunChannelCommands(hdhomerunChannel.Path),
  507. modelInfo.TunerCount,
  508. FileSystem,
  509. Logger,
  510. Config,
  511. _appHost,
  512. _networkManager,
  513. _streamHelper);
  514. }
  515. var enableHttpStream = true;
  516. if (enableHttpStream)
  517. {
  518. mediaSource.Protocol = MediaProtocol.Http;
  519. var httpUrl = channelInfo.Path;
  520. // If raw was used, the tuner doesn't support params
  521. if (!string.IsNullOrWhiteSpace(profile) && !string.Equals(profile, "native", StringComparison.OrdinalIgnoreCase))
  522. {
  523. httpUrl += "?transcode=" + profile;
  524. }
  525. mediaSource.Path = httpUrl;
  526. return new SharedHttpStream(
  527. mediaSource,
  528. info,
  529. streamId,
  530. FileSystem,
  531. _httpClientFactory,
  532. Logger,
  533. Config,
  534. _appHost,
  535. _streamHelper);
  536. }
  537. return new HdHomerunUdpStream(
  538. mediaSource,
  539. info,
  540. streamId,
  541. new HdHomerunChannelCommands(hdhomerunChannel.Number, profile),
  542. modelInfo.TunerCount,
  543. FileSystem,
  544. Logger,
  545. Config,
  546. _appHost,
  547. _networkManager,
  548. _streamHelper);
  549. }
  550. public async Task Validate(TunerHostInfo info)
  551. {
  552. lock (_modelCache)
  553. {
  554. _modelCache.Clear();
  555. }
  556. try
  557. {
  558. // Test it by pulling down the lineup
  559. var modelInfo = await GetModelInfo(info, true, CancellationToken.None).ConfigureAwait(false);
  560. info.DeviceId = modelInfo.DeviceID;
  561. }
  562. catch (HttpRequestException ex)
  563. {
  564. if (ex.StatusCode.HasValue && ex.StatusCode.Value == System.Net.HttpStatusCode.NotFound)
  565. {
  566. // HDHR4 doesn't have this api
  567. return;
  568. }
  569. throw;
  570. }
  571. }
  572. public class DiscoverResponse
  573. {
  574. public string FriendlyName { get; set; }
  575. public string ModelNumber { get; set; }
  576. public string FirmwareName { get; set; }
  577. public string FirmwareVersion { get; set; }
  578. public string DeviceID { get; set; }
  579. public string DeviceAuth { get; set; }
  580. public string BaseURL { get; set; }
  581. public string LineupURL { get; set; }
  582. public int TunerCount { get; set; }
  583. public bool SupportsTranscoding
  584. {
  585. get
  586. {
  587. var model = ModelNumber ?? string.Empty;
  588. if (model.IndexOf("hdtc", StringComparison.OrdinalIgnoreCase) != -1)
  589. {
  590. return true;
  591. }
  592. return false;
  593. }
  594. }
  595. }
  596. public async Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
  597. {
  598. lock (_modelCache)
  599. {
  600. _modelCache.Clear();
  601. }
  602. cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(new CancellationTokenSource(discoveryDurationMs).Token, cancellationToken).Token;
  603. var list = new List<TunerHostInfo>();
  604. // Create udp broadcast discovery message
  605. byte[] discBytes = { 0, 2, 0, 12, 1, 4, 255, 255, 255, 255, 2, 4, 255, 255, 255, 255, 115, 204, 125, 143 };
  606. using (var udpClient = _socketFactory.CreateUdpBroadcastSocket(0))
  607. {
  608. // Need a way to set the Receive timeout on the socket otherwise this might never timeout?
  609. try
  610. {
  611. await udpClient.SendToAsync(discBytes, 0, discBytes.Length, new IPEndPoint(IPAddress.Parse("255.255.255.255"), 65001), cancellationToken).ConfigureAwait(false);
  612. var receiveBuffer = new byte[8192];
  613. while (!cancellationToken.IsCancellationRequested)
  614. {
  615. var response = await udpClient.ReceiveAsync(receiveBuffer, 0, receiveBuffer.Length, cancellationToken).ConfigureAwait(false);
  616. var deviceIp = response.RemoteEndPoint.Address.ToString();
  617. // 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
  618. if (response.ReceivedBytes > 13 && response.Buffer[1] == 3)
  619. {
  620. var deviceAddress = "http://" + deviceIp;
  621. var info = await TryGetTunerHostInfo(deviceAddress, cancellationToken).ConfigureAwait(false);
  622. if (info != null)
  623. {
  624. list.Add(info);
  625. }
  626. }
  627. }
  628. }
  629. catch (OperationCanceledException)
  630. {
  631. }
  632. catch (Exception ex)
  633. {
  634. // Socket timeout indicates all messages have been received.
  635. Logger.LogError(ex, "Error while sending discovery message");
  636. }
  637. }
  638. return list;
  639. }
  640. private async Task<TunerHostInfo> TryGetTunerHostInfo(string url, CancellationToken cancellationToken)
  641. {
  642. var hostInfo = new TunerHostInfo
  643. {
  644. Type = Type,
  645. Url = url
  646. };
  647. var modelInfo = await GetModelInfo(hostInfo, false, cancellationToken).ConfigureAwait(false);
  648. hostInfo.DeviceId = modelInfo.DeviceID;
  649. hostInfo.FriendlyName = modelInfo.FriendlyName;
  650. return hostInfo;
  651. }
  652. }
  653. }