M3UTunerHost.cs 5.8 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.MediaEncoding;
  17. using MediaBrowser.Model.Serialization;
  18. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
  19. {
  20. public class M3UTunerHost : BaseTunerHost, ITunerHost, IConfigurableTunerHost
  21. {
  22. private readonly IFileSystem _fileSystem;
  23. private readonly IHttpClient _httpClient;
  24. public M3UTunerHost(IConfigurationManager config, ILogger logger, IJsonSerializer jsonSerializer, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IHttpClient httpClient)
  25. : base(config, logger, jsonSerializer, mediaEncoder)
  26. {
  27. _fileSystem = fileSystem;
  28. _httpClient = httpClient;
  29. }
  30. public override string Type
  31. {
  32. get { return "m3u"; }
  33. }
  34. public string Name
  35. {
  36. get { return "M3U Tuner"; }
  37. }
  38. private const string ChannelIdPrefix = "m3u_";
  39. protected override async Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo info, CancellationToken cancellationToken)
  40. {
  41. return await new M3uParser(Logger, _fileSystem, _httpClient).Parse(info.Url, ChannelIdPrefix, info.Id, cancellationToken).ConfigureAwait(false);
  42. }
  43. public Task<List<LiveTvTunerInfo>> GetTunerInfos(CancellationToken cancellationToken)
  44. {
  45. var list = GetTunerHosts()
  46. .Select(i => new LiveTvTunerInfo()
  47. {
  48. Name = Name,
  49. SourceType = Type,
  50. Status = LiveTvTunerStatus.Available,
  51. Id = i.Url.GetMD5().ToString("N"),
  52. Url = i.Url
  53. })
  54. .ToList();
  55. return Task.FromResult(list);
  56. }
  57. protected override async Task<MediaSourceInfo> GetChannelStream(TunerHostInfo info, string channelId, string streamId, CancellationToken cancellationToken)
  58. {
  59. var sources = await GetChannelStreamMediaSources(info, channelId, cancellationToken).ConfigureAwait(false);
  60. return sources.First();
  61. }
  62. public async Task Validate(TunerHostInfo info)
  63. {
  64. using (var stream = await new M3uParser(Logger, _fileSystem, _httpClient).GetListingsStream(info.Url, CancellationToken.None).ConfigureAwait(false))
  65. {
  66. }
  67. }
  68. protected override bool IsValidChannelId(string channelId)
  69. {
  70. return channelId.StartsWith(ChannelIdPrefix, StringComparison.OrdinalIgnoreCase);
  71. }
  72. protected override async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo info, string channelId, CancellationToken cancellationToken)
  73. {
  74. var urlHash = info.Url.GetMD5().ToString("N");
  75. var prefix = ChannelIdPrefix + urlHash;
  76. if (!channelId.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  77. {
  78. return null;
  79. }
  80. var channels = await GetChannels(info, true, cancellationToken).ConfigureAwait(false);
  81. var m3uchannels = channels.Cast<M3UChannel>();
  82. var channel = m3uchannels.FirstOrDefault(c => string.Equals(c.Id, channelId, StringComparison.OrdinalIgnoreCase));
  83. if (channel != null)
  84. {
  85. var path = channel.Path;
  86. MediaProtocol protocol = MediaProtocol.File;
  87. if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
  88. {
  89. protocol = MediaProtocol.Http;
  90. }
  91. else if (path.StartsWith("rtmp", StringComparison.OrdinalIgnoreCase))
  92. {
  93. protocol = MediaProtocol.Rtmp;
  94. }
  95. else if (path.StartsWith("rtsp", StringComparison.OrdinalIgnoreCase))
  96. {
  97. protocol = MediaProtocol.Rtsp;
  98. }
  99. var mediaSource = new MediaSourceInfo
  100. {
  101. Path = channel.Path,
  102. Protocol = protocol,
  103. MediaStreams = new List<MediaStream>
  104. {
  105. new MediaStream
  106. {
  107. Type = MediaStreamType.Video,
  108. // Set the index to -1 because we don't know the exact index of the video stream within the container
  109. Index = -1,
  110. IsInterlaced = true
  111. },
  112. new MediaStream
  113. {
  114. Type = MediaStreamType.Audio,
  115. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  116. Index = -1
  117. }
  118. },
  119. RequiresOpening = false,
  120. RequiresClosing = false,
  121. ReadAtNativeFramerate = false
  122. };
  123. return new List<MediaSourceInfo> { mediaSource };
  124. }
  125. return new List<MediaSourceInfo>();
  126. }
  127. protected override Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  128. {
  129. return Task.FromResult(true);
  130. }
  131. public string ApplyDuration(string streamPath, TimeSpan duration)
  132. {
  133. return streamPath;
  134. }
  135. }
  136. }