M3UTunerHost.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. int position = 0;
  34. string line;
  35. // Read the file and display it line by line.
  36. var file = new StreamReader(info.Url);
  37. var channels = new List<M3UChannel>();
  38. while ((line = file.ReadLine()) != null)
  39. {
  40. line = line.Trim();
  41. if (!String.IsNullOrWhiteSpace(line))
  42. {
  43. if (position == 0 && !line.StartsWith("#EXTM3U"))
  44. {
  45. throw new ApplicationException("wrong file");
  46. }
  47. if (position % 2 == 0)
  48. {
  49. if (position != 0)
  50. {
  51. channels.Last().Path = line;
  52. }
  53. else
  54. {
  55. line = line.Replace("#EXTM3U", "");
  56. line = line.Trim();
  57. var vars = line.Split(' ').ToList();
  58. foreach (var variable in vars)
  59. {
  60. var list = variable.Replace('"', ' ').Split('=');
  61. switch (list[0])
  62. {
  63. case ("id"):
  64. //_id = list[1];
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. else
  71. {
  72. if (!line.StartsWith("#EXTINF:")) { throw new ApplicationException("Bad file"); }
  73. line = line.Replace("#EXTINF:", "");
  74. var nameStart = line.LastIndexOf(',');
  75. line = line.Substring(0, nameStart);
  76. var vars = line.Split(' ').ToList();
  77. vars.RemoveAt(0);
  78. channels.Add(new M3UChannel());
  79. foreach (var variable in vars)
  80. {
  81. var list = variable.Replace('"', ' ').Split('=');
  82. switch (list[0])
  83. {
  84. case "tvg-id":
  85. channels.Last().Id = list[1];
  86. channels.Last().Number = list[1];
  87. break;
  88. case "tvg-name":
  89. channels.Last().Name = list[1];
  90. break;
  91. }
  92. }
  93. }
  94. position++;
  95. }
  96. }
  97. file.Close();
  98. return Task.FromResult((IEnumerable<ChannelInfo>)channels);
  99. }
  100. public Task<List<LiveTvTunerInfo>> GetTunerInfos(TunerHostInfo info, CancellationToken cancellationToken)
  101. {
  102. var list = new List<LiveTvTunerInfo>();
  103. list.Add(new LiveTvTunerInfo()
  104. {
  105. Name = Name,
  106. SourceType = Type,
  107. Status = LiveTvTunerStatus.Available,
  108. Id = info.Url.GetMD5().ToString("N"),
  109. Url = info.Url
  110. });
  111. return Task.FromResult(list);
  112. }
  113. private LiveTvOptions GetConfiguration()
  114. {
  115. return _config.GetConfiguration<LiveTvOptions>("livetv");
  116. }
  117. public List<TunerHostInfo> GetTunerHosts()
  118. {
  119. return GetConfiguration().TunerHosts.Where(i => string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase)).ToList();
  120. }
  121. public async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  122. {
  123. var channels = await GetChannels(info, cancellationToken).ConfigureAwait(false);
  124. var m3uchannels = channels.Cast<M3UChannel>();
  125. var channel = m3uchannels.FirstOrDefault(c => c.Id == channelId);
  126. if (channel != null)
  127. {
  128. var path = channel.Path;
  129. MediaProtocol protocol = MediaProtocol.File;
  130. if (path.StartsWith("http"))
  131. {
  132. protocol = MediaProtocol.Http;
  133. }
  134. else if (path.StartsWith("rtmp"))
  135. {
  136. protocol = MediaProtocol.Rtmp;
  137. }
  138. else if (path.StartsWith("rtsp"))
  139. {
  140. protocol = MediaProtocol.Rtsp;
  141. }
  142. return new MediaSourceInfo
  143. {
  144. Path = channel.Path,
  145. Protocol = protocol,
  146. MediaStreams = new List<MediaStream>
  147. {
  148. new MediaStream
  149. {
  150. Type = MediaStreamType.Video,
  151. // Set the index to -1 because we don't know the exact index of the video stream within the container
  152. Index = -1,
  153. IsInterlaced = true
  154. },
  155. new MediaStream
  156. {
  157. Type = MediaStreamType.Audio,
  158. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  159. Index = -1
  160. }
  161. }
  162. };
  163. }
  164. throw new ApplicationException("Host doesnt provide this channel");
  165. }
  166. class M3UChannel : ChannelInfo
  167. {
  168. public string Path { get; set; }
  169. public M3UChannel()
  170. {
  171. }
  172. }
  173. }
  174. }