M3UTunerHost.cs 6.6 KB

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