SatIpHost.cs 6.5 KB

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