M3UTunerHost.cs 6.8 KB

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