M3UTunerHost.cs 6.8 KB

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