M3UTunerHost.cs 7.9 KB

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