ChannelScan.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using IniParser;
  9. using IniParser.Model;
  10. using MediaBrowser.Controller.LiveTv;
  11. using MediaBrowser.Model.LiveTv;
  12. using MediaBrowser.Model.Logging;
  13. using MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp.Rtsp;
  14. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp
  15. {
  16. public class ChannelScan
  17. {
  18. private readonly ILogger _logger;
  19. public ChannelScan(ILogger logger)
  20. {
  21. _logger = logger;
  22. }
  23. public async Task<List<ChannelInfo>> Scan(TunerHostInfo info, CancellationToken cancellationToken)
  24. {
  25. var ini = info.SourceA.Split('|')[1];
  26. var resource = GetType().Assembly.GetManifestResourceNames().FirstOrDefault(i => i.EndsWith(ini, StringComparison.OrdinalIgnoreCase));
  27. _logger.Info("Opening ini file {0}", resource);
  28. var list = new List<ChannelInfo>();
  29. using (var stream = GetType().Assembly.GetManifestResourceStream(resource))
  30. {
  31. using (var reader = new StreamReader(stream))
  32. {
  33. var parser = new StreamIniDataParser();
  34. var data = parser.ReadData(reader);
  35. var count = GetInt(data, "DVB", "0", 0);
  36. _logger.Info("DVB Count: {0}", count);
  37. var index = 1;
  38. var source = "1";
  39. while (index <= count)
  40. {
  41. cancellationToken.ThrowIfCancellationRequested();
  42. using (var rtspSession = new RtspSession(info.Url, _logger))
  43. {
  44. float percent = count == 0 ? 0 : (float)(index) / count;
  45. percent = Math.Max(percent * 100, 100);
  46. //SetControlPropertyThreadSafe(pgbSearchResult, "Value", (int)percent);
  47. var strArray = data["DVB"][index.ToString(CultureInfo.InvariantCulture)].Split(',');
  48. string tuning;
  49. if (strArray[4] == "S2")
  50. {
  51. tuning = string.Format("src={0}&freq={1}&pol={2}&sr={3}&fec={4}&msys=dvbs2&mtype={5}&plts=on&ro=0.35&pids=0,16,17,18,20", source, strArray[0], strArray[1].ToLower(), strArray[2].ToLower(), strArray[3], strArray[5].ToLower());
  52. }
  53. else
  54. {
  55. tuning = string.Format("src={0}&freq={1}&pol={2}&sr={3}&fec={4}&msys=dvbs&mtype={5}&pids=0,16,17,18,20", source, strArray[0], strArray[1].ToLower(), strArray[2], strArray[3], strArray[5].ToLower());
  56. }
  57. rtspSession.Setup(tuning, "unicast");
  58. rtspSession.Play(string.Empty);
  59. int signallevel;
  60. int signalQuality;
  61. rtspSession.Describe(out signallevel, out signalQuality);
  62. await Task.Delay(500).ConfigureAwait(false);
  63. index++;
  64. }
  65. }
  66. }
  67. }
  68. return list;
  69. }
  70. private int GetInt(IniData data, string s1, string s2, int defaultValue)
  71. {
  72. var value = data[s1][s2];
  73. int numericValue;
  74. if (int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out numericValue))
  75. {
  76. return numericValue;
  77. }
  78. return defaultValue;
  79. }
  80. }
  81. public class SatChannel
  82. {
  83. // TODO: Add properties
  84. }
  85. }