BaseTunerHost.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. // Check to make sure the tuner is available
  92. // If there's only one tuner, don't bother with the check and just let the tuner be the one to throw an error
  93. if (hostsWithChannel.Count > 1 && !await IsAvailable(host, channelId, cancellationToken).ConfigureAwait(false))
  94. {
  95. Logger.Error("Tuner is not currently available");
  96. continue;
  97. }
  98. var mediaSources = await GetChannelStreamMediaSources(host, channelId, cancellationToken).ConfigureAwait(false);
  99. // Prefix the id with the host Id so that we can easily find it
  100. foreach (var mediaSource in mediaSources)
  101. {
  102. mediaSource.Id = host.Id + mediaSource.Id;
  103. }
  104. return mediaSources;
  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. // Check to make sure the tuner is available
  136. // If there's only one tuner, don't bother with the check and just let the tuner be the one to throw an error
  137. // If a streamId is specified then availibility has already been checked in GetChannelStreamMediaSources
  138. if (string.IsNullOrWhiteSpace(streamId) && hostsWithChannel.Count > 1)
  139. {
  140. if (!await IsAvailable(host, channelId, cancellationToken).ConfigureAwait(false))
  141. {
  142. Logger.Error("Tuner is not currently available");
  143. continue;
  144. }
  145. }
  146. var stream = await GetChannelStream(host, channelId, streamId, cancellationToken).ConfigureAwait(false);
  147. if (stream != null)
  148. {
  149. return stream;
  150. }
  151. }
  152. }
  153. throw new LiveTvConflictException();
  154. }
  155. protected async Task<bool> IsAvailable(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  156. {
  157. try
  158. {
  159. return await IsAvailableInternal(tuner, channelId, cancellationToken).ConfigureAwait(false);
  160. }
  161. catch (Exception ex)
  162. {
  163. Logger.ErrorException("Error checking tuner availability", ex);
  164. return false;
  165. }
  166. }
  167. protected abstract Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
  168. protected abstract bool IsValidChannelId(string channelId);
  169. protected LiveTvOptions GetConfiguration()
  170. {
  171. return Config.GetConfiguration<LiveTvOptions>("livetv");
  172. }
  173. private class ChannelCache
  174. {
  175. public DateTime Date;
  176. public List<ChannelInfo> Channels;
  177. }
  178. }
  179. }