SatIpHost.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using CommonIO;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Common.Extensions;
  10. using MediaBrowser.Common.Net;
  11. using MediaBrowser.Controller.LiveTv;
  12. using MediaBrowser.Controller.MediaEncoding;
  13. using MediaBrowser.Model.Dto;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.LiveTv;
  16. using MediaBrowser.Model.Logging;
  17. using MediaBrowser.Model.MediaInfo;
  18. using MediaBrowser.Model.Serialization;
  19. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts.SatIp
  20. {
  21. public class SatIpHost : BaseTunerHost, ITunerHost
  22. {
  23. private readonly IFileSystem _fileSystem;
  24. private readonly IHttpClient _httpClient;
  25. public SatIpHost(IConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient)
  26. : base(config, logger, jsonSerializer, mediaEncoder)
  27. {
  28. _fileSystem = fileSystem;
  29. _httpClient = httpClient;
  30. }
  31. private const string ChannelIdPrefix = "sat_";
  32. protected override async Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken)
  33. {
  34. var satInfo = (SatIpTunerHostInfo)tuner;
  35. return await new M3uParser(Logger, _fileSystem, _httpClient).Parse(satInfo.M3UUrl, ChannelIdPrefix, tuner.Id, cancellationToken).ConfigureAwait(false);
  36. }
  37. public static string DeviceType
  38. {
  39. get { return "satip"; }
  40. }
  41. public override string Type
  42. {
  43. get { return DeviceType; }
  44. }
  45. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  46. {
  47. var urlHash = tuner.Url.GetMD5().ToString("N");
  48. var prefix = ChannelIdPrefix + urlHash;
  49. if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  50. {
  51. return null;
  52. }
  53. var channels = await GetChannels(tuner, true, cancellationToken).ConfigureAwait(false);
  54. var m3uchannels = channels.Cast<M3UChannel>();
  55. var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  56. if (channel != null)
  57. {
  58. var path = channel.Path;
  59. MediaProtocol protocol = MediaProtocol.File;
  60. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  61. {
  62. protocol = MediaProtocol.Http;
  63. }
  64. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  65. {
  66. protocol = MediaProtocol.Rtmp;
  67. }
  68. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  69. {
  70. protocol = MediaProtocol.Rtsp;
  71. }
  72. var mediaSource = new MediaSourceInfo
  73. {
  74. Path = channel.Path,
  75. Protocol = protocol,
  76. MediaStreams = new List<MediaStream>
  77. {
  78. new MediaStream
  79. {
  80. Type = MediaStreamType.Video,
  81. // Set the index to -1 because we don't know the exact index of the video stream within the container
  82. Index = -1,
  83. IsInterlaced = true
  84. },
  85. new MediaStream
  86. {
  87. Type = MediaStreamType.Audio,
  88. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  89. Index = -1
  90. }
  91. },
  92. RequiresOpening = false,
  93. RequiresClosing = false
  94. };
  95. return new List<MediaSourceInfo> { mediaSource };
  96. }
  97. return new List<MediaSourceInfo> { };
  98. }
  99. protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken)
  100. {
  101. var sources = await GetChannelStreamMediaSources(tuner, channelId, cancellationToken).ConfigureAwait(false);
  102. return sources.First();
  103. }
  104. protected override async Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  105. {
  106. var updatedInfo = await SatIpDiscovery.Current.GetInfo(tuner.Url, cancellationToken).ConfigureAwait(false);
  107. return updatedInfo.TunersAvailable > 0;
  108. }
  109. protected override bool IsValidChannelId(string channelId)
  110. {
  111. return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
  112. }
  113. protected override List<TunerHostInfo> GetTunerHosts()
  114. {
  115. return SatIpDiscovery.Current.DiscoveredHosts;
  116. }
  117. public string Name
  118. {
  119. get { return "Sat IP"; }
  120. }
  121. public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
  122. {
  123. var list = GetTunerHosts()
  124. .SelectMany(i => GetTunerInfos(i, cancellationToken))
  125. .ToList();
  126. return Task.FromResult(list);
  127. }
  128. public List<LiveTvTunerInfo> GetTunerInfos(TunerHostInfo info, CancellationToken cancellationToken)
  129. {
  130. var satInfo = (SatIpTunerHostInfo)info;
  131. var list = new List<LiveTvTunerInfo>();
  132. for (var i = 0; i < satInfo.Tuners; i++)
  133. {
  134. list.Add(new LiveTvTunerInfo
  135. {
  136. Name = satInfo.FriendlyName ?? Name,
  137. SourceType = Type,
  138. Status = LiveTvTunerStatus.Available,
  139. Id = info.Url.GetMD5().ToString("N") + i.ToString(CultureInfo.InvariantCulture),
  140. Url = info.Url
  141. });
  142. }
  143. return list;
  144. }
  145. }
  146. }