SatIpHost.cs 6.2 KB

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