SatIpHost.cs 6.7 KB

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