M3UTunerHost.cs 7.4 KB

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