M3UTunerHost.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 CommonIO;
  15. using MediaBrowser.Common.Net;
  16. using MediaBrowser.Controller.Configuration;
  17. using MediaBrowser.Controller.MediaEncoding;
  18. using MediaBrowser.Model.Serialization;
  19. using MediaBrowser.Server.Implementations.LiveTv.EmbyTV;
  20. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
  21. {
  22. public class M3UTunerHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
  23. {
  24. private readonly IFileSystem _fileSystem;
  25. private readonly IHttpClient _httpClient;
  26. public M3UTunerHost(IServerConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient)
  27. : base(config, logger, jsonSerializer, mediaEncoder)
  28. {
  29. _fileSystem = fileSystem;
  30. _httpClient = httpClient;
  31. }
  32. public override string Type
  33. {
  34. get { return "m3u"; }
  35. }
  36. public string Name
  37. {
  38. get { return "M3U Tuner"; }
  39. }
  40. private const string ChannelIdPrefix = "m3u_";
  41. protected override async Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
  42. {
  43. return await new M3uParser(Logger, _fileSystem, _httpClient).Parse(info.Url, ChannelIdPrefix, info.Id, cancellationToken).ConfigureAwait(false);
  44. }
  45. public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
  46. {
  47. var list = GetTunerHosts()
  48. .Select(i => new LiveTvTunerInfo()
  49. {
  50. Name = Name,
  51. SourceType = Type,
  52. Status = LiveTvTunerStatus.Available,
  53. Id = i.Url.GetMD5().ToString("N"),
  54. Url = i.Url
  55. })
  56. .ToList();
  57. return Task.FromResult(list);
  58. }
  59. protected override async Task<LiveStream> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  60. {
  61. var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
  62. var liveStream = new LiveStream(sources.First());
  63. return liveStream;
  64. }
  65. public async Task Validate(TunerHostInfo info)
  66. {
  67. using (var stream = await new M3uParser(Logger, _fileSystem, _httpClient).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
  68. {
  69. }
  70. }
  71. protected override bool IsValidChannelId(string channelId)
  72. {
  73. return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
  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. var mediaSource = new MediaSourceInfo
  103. {
  104. Path = channel.Path,
  105. Protocol = protocol,
  106. MediaStreams = new List<MediaStream>
  107. {
  108. new MediaStream
  109. {
  110. Type = MediaStreamType.Video,
  111. // Set the index to -1 because we don't know the exact index of the video stream within the container
  112. Index = -1,
  113. IsInterlaced = true
  114. },
  115. new MediaStream
  116. {
  117. Type = MediaStreamType.Audio,
  118. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  119. Index = -1
  120. }
  121. },
  122. RequiresOpening = false,
  123. RequiresClosing = false,
  124. ReadAtNativeFramerate = false,
  125. Id = channel.Path.GetMD5().ToString("N")
  126. };
  127. return new List<MediaSourceInfo> { mediaSource };
  128. }
  129. return new List<MediaSourceInfo>();
  130. }
  131. protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  132. {
  133. return Task.FromResult(true);
  134. }
  135. }
  136. }