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