M3UTunerHost.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  118. {
  119. var channels = await GetChannels(info, cancellationToken).ConfigureAwait(false);
  120. var m3uchannels = channels.Cast<M3UChannel>();
  121. var channel = m3uchannels.FirstOrDefault(c => c.Id == channelId);
  122. if (channel != null)
  123. {
  124. var path = channel.Path;
  125. MediaProtocol protocol = MediaProtocol.File;
  126. if (path.StartsWith("http"))
  127. {
  128. protocol = MediaProtocol.Http;
  129. }
  130. else if (path.StartsWith("rtmp"))
  131. {
  132. protocol = MediaProtocol.Rtmp;
  133. }
  134. else if (path.StartsWith("rtsp"))
  135. {
  136. protocol = MediaProtocol.Rtsp;
  137. }
  138. return new MediaSourceInfo
  139. {
  140. Path = channel.Path,
  141. Protocol = protocol,
  142. MediaStreams = new List<MediaStream>
  143. {
  144. new MediaStream
  145. {
  146. Type = MediaStreamType.Video,
  147. // Set the index to -1 because we don't know the exact index of the video stream within the container
  148. Index = -1,
  149. IsInterlaced = true
  150. },
  151. new MediaStream
  152. {
  153. Type = MediaStreamType.Audio,
  154. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  155. Index = -1
  156. }
  157. },
  158. RequiresOpening = false,
  159. RequiresClosing = false
  160. };
  161. }
  162. throw new ApplicationException("Host doesnt provide this channel");
  163. }
  164. class M3UChannel : ChannelInfo
  165. {
  166. public string Path { get; set; }
  167. public M3UChannel()
  168. {
  169. }
  170. }
  171. public async Task Validate(TunerHostInfo info)
  172. {
  173. if (!File.Exists(info.Url))
  174. {
  175. throw new FileNotFoundException();
  176. }
  177. }
  178. public Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
  179. {
  180. throw new NotImplementedException();
  181. }
  182. }
  183. }