SatIpHost.cs 6.0 KB

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