M3UTunerHost.cs 7.2 KB

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