BaseTunerHost.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. cache = cache ?? new ChannelCache();
  40. cache.Date = DateTime.UtcNow;
  41. cache.Channels = result.ToList();
  42. _channelCache.AddOrUpdate(key, cache, (k, v) => cache);
  43. return cache.Channels.ToList();
  44. }
  45. private List<TunerHostInfo> GetTunerHosts()
  46. {
  47. return GetConfiguration().TunerHosts
  48. .Where(i => i.IsEnabled && string.Equals(i.Type, Type, StringComparison.OrdinalIgnoreCase))
  49. .ToList();
  50. }
  51. public async Task<IEnumerable<ChannelInfo>> GetChannels(CancellationToken cancellationToken)
  52. {
  53. var list = new List<ChannelInfo>();
  54. var hosts = GetTunerHosts();
  55. foreach (var host in hosts)
  56. {
  57. try
  58. {
  59. var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
  60. var newChannels = channels.Where(i => !list.Any(l => string.Equals(i.Id, l.Id, StringComparison.OrdinalIgnoreCase))).ToList();
  61. list.AddRange(newChannels);
  62. }
  63. catch (Exception ex)
  64. {
  65. Logger.ErrorException("Error getting channel list", ex);
  66. }
  67. }
  68. return list;
  69. }
  70. protected abstract Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
  71. public async Task<List<MediaSourceInfo>> GetChannelStreamMediaSources(string channelId, CancellationToken cancellationToken)
  72. {
  73. if (IsValidChannelId(channelId))
  74. {
  75. var hosts = GetTunerHosts();
  76. var hostsWithChannel = new List<TunerHostInfo>();
  77. foreach (var host in hosts)
  78. {
  79. var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
  80. if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
  81. {
  82. hostsWithChannel.Add(host);
  83. }
  84. }
  85. foreach (var host in hostsWithChannel)
  86. {
  87. // Check to make sure the tuner is available
  88. // If there's only one tuner, don't bother with the check and just let the tuner be the one to throw an error
  89. if (hostsWithChannel.Count > 1 && !await IsAvailable(host, channelId, cancellationToken).ConfigureAwait(false))
  90. {
  91. Logger.Error("Tuner is not currently available");
  92. continue;
  93. }
  94. var mediaSources = await GetChannelStreamMediaSources(host, channelId, cancellationToken).ConfigureAwait(false);
  95. // Prefix the id with the host Id so that we can easily find it
  96. foreach (var mediaSource in mediaSources)
  97. {
  98. mediaSource.Id = host.Id + mediaSource.Id;
  99. }
  100. return mediaSources;
  101. }
  102. }
  103. return new List<MediaSourceInfo>();
  104. }
  105. protected abstract Task<MediaSourceInfo> GetChannelStream(TunerHostInfo tuner, string channelId, string streamId, CancellationToken cancellationToken);
  106. public async Task<MediaSourceInfo> GetChannelStream(string channelId, string streamId, CancellationToken cancellationToken)
  107. {
  108. if (IsValidChannelId(channelId))
  109. {
  110. var hosts = GetTunerHosts();
  111. var hostsWithChannel = new List<TunerHostInfo>();
  112. foreach (var host in hosts)
  113. {
  114. if (string.IsNullOrWhiteSpace(streamId))
  115. {
  116. var channels = await GetChannels(host, true, cancellationToken).ConfigureAwait(false);
  117. if (channels.Any(i => string.Equals(i.Id, channelId, StringComparison.OrdinalIgnoreCase)))
  118. {
  119. hostsWithChannel.Add(host);
  120. }
  121. }
  122. else if (streamId.StartsWith(host.Id, StringComparison.OrdinalIgnoreCase))
  123. {
  124. hostsWithChannel = new List<TunerHostInfo> { host };
  125. streamId = streamId.Substring(host.Id.Length);
  126. break;
  127. }
  128. }
  129. foreach (var host in hostsWithChannel)
  130. {
  131. // Check to make sure the tuner is available
  132. // If there's only one tuner, don't bother with the check and just let the tuner be the one to throw an error
  133. // If a streamId is specified then availibility has already been checked in GetChannelStreamMediaSources
  134. if (string.IsNullOrWhiteSpace(streamId) && hostsWithChannel.Count > 1)
  135. {
  136. if (!await IsAvailable(host, channelId, cancellationToken).ConfigureAwait(false))
  137. {
  138. Logger.Error("Tuner is not currently available");
  139. continue;
  140. }
  141. }
  142. var stream = await GetChannelStream(host, channelId, streamId, cancellationToken).ConfigureAwait(false);
  143. if (stream != null)
  144. {
  145. return stream;
  146. }
  147. }
  148. }
  149. throw new LiveTvConflictException();
  150. }
  151. protected async Task<bool> IsAvailable(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken)
  152. {
  153. try
  154. {
  155. return await IsAvailableInternal(tuner, channelId, cancellationToken).ConfigureAwait(false);
  156. }
  157. catch (Exception ex)
  158. {
  159. Logger.ErrorException("Error checking tuner availability", ex);
  160. return false;
  161. }
  162. }
  163. protected abstract Task<bool> IsAvailableInternal(TunerHostInfo tuner, string channelId, CancellationToken cancellationToken);
  164. protected abstract bool IsValidChannelId(string channelId);
  165. protected LiveTvOptions GetConfiguration()
  166. {
  167. return Config.GetConfiguration<LiveTvOptions>("livetv");
  168. }
  169. private class ChannelCache
  170. {
  171. public DateTime Date;
  172. public List<ChannelInfo> Channels;
  173. }
  174. }
  175. }