M3UTunerHost.cs 7.9 KB

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