M3UTunerHost.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Controller.LiveTv;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.LiveTv;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.MediaInfo;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using MediaBrowser.Common.IO;
  16. using MediaBrowser.Common.Net;
  17. using MediaBrowser.Model.Serialization;
  18. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
  19. {
  20. public class M3UTunerHost : BaseTunerHost, ITunerHost
  21. {
  22. private readonly IFileSystem _fileSystem;
  23. private IHttpClient _httpClient;
  24. public M3UTunerHost(IConfigurationManager config, ILogger logger, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer jsonSerializer)
  25. : base(config, logger, jsonSerializer)
  26. {
  27. _fileSystem = fileSystem;
  28. _httpClient = httpClient;
  29. }
  30. public override string Type
  31. {
  32. get { return "m3u"; }
  33. }
  34. public string Name
  35. {
  36. get { return "M3U Tuner"; }
  37. }
  38. private const string ChannelIdPrefix = "m3u_";
  39. protected override async Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
  40. {
  41. var url = info.Url;
  42. var urlHash = url.GetMD5().ToString("N");
  43. string line;
  44. // Read the file and display it line by line.
  45. using (var file = new StreamReader(await GetListingsStream(info, cancellationToken).ConfigureAwait(false)))
  46. {
  47. var channels = new List<M3UChannel>();
  48. string channnelName = null;
  49. string channelNumber = null;
  50. while ((line = file.ReadLine()) != null)
  51. {
  52. line = line.Trim();
  53. if (string.IsNullOrWhiteSpace(line))
  54. {
  55. continue;
  56. }
  57. if (line.StartsWith("#EXTM3U", StringComparison.OrdinalIgnoreCase))
  58. {
  59. continue;
  60. }
  61. if (line.StartsWith("#EXTINF:", StringComparison.OrdinalIgnoreCase))
  62. {
  63. var parts = line.Split(new[] { ':' }, 2).Last().Split(new[] { ',' }, 2);
  64. channelNumber = parts[0];
  65. channnelName = parts[1];
  66. }
  67. else if (!string.IsNullOrWhiteSpace(channelNumber))
  68. {
  69. channels.Add(new M3UChannel
  70. {
  71. Name = channnelName,
  72. Number = channelNumber,
  73. Id = ChannelIdPrefix + urlHash + channelNumber,
  74. Path = line
  75. });
  76. channelNumber = null;
  77. channnelName = null;
  78. }
  79. }
  80. return channels;
  81. }
  82. }
  83. public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
  84. {
  85. var list = GetConfiguration().TunerHosts
  86. .Where(i => i.IsEnabled && string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
  87. .Select(i => new LiveTvTunerInfo()
  88. {
  89. Name = Name,
  90. SourceType = Type,
  91. Status = LiveTvTunerStatus.Available,
  92. Id = i.Url.GetMD5().ToString("N"),
  93. Url = i.Url
  94. })
  95. .ToList();
  96. return Task.FromResult(list);
  97. }
  98. protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  99. {
  100. var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
  101. return sources.First();
  102. }
  103. class M3UChannel : ChannelInfo
  104. {
  105. public string Path { get; set; }
  106. public M3UChannel()
  107. {
  108. }
  109. }
  110. public async Task Validate(TunerHostInfo info)
  111. {
  112. using (var stream = await GetListingsStream(info, CancellationToken.None).ConfigureAwait(false))
  113. {
  114. }
  115. }
  116. protected override bool IsValidChannelId(string channelId)
  117. {
  118. return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
  119. }
  120. private Task<Stream> GetListingsStream(TunerHostInfo info, CancellationToken cancellationToken)
  121. {
  122. if (info.Url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  123. {
  124. return _httpClient.Get(info.Url, cancellationToken);
  125. }
  126. return Task.FromResult(_fileSystem.OpenRead(info.Url));
  127. }
  128. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
  129. {
  130. var urlHash = info.Url.GetMD5().ToString("N");
  131. var prefix = ChannelIdPrefix + urlHash;
  132. if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  133. {
  134. return null;
  135. }
  136. //channelId = channelId.Substring(prefix.Length);
  137. var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
  138. var m3uchannels = channels.Cast<M3UChannel>();
  139. var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  140. if (channel != null)
  141. {
  142. var path = channel.Path;
  143. MediaProtocol protocol = MediaProtocol.File;
  144. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  145. {
  146. protocol = MediaProtocol.Http;
  147. }
  148. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  149. {
  150. protocol = MediaProtocol.Rtmp;
  151. }
  152. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  153. {
  154. protocol = MediaProtocol.Rtsp;
  155. }
  156. var mediaSource = new MediaSourceInfo
  157. {
  158. Path = channel.Path,
  159. Protocol = protocol,
  160. MediaStreams = new List<MediaStream>
  161. {
  162. new MediaStream
  163. {
  164. Type = MediaStreamType.Video,
  165. // Set the index to -1 because we don't know the exact index of the video stream within the container
  166. Index = -1,
  167. IsInterlaced = true
  168. },
  169. new MediaStream
  170. {
  171. Type = MediaStreamType.Audio,
  172. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  173. Index = -1
  174. }
  175. },
  176. RequiresOpening = false,
  177. RequiresClosing = false
  178. };
  179. return new List<MediaSourceInfo> { mediaSource };
  180. }
  181. return new List<MediaSourceInfo> { };
  182. }
  183. }
  184. }