M3UTunerHost.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
  46. {
  47. var result = await new M3uParser(Logger, _fileSystem, _httpClient, _appHost).Parse(info.Url, ChannelIdPrefix, info.Id, !info.EnableTvgId, cancellationToken).ConfigureAwait(false);
  48. return result.Cast<ChannelInfo>().ToList();
  49. }
  50. public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
  51. {
  52. var list = GetTunerHosts()
  53. .Select(i => new LiveTvTunerInfo()
  54. {
  55. Name = Name,
  56. SourceType = Type,
  57. Status = LiveTvTunerStatus.Available,
  58. Id = i.Url.GetMD5().ToString("N"),
  59. Url = i.Url
  60. })
  61. .ToList();
  62. return Task.FromResult(list);
  63. }
  64. protected override async Task<LiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  65. {
  66. var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
  67. var liveStream = new LiveStream(sources.First());
  68. return liveStream;
  69. }
  70. public async Task Validate(TunerHostInfo info)
  71. {
  72. using (var stream = await new M3uParser(Logger, _fileSystem, _httpClient, _appHost).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
  73. {
  74. }
  75. }
  76. protected override bool IsValidChannelId(string channelId)
  77. {
  78. if (string.IsNullOrWhiteSpace(channelId))
  79. {
  80. throw new ArgumentNullException("channelId");
  81. }
  82. return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
  83. }
  84. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
  85. {
  86. var urlHash = info.Url.GetMD5().ToString("N");
  87. var prefix = ChannelIdPrefix + urlHash;
  88. if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  89. {
  90. return null;
  91. }
  92. var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
  93. var m3uchannels = channels.Cast<M3UChannel>();
  94. var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  95. if (channel != null)
  96. {
  97. var path = channel.Path;
  98. MediaProtocol protocol = MediaProtocol.File;
  99. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  100. {
  101. protocol = MediaProtocol.Http;
  102. }
  103. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  104. {
  105. protocol = MediaProtocol.Rtmp;
  106. }
  107. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  108. {
  109. protocol = MediaProtocol.Rtsp;
  110. }
  111. else if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
  112. {
  113. protocol = MediaProtocol.Udp;
  114. }
  115. else if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
  116. {
  117. protocol = MediaProtocol.Rtmp;
  118. }
  119. var mediaSource = new MediaSourceInfo
  120. {
  121. Path = channel.Path,
  122. Protocol = protocol,
  123. MediaStreams = new List<MediaStream>
  124. {
  125. new MediaStream
  126. {
  127. Type = MediaStreamType.Video,
  128. // Set the index to -1 because we don't know the exact index of the video stream within the container
  129. Index = -1,
  130. IsInterlaced = true
  131. },
  132. new MediaStream
  133. {
  134. Type = MediaStreamType.Audio,
  135. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  136. Index = -1
  137. }
  138. },
  139. RequiresOpening = true,
  140. RequiresClosing = true,
  141. ReadAtNativeFramerate = false,
  142. Id = channel.Path.GetMD5().ToString("N"),
  143. IsInfiniteStream = true,
  144. IsRemote = true
  145. };
  146. mediaSource.InferTotalBitrate();
  147. return new List<MediaSourceInfo> { mediaSource };
  148. }
  149. return new List<MediaSourceInfo>();
  150. }
  151. protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  152. {
  153. return Task.FromResult(true);
  154. }
  155. }
  156. }