M3UTunerHost.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. public M3UTunerHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient, IServerApplicationHost appHost, IEnvironmentInfo environment) : base(config, logger, jsonSerializer, mediaEncoder, fileSystem)
  28. {
  29. _httpClient = httpClient;
  30. _appHost = appHost;
  31. _environment = environment;
  32. }
  33. public override string Type
  34. {
  35. get { return "m3u"; }
  36. }
  37. public string Name
  38. {
  39. get { return "M3U Tuner"; }
  40. }
  41. private string GetFullChannelIdPrefix(TunerHostInfo info)
  42. {
  43. return ChannelIdPrefix + info.Url.GetMD5().ToString("N");
  44. }
  45. protected override async Task<List<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
  46. {
  47. var channelIdPrefix = GetFullChannelIdPrefix(info);
  48. var result = await new M3uParser(Logger, FileSystem, _httpClient, _appHost).Parse(info.Url, channelIdPrefix, info.Id, 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 channelIdPrefix = GetFullChannelIdPrefix(info);
  80. if (!channelId.StartsWith(channelIdPrefix, StringComparison.OrdinalIgnoreCase))
  81. {
  82. return null;
  83. }
  84. var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
  85. var m3uchannels = channels.Cast<M3UChannel>();
  86. var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  87. if (channel != null)
  88. {
  89. var path = channel.Path;
  90. MediaProtocol protocol = MediaProtocol.File;
  91. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  92. {
  93. protocol = MediaProtocol.Http;
  94. }
  95. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  96. {
  97. protocol = MediaProtocol.Rtmp;
  98. }
  99. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  100. {
  101. protocol = MediaProtocol.Rtsp;
  102. }
  103. else if (path.StartsWith("udp", StringComparison.OrdinalIgnoreCase))
  104. {
  105. protocol = MediaProtocol.Udp;
  106. }
  107. else if (path.StartsWith("rtp", StringComparison.OrdinalIgnoreCase))
  108. {
  109. protocol = MediaProtocol.Rtmp;
  110. }
  111. var mediaSource = new MediaSourceInfo
  112. {
  113. Path = channel.Path,
  114. Protocol = protocol,
  115. MediaStreams = new List<MediaStream>
  116. {
  117. new MediaStream
  118. {
  119. Type = MediaStreamType.Video,
  120. // Set the index to -1 because we don't know the exact index of the video stream within the container
  121. Index = -1,
  122. IsInterlaced = true
  123. },
  124. new MediaStream
  125. {
  126. Type = MediaStreamType.Audio,
  127. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  128. Index = -1
  129. }
  130. },
  131. RequiresOpening = true,
  132. RequiresClosing = true,
  133. RequiresLooping = info.EnableStreamLooping,
  134. ReadAtNativeFramerate = false,
  135. Id = channel.Path.GetMD5().ToString("N"),
  136. IsInfiniteStream = true,
  137. IsRemote = true,
  138. IgnoreDts = 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. }