SatIpHost.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if (!string.IsNullOrWhiteSpace(tuner.M3UUrl))
  35. {
  36. return await new M3uParser(Logger, _fileSystem, _httpClient).Parse(tuner.M3UUrl, ChannelIdPrefix, tuner.Id, cancellationToken).ConfigureAwait(false);
  37. }
  38. var channels = await new ChannelScan(Logger).Scan(tuner, cancellationToken).ConfigureAwait(false);
  39. return channels;
  40. }
  41. public static string DeviceType
  42. {
  43. get { return "satip"; }
  44. }
  45. public override string Type
  46. {
  47. get { return DeviceType; }
  48. }
  49. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  50. {
  51. var urlHash = tuner.Url.GetMD5().ToString("N");
  52. var prefix = ChannelIdPrefix + urlHash;
  53. if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  54. {
  55. return null;
  56. }
  57. var channels = await GetChannels(tuner, true, cancellationToken).ConfigureAwait(false);
  58. var m3uchannels = channels.Cast<M3UChannel>();
  59. var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  60. if (channel != null)
  61. {
  62. var path = channel.Path;
  63. MediaProtocol protocol = MediaProtocol.File;
  64. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  65. {
  66. protocol = MediaProtocol.Http;
  67. }
  68. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  69. {
  70. protocol = MediaProtocol.Rtmp;
  71. }
  72. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  73. {
  74. protocol = MediaProtocol.Rtsp;
  75. }
  76. var mediaSource = new MediaSourceInfo
  77. {
  78. Path = channel.Path,
  79. Protocol = protocol,
  80. MediaStreams = new List<MediaStream>
  81. {
  82. new MediaStream
  83. {
  84. Type = MediaStreamType.Video,
  85. // Set the index to -1 because we don't know the exact index of the video stream within the container
  86. Index = -1,
  87. IsInterlaced = true
  88. },
  89. new MediaStream
  90. {
  91. Type = MediaStreamType.Audio,
  92. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  93. Index = -1
  94. }
  95. },
  96. RequiresOpening = false,
  97. RequiresClosing = false
  98. };
  99. return new List<MediaSourceInfo> { mediaSource };
  100. }
  101. return new List<MediaSourceInfo>();
  102. }
  103. protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken)
  104. {
  105. var sources = await GetChannelStreamMediaSources(tuner, channelId, cancellationToken).ConfigureAwait(false);
  106. return sources.First();
  107. }
  108. protected override async Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  109. {
  110. var updatedInfo = await SatIpDiscovery.Current.GetInfo(tuner.InfoUrl, cancellationToken).ConfigureAwait(false);
  111. return updatedInfo.TunersAvailable > 0;
  112. }
  113. protected override bool IsValidChannelId(string channelId)
  114. {
  115. return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
  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 list = new List<LiveTvTunerInfo>();
  131. for (var i = 0; i < info.Tuners; i++)
  132. {
  133. list.Add(new LiveTvTunerInfo
  134. {
  135. Name = info.FriendlyName ?? Name,
  136. SourceType = Type,
  137. Status = LiveTvTunerStatus.Available,
  138. Id = info.Url.GetMD5().ToString("N") + i.ToString(CultureInfo.InvariantCulture),
  139. Url = info.Url
  140. });
  141. }
  142. return list;
  143. }
  144. public string ApplyDuration(string streamPath, TimeSpan duration)
  145. {
  146. return streamPath;
  147. }
  148. }
  149. }