BaseTunerHost.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.LiveTv;
  3. using MediaBrowser.Model.Dto;
  4. using MediaBrowser.Model.LiveTv;
  5. using MediaBrowser.Model.Logging;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Server.Implementations.LiveTv.TunerHosts
  13. {
  14. public abstract class BaseTunerHost
  15. {
  16. protected readonly IConfigurationManager Config;
  17. protected readonly ILogger Logger;
  18. private readonly ConcurrentDictionary<string, ChannelCache> _channelCache =
  19. new ConcurrentDictionary<string, ChannelCache>(StringComparer.OrdinalIgnoreCase);
  20. public BaseTunerHost(IConfigurationManager config, ILogger logger)
  21. {
  22. Config = config;
  23. Logger = logger;
  24. }
  25. protected abstract Task<IEnumerable<ChannelInfo>> GetChannelsInternal(TunerHostInfo tuner, CancellationToken cancellationToken);
  26. public abstract string Type { get; }
  27. public async Task<IEnumerable<ChannelInfo>> GetChannels(TunerHostInfo tuner, bool enableCache, CancellationToken cancellationToken)
  28. {
  29. ChannelCache cache = null;
  30. var key = tuner.Id;
  31. if (enableCache && !string.IsNullOrWhiteSpace(key) && _channelCache.TryGetValue(key, out cache))
  32. {
  33. if ((DateTime.UtcNow - cache.Date) < TimeSpan.FromMinutes(60))
  34. {
  35. return cache.Channels.ToList();
  36. }
  37. }
  38. var result = await GetChannelsInternal(tuner, cancellationToken).ConfigureAwait(false);
  39. var list = result.ToList();
  40. if (!string.IsNullOrWhiteSpace(key))
  41. {
  42. cache = cache ?? new ChannelCache();
  43. cache.Date = DateTime.UtcNow;
  44. cache.Channels = list;
  45. _channelCache.AddOrUpdate(key, cache, (k, v) => cache);
  46. }
  47. return list;
  48. }
  49. private List<TunerHostInfo> GetTunerHosts()
  50. {
  51. return GetConfiguration().TunerHosts
  52. .Where(i => i.IsEnabled && string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
  53. .ToList();
  54. }
  55. public async Task<IEnumerable<ChannelInfo>> GetChannels(CancellationToken cancellationToken)
  56. {
  57. var list = new List<ChannelInfo>();
  58. var hosts = GetTunerHosts();
  59. foreach (var host in hosts)
  60. {
  61. try
  62. {
  63. var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
  64. var newChannels = channels.Where(i => !list.Any(l => string.Equals(i.Id, l.Id, StringComparison.OrdinalIgnoreCase))).ToList();
  65. list.AddRange(newChannels);
  66. }
  67. catch (Exception ex)
  68. {
  69. Logger.ErrorException("Error getting channel list", ex);
  70. }
  71. }
  72. return list;
  73. }
  74. protected abstract Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
  75. public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancellationToken)
  76. {
  77. if (IsValidChannelId(channelId))
  78. {
  79. var hosts = GetTunerHosts();
  80. var hostsWithChannel = new List<TunerHostInfo>();
  81. foreach (var host in hosts)
  82. {
  83. var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
  84. if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
  85. {
  86. hostsWithChannel.Add(host);
  87. }
  88. }
  89. foreach (var host in hostsWithChannel)
  90. {
  91. try
  92. {
  93. var mediaSources = await GetChannelStreamMediaSources(host, channelId, cancellationToken).ConfigureAwait(false);
  94. // Prefix the id with the host Id so that we can easily find it
  95. foreach (var mediaSource in mediaSources)
  96. {
  97. mediaSource.Id = host.Id + mediaSource.Id;
  98. }
  99. return mediaSources;
  100. }
  101. catch (Exception ex)
  102. {
  103. Logger.Error("Error opening tuner", ex);
  104. }
  105. }
  106. }
  107. return new List<MediaSourceInfo>();
  108. }
  109. protected abstract Task<MediaSourceInfo> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken);
  110. public async Task<MediaSourceInfo> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
  111. {
  112. if (IsValidChannelId(channelId))
  113. {
  114. var hosts = GetTunerHosts();
  115. var hostsWithChannel = new List<TunerHostInfo>();
  116. foreach (var host in hosts)
  117. {
  118. if (string.IsNullOrWhiteSpace(streamId))
  119. {
  120. var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
  121. if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
  122. {
  123. hostsWithChannel.Add(host);
  124. }
  125. }
  126. else if (streamId.StartsWith(host.Id, StringComparison.OrdinalIgnoreCase))
  127. {
  128. hostsWithChannel = new List<TunerHostInfo> { host };
  129. streamId = streamId.Substring(host.Id.Length);
  130. break;
  131. }
  132. }
  133. foreach (var host in hostsWithChannel)
  134. {
  135. try
  136. {
  137. var stream = await GetChannelStream(host, channelId, streamId, cancellationToken).ConfigureAwait(false);
  138. if (stream != null)
  139. {
  140. return stream;
  141. }
  142. }
  143. catch (Exception ex)
  144. {
  145. Logger.Error("Error opening tuner", ex);
  146. }
  147. }
  148. }
  149. throw new LiveTvConflictException();
  150. }
  151. protected abstract bool IsValidChannelId(string channelId);
  152. protected LiveTvOptions GetConfiguration()
  153. {
  154. return Config.GetConfiguration<LiveTvOptions>("livetv");
  155. }
  156. private class ChannelCache
  157. {
  158. public DateTime Date;
  159. public List<ChannelInfo> Channels;
  160. }
  161. }
  162. }