HdHomerunHost.cs 28 KB

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