M3UTunerHost.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 tunerCount = info.TunerCount;
  70. if (tunerCount > 0)
  71. {
  72. var liveStreams = await EmbyTV.EmbyTV.Current.GetLiveStreams(info, cancellationToken).ConfigureAwait(false);
  73. if (liveStreams.Count >= info.TunerCount)
  74. {
  75. throw new LiveTvConflictException();
  76. }
  77. }
  78. var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
  79. var mediaSource = sources.First();
  80. if (mediaSource.Protocol == MediaProtocol.Http && !mediaSource.RequiresLooping)
  81. {
  82. return new SharedHttpStream(mediaSource, info, streamId, FileSystem, _httpClient, Logger, Config.ApplicationPaths, _appHost, _environment);
  83. }
  84. return new LiveStream(mediaSource, info, _environment, FileSystem, Logger, Config.ApplicationPaths);
  85. }
  86. public async Task Validate(TunerHostInfo info)
  87. {
  88. using (var stream = await new M3uParser(Logger, FileSystem, _httpClient, _appHost).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
  89. {
  90. }
  91. }
  92. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
  93. {
  94. var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
  95. var channel = channels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  96. if (channel != null)
  97. {
  98. return new List<MediaSourceInfo> { CreateMediaSourceInfo(info, channel) };
  99. }
  100. return new List<MediaSourceInfo>();
  101. }
  102. protected virtual MediaSourceInfo CreateMediaSourceInfo(TunerHostInfo info, ChannelInfo channel)
  103. {
  104. var path = channel.Path;
  105. MediaProtocol protocol = MediaProtocol.File;
  106. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  107. {
  108. protocol = MediaProtocol.Http;
  109. }
  110. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  111. {
  112. protocol = MediaProtocol.Rtmp;
  113. }
  114. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  115. {
  116. protocol = MediaProtocol.Rtsp;
  117. }
  118. else if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
  119. {
  120. protocol = MediaProtocol.Udp;
  121. }
  122. else if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
  123. {
  124. protocol = MediaProtocol.Rtmp;
  125. }
  126. Uri uri;
  127. var isRemote = true;
  128. if (Uri.TryCreate(path, UriKind.Absolute, out uri))
  129. {
  130. isRemote = !_networkManager.IsInLocalNetwork(uri.Host);
  131. }
  132. var mediaSource = new MediaSourceInfo
  133. {
  134. Path = path,
  135. Protocol = protocol,
  136. MediaStreams = new List<MediaStream>
  137. {
  138. new MediaStream
  139. {
  140. Type = MediaStreamType.Video,
  141. // Set the index to -1 because we don't know the exact index of the video stream within the container
  142. Index = -1,
  143. IsInterlaced = true
  144. },
  145. new MediaStream
  146. {
  147. Type = MediaStreamType.Audio,
  148. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  149. Index = -1
  150. }
  151. },
  152. RequiresOpening = true,
  153. RequiresClosing = true,
  154. RequiresLooping = info.EnableStreamLooping,
  155. ReadAtNativeFramerate = false,
  156. Id = channel.Path.GetMD5().ToString("N"),
  157. IsInfiniteStream = true,
  158. IsRemote = isRemote,
  159. IgnoreDts = true
  160. };
  161. mediaSource.InferTotalBitrate();
  162. return mediaSource;
  163. }
  164. protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  165. {
  166. return Task.FromResult(true);
  167. }
  168. public Task<List<TunerHostInfo>> DiscoverDevices(int discoveryDurationMs, CancellationToken cancellationToken)
  169. {
  170. return Task.FromResult(new List<TunerHostInfo>());
  171. }
  172. }
  173. }