M3UTunerHost.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using MediaBrowser.Common.IO;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Common.Net;
  17. using MediaBrowser.Controller;
  18. using MediaBrowser.Controller.Configuration;
  19. using MediaBrowser.Controller.IO;
  20. using MediaBrowser.Controller.MediaEncoding;
  21. using MediaBrowser.Model.Serialization;
  22. namespace Emby.Server.Implementations.LiveTv.TunerHosts
  23. {
  24. public class M3UTunerHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
  25. {
  26. private readonly IFileSystem _fileSystem;
  27. private readonly IHttpClient _httpClient;
  28. private readonly IServerApplicationHost _appHost;
  29. public M3UTunerHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient, IServerApplicationHost appHost)
  30. : base(config, logger, jsonSerializer, mediaEncoder)
  31. {
  32. _fileSystem = fileSystem;
  33. _httpClient = httpClient;
  34. _appHost = appHost;
  35. }
  36. public override string Type
  37. {
  38. get { return "m3u"; }
  39. }
  40. public string Name
  41. {
  42. get { return "M3U Tuner"; }
  43. }
  44. private const string ChannelIdPrefix = "m3u_";
  45. protected override async Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
  46. {
  47. return await new M3uParser(Logger, _fileSystem, _httpClient, _appHost).Parse(info.Url, ChannelIdPrefix, info.Id, cancellationToken).ConfigureAwait(false);
  48. }
  49. public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
  50. {
  51. var list = GetTunerHosts()
  52. .Select(i => new LiveTvTunerInfo()
  53. {
  54. Name = Name,
  55. SourceType = Type,
  56. Status = LiveTvTunerStatus.Available,
  57. Id = i.Url.GetMD5().ToString("N"),
  58. Url = i.Url
  59. })
  60. .ToList();
  61. return Task.FromResult(list);
  62. }
  63. protected override async Task<LiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  64. {
  65. var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
  66. var liveStream = new LiveStream(sources.First());
  67. return liveStream;
  68. }
  69. public async Task Validate(TunerHostInfo info)
  70. {
  71. using (var stream = await new M3uParser(Logger, _fileSystem, _httpClient, _appHost).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
  72. {
  73. }
  74. }
  75. protected override bool IsValidChannelId(string channelId)
  76. {
  77. if (string.IsNullOrWhiteSpace(channelId))
  78. {
  79. throw new ArgumentNullException("channelId");
  80. }
  81. return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
  82. }
  83. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
  84. {
  85. var urlHash = info.Url.GetMD5().ToString("N");
  86. var prefix = ChannelIdPrefix + urlHash;
  87. if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  88. {
  89. return null;
  90. }
  91. var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
  92. var m3uchannels = channels.Cast<M3UChannel>();
  93. var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  94. if (channel != null)
  95. {
  96. var path = channel.Path;
  97. MediaProtocol protocol = MediaProtocol.File;
  98. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  99. {
  100. protocol = MediaProtocol.Http;
  101. }
  102. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  103. {
  104. protocol = MediaProtocol.Rtmp;
  105. }
  106. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  107. {
  108. protocol = MediaProtocol.Rtsp;
  109. }
  110. else if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
  111. {
  112. protocol = MediaProtocol.Udp;
  113. }
  114. else if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
  115. {
  116. protocol = MediaProtocol.Rtmp;
  117. }
  118. var mediaSource = new MediaSourceInfo
  119. {
  120. Path = channel.Path,
  121. Protocol = protocol,
  122. MediaStreams = new List<MediaStream>
  123. {
  124. new MediaStream
  125. {
  126. Type = MediaStreamType.Video,
  127. // Set the index to -1 because we don't know the exact index of the video stream within the container
  128. Index = -1,
  129. IsInterlaced = true
  130. },
  131. new MediaStream
  132. {
  133. Type = MediaStreamType.Audio,
  134. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  135. Index = -1
  136. }
  137. },
  138. RequiresOpening = true,
  139. RequiresClosing = true,
  140. ReadAtNativeFramerate = false,
  141. Id = channel.Path.GetMD5().ToString("N"),
  142. IsInfiniteStream = true,
  143. SupportsDirectStream = false
  144. };
  145. return new List<MediaSourceInfo> { mediaSource };
  146. }
  147. return new List<MediaSourceInfo>();
  148. }
  149. protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  150. {
  151. return Task.FromResult(true);
  152. }
  153. }
  154. }