LiveTvManager.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Drawing;
  7. using MediaBrowser.Controller.Dto;
  8. using MediaBrowser.Controller.Entities;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.LiveTv;
  11. using MediaBrowser.Controller.MediaInfo;
  12. using MediaBrowser.Controller.Persistence;
  13. using MediaBrowser.Controller.Sorting;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.LiveTv;
  16. using MediaBrowser.Model.Logging;
  17. using MediaBrowser.Model.Querying;
  18. using System;
  19. using System.Collections.Concurrent;
  20. using System.Collections.Generic;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. namespace MediaBrowser.Server.Implementations.LiveTv
  26. {
  27. /// <summary>
  28. /// Class LiveTvManager
  29. /// </summary>
  30. public class LiveTvManager : ILiveTvManager, IDisposable
  31. {
  32. private readonly IServerConfigurationManager _config;
  33. private readonly IFileSystem _fileSystem;
  34. private readonly ILogger _logger;
  35. private readonly IItemRepository _itemRepo;
  36. private readonly IUserManager _userManager;
  37. private readonly IUserDataManager _userDataManager;
  38. private readonly ILibraryManager _libraryManager;
  39. private readonly IMediaEncoder _mediaEncoder;
  40. private readonly ITaskManager _taskManager;
  41. private readonly LiveTvDtoService _tvDtoService;
  42. private readonly List<ILiveTvService> _services = new List<ILiveTvService>();
  43. private readonly ConcurrentDictionary<string, LiveStreamInfo> _openStreams =
  44. new ConcurrentDictionary<string, LiveStreamInfo>();
  45. private List<Guid> _channelIdList = new List<Guid>();
  46. private Dictionary<Guid, LiveTvProgram> _programs = new Dictionary<Guid, LiveTvProgram>();
  47. public LiveTvManager(IServerConfigurationManager config, IFileSystem fileSystem, ILogger logger, IItemRepository itemRepo, IImageProcessor imageProcessor, IUserDataManager userDataManager, IDtoService dtoService, IUserManager userManager, ILibraryManager libraryManager, IMediaEncoder mediaEncoder, ITaskManager taskManager)
  48. {
  49. _config = config;
  50. _fileSystem = fileSystem;
  51. _logger = logger;
  52. _itemRepo = itemRepo;
  53. _userManager = userManager;
  54. _libraryManager = libraryManager;
  55. _mediaEncoder = mediaEncoder;
  56. _taskManager = taskManager;
  57. _userDataManager = userDataManager;
  58. _tvDtoService = new LiveTvDtoService(dtoService, userDataManager, imageProcessor, logger, _itemRepo);
  59. }
  60. /// <summary>
  61. /// Gets the services.
  62. /// </summary>
  63. /// <value>The services.</value>
  64. public IReadOnlyList<ILiveTvService> Services
  65. {
  66. get { return _services; }
  67. }
  68. public ILiveTvService ActiveService { get; private set; }
  69. /// <summary>
  70. /// Adds the parts.
  71. /// </summary>
  72. /// <param name="services">The services.</param>
  73. public void AddParts(IEnumerable<ILiveTvService> services)
  74. {
  75. _services.AddRange(services);
  76. SetActiveService(_services.FirstOrDefault());
  77. }
  78. private void SetActiveService(ILiveTvService service)
  79. {
  80. if (ActiveService != null)
  81. {
  82. ActiveService.DataSourceChanged -= service_DataSourceChanged;
  83. }
  84. ActiveService = service;
  85. if (service != null)
  86. {
  87. service.DataSourceChanged += service_DataSourceChanged;
  88. }
  89. }
  90. void service_DataSourceChanged(object sender, EventArgs e)
  91. {
  92. _taskManager.CancelIfRunningAndQueue<RefreshChannelsScheduledTask>();
  93. }
  94. public Task<QueryResult<ChannelInfoDto>> GetChannels(ChannelQuery query, CancellationToken cancellationToken)
  95. {
  96. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId));
  97. var channels = _channelIdList.Select(_libraryManager.GetItemById)
  98. .Where(i => i != null)
  99. .OfType<LiveTvChannel>();
  100. if (user != null)
  101. {
  102. // Avoid implicitly captured closure
  103. var currentUser = user;
  104. channels = channels
  105. .Where(i => i.IsParentalAllowed(currentUser))
  106. .OrderBy(i =>
  107. {
  108. double number = 0;
  109. if (!string.IsNullOrEmpty(i.ChannelInfo.Number))
  110. {
  111. double.TryParse(i.ChannelInfo.Number, out number);
  112. }
  113. return number;
  114. });
  115. }
  116. channels = channels.OrderBy(i =>
  117. {
  118. double number = 0;
  119. if (!string.IsNullOrEmpty(i.ChannelInfo.Number))
  120. {
  121. double.TryParse(i.ChannelInfo.Number, out number);
  122. }
  123. return number;
  124. }).ThenBy(i => i.Name);
  125. var allChannels = channels.ToList();
  126. IEnumerable<LiveTvChannel> allEnumerable = allChannels;
  127. if (query.StartIndex.HasValue)
  128. {
  129. allEnumerable = allEnumerable.Skip(query.StartIndex.Value);
  130. }
  131. if (query.Limit.HasValue)
  132. {
  133. allEnumerable = allEnumerable.Take(query.Limit.Value);
  134. }
  135. var returnChannels = allEnumerable
  136. .Select(i => _tvDtoService.GetChannelInfoDto(i, GetCurrentProgram(i.ChannelInfo.Id), user))
  137. .ToArray();
  138. var result = new QueryResult<ChannelInfoDto>
  139. {
  140. Items = returnChannels,
  141. TotalRecordCount = allChannels.Count
  142. };
  143. return Task.FromResult(result);
  144. }
  145. public LiveTvChannel GetInternalChannel(string id)
  146. {
  147. return GetInternalChannel(new Guid(id));
  148. }
  149. private LiveTvChannel GetInternalChannel(Guid id)
  150. {
  151. return _libraryManager.GetItemById(id) as LiveTvChannel;
  152. }
  153. public LiveTvProgram GetInternalProgram(string id)
  154. {
  155. var guid = new Guid(id);
  156. LiveTvProgram obj = null;
  157. _programs.TryGetValue(guid, out obj);
  158. return obj;
  159. }
  160. public async Task<ILiveTvRecording> GetInternalRecording(string id, CancellationToken cancellationToken)
  161. {
  162. var service = ActiveService;
  163. var recordings = await service.GetRecordingsAsync(cancellationToken).ConfigureAwait(false);
  164. var recording = recordings.FirstOrDefault(i => _tvDtoService.GetInternalRecordingId(service.Name, i.Id) == new Guid(id));
  165. return await GetRecording(recording, service.Name, cancellationToken).ConfigureAwait(false);
  166. }
  167. private readonly SemaphoreSlim _liveStreamSemaphore = new SemaphoreSlim(1, 1);
  168. public async Task<LiveStreamInfo> GetRecordingStream(string id, CancellationToken cancellationToken)
  169. {
  170. await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  171. try
  172. {
  173. var service = ActiveService;
  174. var recordings = await service.GetRecordingsAsync(cancellationToken).ConfigureAwait(false);
  175. var recording = recordings.First(i => _tvDtoService.GetInternalRecordingId(service.Name, i.Id) == new Guid(id));
  176. var result = await service.GetRecordingStream(recording.Id, cancellationToken).ConfigureAwait(false);
  177. if (!string.IsNullOrEmpty(result.Id))
  178. {
  179. _openStreams.AddOrUpdate(result.Id, result, (key, info) => result);
  180. }
  181. return result;
  182. }
  183. catch (Exception ex)
  184. {
  185. _logger.ErrorException("Error getting recording stream", ex);
  186. throw;
  187. }
  188. finally
  189. {
  190. _liveStreamSemaphore.Release();
  191. }
  192. }
  193. public async Task<LiveStreamInfo> GetChannelStream(string id, CancellationToken cancellationToken)
  194. {
  195. await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  196. try
  197. {
  198. var service = ActiveService;
  199. var channel = GetInternalChannel(id);
  200. _logger.Info("Opening channel stream from {0}, external channel Id: {1}", service.Name, channel.ChannelInfo.Id);
  201. var result = await service.GetChannelStream(channel.ChannelInfo.Id, cancellationToken).ConfigureAwait(false);
  202. if (!string.IsNullOrEmpty(result.Id))
  203. {
  204. _openStreams.AddOrUpdate(result.Id, result, (key, info) => result);
  205. }
  206. return result;
  207. }
  208. catch (Exception ex)
  209. {
  210. _logger.ErrorException("Error getting channel stream", ex);
  211. throw;
  212. }
  213. finally
  214. {
  215. _liveStreamSemaphore.Release();
  216. }
  217. }
  218. private async Task<LiveTvChannel> GetChannel(ChannelInfo channelInfo, string serviceName, CancellationToken cancellationToken)
  219. {
  220. var path = Path.Combine(_config.ApplicationPaths.ItemsByNamePath, "channels", _fileSystem.GetValidFilename(channelInfo.Name));
  221. var fileInfo = new DirectoryInfo(path);
  222. var isNew = false;
  223. if (!fileInfo.Exists)
  224. {
  225. Directory.CreateDirectory(path);
  226. fileInfo = new DirectoryInfo(path);
  227. if (!fileInfo.Exists)
  228. {
  229. throw new IOException("Path not created: " + path);
  230. }
  231. isNew = true;
  232. }
  233. var id = _tvDtoService.GetInternalChannelId(serviceName, channelInfo.Id);
  234. var item = _itemRepo.RetrieveItem(id) as LiveTvChannel;
  235. if (item == null)
  236. {
  237. item = new LiveTvChannel
  238. {
  239. Name = channelInfo.Name,
  240. Id = id,
  241. DateCreated = _fileSystem.GetCreationTimeUtc(fileInfo),
  242. DateModified = _fileSystem.GetLastWriteTimeUtc(fileInfo),
  243. Path = path
  244. };
  245. isNew = true;
  246. }
  247. item.ChannelInfo = channelInfo;
  248. item.ServiceName = serviceName;
  249. // Set this now so we don't cause additional file system access during provider executions
  250. item.ResetResolveArgs(fileInfo);
  251. await item.RefreshMetadata(cancellationToken, forceSave: isNew, resetResolveArgs: false);
  252. return item;
  253. }
  254. private async Task<LiveTvProgram> GetProgram(ProgramInfo info, ChannelType channelType, string serviceName, CancellationToken cancellationToken)
  255. {
  256. var isNew = false;
  257. var id = _tvDtoService.GetInternalProgramId(serviceName, info.Id);
  258. var item = _itemRepo.RetrieveItem(id) as LiveTvProgram;
  259. if (item == null)
  260. {
  261. item = new LiveTvProgram
  262. {
  263. Name = info.Name,
  264. Id = id,
  265. DateCreated = DateTime.UtcNow,
  266. DateModified = DateTime.UtcNow
  267. };
  268. isNew = true;
  269. }
  270. item.ChannelType = channelType;
  271. item.ProgramInfo = info;
  272. item.ServiceName = serviceName;
  273. await item.RefreshMetadata(cancellationToken, forceSave: isNew, resetResolveArgs: false);
  274. return item;
  275. }
  276. private async Task<ILiveTvRecording> GetRecording(RecordingInfo info, string serviceName, CancellationToken cancellationToken)
  277. {
  278. var isNew = false;
  279. var id = _tvDtoService.GetInternalRecordingId(serviceName, info.Id);
  280. var item = _itemRepo.RetrieveItem(id) as ILiveTvRecording;
  281. if (item == null)
  282. {
  283. if (info.ChannelType == ChannelType.TV)
  284. {
  285. item = new LiveTvVideoRecording
  286. {
  287. Name = info.Name,
  288. Id = id,
  289. DateCreated = DateTime.UtcNow,
  290. DateModified = DateTime.UtcNow,
  291. VideoType = VideoType.VideoFile
  292. };
  293. }
  294. else
  295. {
  296. item = new LiveTvAudioRecording
  297. {
  298. Name = info.Name,
  299. Id = id,
  300. DateCreated = DateTime.UtcNow,
  301. DateModified = DateTime.UtcNow
  302. };
  303. }
  304. if (!string.IsNullOrEmpty(info.Path))
  305. {
  306. item.Path = info.Path;
  307. }
  308. else if (!string.IsNullOrEmpty(info.Url))
  309. {
  310. item.Path = info.Url;
  311. }
  312. isNew = true;
  313. }
  314. item.RecordingInfo = info;
  315. item.ServiceName = serviceName;
  316. await item.RefreshMetadata(cancellationToken, forceSave: isNew, resetResolveArgs: false);
  317. _libraryManager.RegisterItem((BaseItem)item);
  318. return item;
  319. }
  320. private LiveTvChannel GetChannel(LiveTvProgram program)
  321. {
  322. var programChannelId = program.ProgramInfo.ChannelId;
  323. var internalProgramChannelId = _tvDtoService.GetInternalChannelId(program.ServiceName, programChannelId);
  324. return GetInternalChannel(internalProgramChannelId);
  325. }
  326. public async Task<ProgramInfoDto> GetProgram(string id, CancellationToken cancellationToken, User user = null)
  327. {
  328. var program = GetInternalProgram(id);
  329. var channel = GetChannel(program);
  330. var dto = _tvDtoService.GetProgramInfoDto(program, channel, user);
  331. await AddRecordingInfo(new[] { dto }, cancellationToken).ConfigureAwait(false);
  332. return dto;
  333. }
  334. public async Task<QueryResult<ProgramInfoDto>> GetPrograms(ProgramQuery query, CancellationToken cancellationToken)
  335. {
  336. IEnumerable<LiveTvProgram> programs = _programs.Values;
  337. if (query.MinEndDate.HasValue)
  338. {
  339. var val = query.MinEndDate.Value;
  340. programs = programs.Where(i => i.ProgramInfo.EndDate >= val);
  341. }
  342. if (query.MinStartDate.HasValue)
  343. {
  344. var val = query.MinStartDate.Value;
  345. programs = programs.Where(i => i.ProgramInfo.StartDate >= val);
  346. }
  347. if (query.MaxEndDate.HasValue)
  348. {
  349. var val = query.MaxEndDate.Value;
  350. programs = programs.Where(i => i.ProgramInfo.EndDate <= val);
  351. }
  352. if (query.MaxStartDate.HasValue)
  353. {
  354. var val = query.MaxStartDate.Value;
  355. programs = programs.Where(i => i.ProgramInfo.StartDate <= val);
  356. }
  357. if (query.ChannelIdList.Length > 0)
  358. {
  359. var guids = query.ChannelIdList.Select(i => new Guid(i)).ToList();
  360. var serviceName = ActiveService.Name;
  361. programs = programs.Where(i =>
  362. {
  363. var programChannelId = i.ProgramInfo.ChannelId;
  364. var internalProgramChannelId = _tvDtoService.GetInternalChannelId(serviceName, programChannelId);
  365. return guids.Contains(internalProgramChannelId);
  366. });
  367. }
  368. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId));
  369. if (user != null)
  370. {
  371. // Avoid implicitly captured closure
  372. var currentUser = user;
  373. programs = programs.Where(i => i.IsParentalAllowed(currentUser));
  374. }
  375. var returnArray = programs
  376. .Select(i =>
  377. {
  378. var channel = GetChannel(i);
  379. return _tvDtoService.GetProgramInfoDto(i, channel, user);
  380. })
  381. .ToArray();
  382. await AddRecordingInfo(returnArray, cancellationToken).ConfigureAwait(false);
  383. var result = new QueryResult<ProgramInfoDto>
  384. {
  385. Items = returnArray,
  386. TotalRecordCount = returnArray.Length
  387. };
  388. return result;
  389. }
  390. public async Task<QueryResult<ProgramInfoDto>> GetRecommendedPrograms(RecommendedProgramQuery query, CancellationToken cancellationToken)
  391. {
  392. IEnumerable<LiveTvProgram> programs = _programs.Values;
  393. var user = _userManager.GetUserById(new Guid(query.UserId));
  394. // Avoid implicitly captured closure
  395. var currentUser = user;
  396. programs = programs.Where(i => i.IsParentalAllowed(currentUser));
  397. if (query.IsAiring.HasValue)
  398. {
  399. var val = query.IsAiring.Value;
  400. programs = programs.Where(i => i.IsAiring == val);
  401. }
  402. if (query.HasAired.HasValue)
  403. {
  404. var val = query.HasAired.Value;
  405. programs = programs.Where(i => i.HasAired == val);
  406. }
  407. var serviceName = ActiveService.Name;
  408. var programList = programs.ToList();
  409. var genres = programList.SelectMany(i => i.Genres)
  410. .Distinct(StringComparer.OrdinalIgnoreCase)
  411. .Select(i => _libraryManager.GetGenre(i))
  412. .ToDictionary(i => i.Name, StringComparer.OrdinalIgnoreCase);
  413. programs = programList.OrderByDescending(i => GetRecommendationScore(i.ProgramInfo, user.Id, serviceName, genres))
  414. .ThenBy(i => i.ProgramInfo.StartDate);
  415. if (query.Limit.HasValue)
  416. {
  417. programs = programs.Take(query.Limit.Value)
  418. .OrderBy(i => i.ProgramInfo.StartDate);
  419. }
  420. var returnArray = programs
  421. .Select(i =>
  422. {
  423. var channel = GetChannel(i);
  424. return _tvDtoService.GetProgramInfoDto(i, channel, user);
  425. })
  426. .ToArray();
  427. await AddRecordingInfo(returnArray, cancellationToken).ConfigureAwait(false);
  428. var result = new QueryResult<ProgramInfoDto>
  429. {
  430. Items = returnArray,
  431. TotalRecordCount = returnArray.Length
  432. };
  433. return result;
  434. }
  435. private int GetRecommendationScore(ProgramInfo program, Guid userId, string serviceName, Dictionary<string, Genre> genres)
  436. {
  437. var score = 0;
  438. if (program.IsLive)
  439. {
  440. score++;
  441. }
  442. if (program.IsSeries && !program.IsRepeat)
  443. {
  444. score++;
  445. }
  446. var internalChannelId = _tvDtoService.GetInternalChannelId(serviceName, program.ChannelId);
  447. var channel = GetInternalChannel(internalChannelId);
  448. var channelUserdata = _userDataManager.GetUserData(userId, channel.GetUserDataKey());
  449. if ((channelUserdata.Likes ?? false))
  450. {
  451. score += 2;
  452. }
  453. else if (!(channelUserdata.Likes ?? true))
  454. {
  455. score -= 2;
  456. }
  457. if (channelUserdata.IsFavorite)
  458. {
  459. score += 3;
  460. }
  461. score += GetGenreScore(program.Genres, userId, genres);
  462. return score;
  463. }
  464. private int GetGenreScore(IEnumerable<string> programGenres, Guid userId, Dictionary<string, Genre> genres)
  465. {
  466. return programGenres.Select(i =>
  467. {
  468. var score = 0;
  469. Genre genre;
  470. if (genres.TryGetValue(i, out genre))
  471. {
  472. var genreUserdata = _userDataManager.GetUserData(userId, genre.GetUserDataKey());
  473. if ((genreUserdata.Likes ?? false))
  474. {
  475. score++;
  476. }
  477. else if (!(genreUserdata.Likes ?? true))
  478. {
  479. score--;
  480. }
  481. if (genreUserdata.IsFavorite)
  482. {
  483. score += 2;
  484. }
  485. }
  486. return score;
  487. }).Sum();
  488. }
  489. private async Task AddRecordingInfo(IEnumerable<ProgramInfoDto> programs, CancellationToken cancellationToken)
  490. {
  491. var timers = await ActiveService.GetTimersAsync(cancellationToken).ConfigureAwait(false);
  492. var timerList = timers.ToList();
  493. foreach (var program in programs)
  494. {
  495. var timer = timerList.FirstOrDefault(i => string.Equals(i.ProgramId, program.ExternalId, StringComparison.OrdinalIgnoreCase));
  496. if (timer != null)
  497. {
  498. program.TimerId = _tvDtoService.GetInternalTimerId(program.ServiceName, timer.Id)
  499. .ToString("N");
  500. if (!string.IsNullOrEmpty(timer.SeriesTimerId))
  501. {
  502. program.SeriesTimerId = _tvDtoService.GetInternalSeriesTimerId(program.ServiceName, timer.SeriesTimerId)
  503. .ToString("N");
  504. }
  505. }
  506. }
  507. }
  508. internal async Task RefreshChannels(IProgress<double> progress, CancellationToken cancellationToken)
  509. {
  510. // Avoid implicitly captured closure
  511. var service = ActiveService;
  512. if (service == null)
  513. {
  514. progress.Report(100);
  515. return;
  516. }
  517. progress.Report(10);
  518. var allChannels = await GetChannels(service, cancellationToken).ConfigureAwait(false);
  519. var allChannelsList = allChannels.ToList();
  520. var list = new List<LiveTvChannel>();
  521. var numComplete = 0;
  522. foreach (var channelInfo in allChannelsList)
  523. {
  524. try
  525. {
  526. var item = await GetChannel(channelInfo.Item2, channelInfo.Item1, cancellationToken).ConfigureAwait(false);
  527. list.Add(item);
  528. _libraryManager.RegisterItem(item);
  529. }
  530. catch (OperationCanceledException)
  531. {
  532. throw;
  533. }
  534. catch (Exception ex)
  535. {
  536. _logger.ErrorException("Error getting channel information for {0}", ex, channelInfo.Item2.Name);
  537. }
  538. numComplete++;
  539. double percent = numComplete;
  540. percent /= allChannelsList.Count;
  541. progress.Report(5 * percent + 10);
  542. }
  543. _channelIdList = list.Select(i => i.Id).ToList();
  544. progress.Report(15);
  545. numComplete = 0;
  546. var programs = new List<LiveTvProgram>();
  547. var guideDays = GetGuideDays(list.Count);
  548. foreach (var item in list)
  549. {
  550. // Avoid implicitly captured closure
  551. var currentChannel = item;
  552. try
  553. {
  554. var start = DateTime.UtcNow.AddHours(-1);
  555. var end = start.AddDays(guideDays);
  556. var channelPrograms = await service.GetProgramsAsync(currentChannel.ChannelInfo.Id, start, end, cancellationToken).ConfigureAwait(false);
  557. var programTasks = channelPrograms.Select(program => GetProgram(program, currentChannel.ChannelInfo.ChannelType, service.Name, cancellationToken));
  558. var programEntities = await Task.WhenAll(programTasks).ConfigureAwait(false);
  559. programs.AddRange(programEntities);
  560. }
  561. catch (OperationCanceledException)
  562. {
  563. throw;
  564. }
  565. catch (Exception ex)
  566. {
  567. _logger.ErrorException("Error getting programs for channel {0}", ex, currentChannel.Name);
  568. }
  569. numComplete++;
  570. double percent = numComplete;
  571. percent /= allChannelsList.Count;
  572. progress.Report(90 * percent + 10);
  573. }
  574. _programs = programs.ToDictionary(i => i.Id);
  575. }
  576. private double GetGuideDays(int channelCount)
  577. {
  578. if (_config.Configuration.LiveTvOptions.GuideDays.HasValue)
  579. {
  580. return _config.Configuration.LiveTvOptions.GuideDays.Value;
  581. }
  582. var programsPerDay = channelCount * 48;
  583. const int maxPrograms = 24000;
  584. var days = Math.Round(((double)maxPrograms) / programsPerDay);
  585. // No less than 2, no more than 14
  586. return Math.Max(2, Math.Min(days, 14));
  587. }
  588. private async Task<IEnumerable<Tuple<string, ChannelInfo>>> GetChannels(ILiveTvService service, CancellationToken cancellationToken)
  589. {
  590. var channels = await service.GetChannelsAsync(cancellationToken).ConfigureAwait(false);
  591. return channels.Select(i => new Tuple<string, ChannelInfo>(service.Name, i));
  592. }
  593. public async Task<QueryResult<RecordingInfoDto>> GetRecordings(RecordingQuery query, CancellationToken cancellationToken)
  594. {
  595. var service = ActiveService;
  596. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId));
  597. var recordings = await service.GetRecordingsAsync(cancellationToken).ConfigureAwait(false);
  598. if (!string.IsNullOrEmpty(query.ChannelId))
  599. {
  600. var guid = new Guid(query.ChannelId);
  601. var currentServiceName = service.Name;
  602. recordings = recordings
  603. .Where(i => _tvDtoService.GetInternalChannelId(currentServiceName, i.ChannelId) == guid);
  604. }
  605. if (!string.IsNullOrEmpty(query.Id))
  606. {
  607. var guid = new Guid(query.Id);
  608. var currentServiceName = service.Name;
  609. recordings = recordings
  610. .Where(i => _tvDtoService.GetInternalRecordingId(currentServiceName, i.Id) == guid);
  611. }
  612. if (!string.IsNullOrEmpty(query.GroupId))
  613. {
  614. var guid = new Guid(query.GroupId);
  615. recordings = recordings.Where(i => GetRecordingGroupIds(i).Contains(guid));
  616. }
  617. if (query.IsInProgress.HasValue)
  618. {
  619. var val = query.IsInProgress.Value;
  620. recordings = recordings.Where(i => (i.Status == RecordingStatus.InProgress) == val);
  621. }
  622. if (query.Status.HasValue)
  623. {
  624. var val = query.Status.Value;
  625. recordings = recordings.Where(i => (i.Status == val));
  626. }
  627. if (!string.IsNullOrEmpty(query.SeriesTimerId))
  628. {
  629. var guid = new Guid(query.SeriesTimerId);
  630. var currentServiceName = service.Name;
  631. recordings = recordings
  632. .Where(i => _tvDtoService.GetInternalSeriesTimerId(currentServiceName, i.SeriesTimerId) == guid);
  633. }
  634. recordings = recordings.OrderByDescending(i => i.StartDate);
  635. IEnumerable<ILiveTvRecording> entities = await GetEntities(recordings, service.Name, cancellationToken).ConfigureAwait(false);
  636. if (user != null)
  637. {
  638. var currentUser = user;
  639. entities = entities.Where(i => i.IsParentalAllowed(currentUser));
  640. }
  641. if (query.StartIndex.HasValue)
  642. {
  643. entities = entities.Skip(query.StartIndex.Value);
  644. }
  645. if (query.Limit.HasValue)
  646. {
  647. entities = entities.Take(query.Limit.Value);
  648. }
  649. var returnArray = entities
  650. .Select(i =>
  651. {
  652. var channel = string.IsNullOrEmpty(i.RecordingInfo.ChannelId) ? null : GetInternalChannel(_tvDtoService.GetInternalChannelId(service.Name, i.RecordingInfo.ChannelId));
  653. return _tvDtoService.GetRecordingInfoDto(i, channel, service, user);
  654. })
  655. .ToArray();
  656. return new QueryResult<RecordingInfoDto>
  657. {
  658. Items = returnArray,
  659. TotalRecordCount = returnArray.Length
  660. };
  661. }
  662. private Task<ILiveTvRecording[]> GetEntities(IEnumerable<RecordingInfo> recordings, string serviceName, CancellationToken cancellationToken)
  663. {
  664. var tasks = recordings.Select(i => GetRecording(i, serviceName, cancellationToken));
  665. return Task.WhenAll(tasks);
  666. }
  667. private IEnumerable<ILiveTvService> GetServices(string serviceName, string channelId)
  668. {
  669. IEnumerable<ILiveTvService> services = _services;
  670. if (string.IsNullOrEmpty(serviceName) && !string.IsNullOrEmpty(channelId))
  671. {
  672. var channel = GetInternalChannel(channelId);
  673. if (channel != null)
  674. {
  675. serviceName = channel.ServiceName;
  676. }
  677. }
  678. if (!string.IsNullOrEmpty(serviceName))
  679. {
  680. services = services.Where(i => string.Equals(i.Name, serviceName, StringComparison.OrdinalIgnoreCase));
  681. }
  682. return services;
  683. }
  684. public async Task<QueryResult<TimerInfoDto>> GetTimers(TimerQuery query, CancellationToken cancellationToken)
  685. {
  686. var service = ActiveService;
  687. var timers = await service.GetTimersAsync(cancellationToken).ConfigureAwait(false);
  688. if (!string.IsNullOrEmpty(query.ChannelId))
  689. {
  690. var guid = new Guid(query.ChannelId);
  691. timers = timers.Where(i => guid == _tvDtoService.GetInternalChannelId(service.Name, i.ChannelId));
  692. }
  693. if (!string.IsNullOrEmpty(query.SeriesTimerId))
  694. {
  695. var guid = new Guid(query.SeriesTimerId);
  696. var currentServiceName = service.Name;
  697. timers = timers
  698. .Where(i => _tvDtoService.GetInternalSeriesTimerId(currentServiceName, i.SeriesTimerId) == guid);
  699. }
  700. var returnArray = timers
  701. .Select(i =>
  702. {
  703. var program = string.IsNullOrEmpty(i.ProgramId) ? null : GetInternalProgram(_tvDtoService.GetInternalProgramId(service.Name, i.ProgramId).ToString("N"));
  704. var channel = string.IsNullOrEmpty(i.ChannelId) ? null : GetInternalChannel(_tvDtoService.GetInternalChannelId(service.Name, i.ChannelId));
  705. return _tvDtoService.GetTimerInfoDto(i, service, program, channel);
  706. })
  707. .OrderBy(i => i.StartDate)
  708. .ToArray();
  709. return new QueryResult<TimerInfoDto>
  710. {
  711. Items = returnArray,
  712. TotalRecordCount = returnArray.Length
  713. };
  714. }
  715. public async Task DeleteRecording(string recordingId)
  716. {
  717. var recording = await GetRecording(recordingId, CancellationToken.None).ConfigureAwait(false);
  718. if (recording == null)
  719. {
  720. throw new ResourceNotFoundException(string.Format("Recording with Id {0} not found", recordingId));
  721. }
  722. var service = GetServices(recording.ServiceName, null)
  723. .First();
  724. await service.DeleteRecordingAsync(recording.ExternalId, CancellationToken.None).ConfigureAwait(false);
  725. }
  726. public async Task CancelTimer(string id)
  727. {
  728. var timer = await GetTimer(id, CancellationToken.None).ConfigureAwait(false);
  729. if (timer == null)
  730. {
  731. throw new ResourceNotFoundException(string.Format("Timer with Id {0} not found", id));
  732. }
  733. var service = GetServices(timer.ServiceName, null)
  734. .First();
  735. await service.CancelTimerAsync(timer.ExternalId, CancellationToken.None).ConfigureAwait(false);
  736. }
  737. public async Task CancelSeriesTimer(string id)
  738. {
  739. var timer = await GetSeriesTimer(id, CancellationToken.None).ConfigureAwait(false);
  740. if (timer == null)
  741. {
  742. throw new ResourceNotFoundException(string.Format("Timer with Id {0} not found", id));
  743. }
  744. var service = GetServices(timer.ServiceName, null)
  745. .First();
  746. await service.CancelSeriesTimerAsync(timer.ExternalId, CancellationToken.None).ConfigureAwait(false);
  747. }
  748. public async Task<RecordingInfoDto> GetRecording(string id, CancellationToken cancellationToken, User user = null)
  749. {
  750. var results = await GetRecordings(new RecordingQuery
  751. {
  752. UserId = user == null ? null : user.Id.ToString("N"),
  753. Id = id
  754. }, cancellationToken).ConfigureAwait(false);
  755. return results.Items.FirstOrDefault();
  756. }
  757. public async Task<TimerInfoDto> GetTimer(string id, CancellationToken cancellationToken)
  758. {
  759. var results = await GetTimers(new TimerQuery(), cancellationToken).ConfigureAwait(false);
  760. return results.Items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.CurrentCulture));
  761. }
  762. public async Task<SeriesTimerInfoDto> GetSeriesTimer(string id, CancellationToken cancellationToken)
  763. {
  764. var results = await GetSeriesTimers(new SeriesTimerQuery(), cancellationToken).ConfigureAwait(false);
  765. return results.Items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.CurrentCulture));
  766. }
  767. public async Task<QueryResult<SeriesTimerInfoDto>> GetSeriesTimers(SeriesTimerQuery query, CancellationToken cancellationToken)
  768. {
  769. var service = ActiveService;
  770. var timers = await service.GetSeriesTimersAsync(cancellationToken).ConfigureAwait(false);
  771. if (string.Equals(query.SortBy, "Priority", StringComparison.OrdinalIgnoreCase))
  772. {
  773. timers = query.SortOrder == SortOrder.Descending ?
  774. timers.OrderBy(i => i.Priority).ThenByStringDescending(i => i.Name) :
  775. timers.OrderByDescending(i => i.Priority).ThenByString(i => i.Name);
  776. }
  777. else
  778. {
  779. timers = query.SortOrder == SortOrder.Descending ?
  780. timers.OrderByStringDescending(i => i.Name) :
  781. timers.OrderByString(i => i.Name);
  782. }
  783. var returnArray = timers
  784. .Select(i =>
  785. {
  786. string channelName = null;
  787. if (!string.IsNullOrEmpty(i.ChannelId))
  788. {
  789. var internalChannelId = _tvDtoService.GetInternalChannelId(service.Name, i.ChannelId);
  790. var channel = GetInternalChannel(internalChannelId);
  791. channelName = channel == null ? null : channel.ChannelInfo.Name;
  792. }
  793. return _tvDtoService.GetSeriesTimerInfoDto(i, service, channelName);
  794. })
  795. .ToArray();
  796. return new QueryResult<SeriesTimerInfoDto>
  797. {
  798. Items = returnArray,
  799. TotalRecordCount = returnArray.Length
  800. };
  801. }
  802. public Task<ChannelInfoDto> GetChannel(string id, CancellationToken cancellationToken, User user = null)
  803. {
  804. var channel = GetInternalChannel(id);
  805. var dto = _tvDtoService.GetChannelInfoDto(channel, GetCurrentProgram(channel.ChannelInfo.Id), user);
  806. return Task.FromResult(dto);
  807. }
  808. private LiveTvProgram GetCurrentProgram(string externalChannelId)
  809. {
  810. var now = DateTime.UtcNow;
  811. return _programs.Values
  812. .Where(i => string.Equals(externalChannelId, i.ProgramInfo.ChannelId, StringComparison.OrdinalIgnoreCase))
  813. .OrderBy(i => i.ProgramInfo.StartDate)
  814. .SkipWhile(i => now >= i.ProgramInfo.EndDate)
  815. .FirstOrDefault();
  816. }
  817. private async Task<SeriesTimerInfo> GetNewTimerDefaultsInternal(CancellationToken cancellationToken, ProgramInfo program = null)
  818. {
  819. var info = await ActiveService.GetNewTimerDefaultsAsync(cancellationToken, program).ConfigureAwait(false);
  820. info.Id = null;
  821. return info;
  822. }
  823. public async Task<SeriesTimerInfoDto> GetNewTimerDefaults(CancellationToken cancellationToken)
  824. {
  825. var info = await GetNewTimerDefaultsInternal(cancellationToken).ConfigureAwait(false);
  826. var obj = _tvDtoService.GetSeriesTimerInfoDto(info, ActiveService, null);
  827. return obj;
  828. }
  829. public async Task<SeriesTimerInfoDto> GetNewTimerDefaults(string programId, CancellationToken cancellationToken)
  830. {
  831. var program = GetInternalProgram(programId).ProgramInfo;
  832. var programDto = await GetProgram(programId, cancellationToken).ConfigureAwait(false);
  833. var defaults = await GetNewTimerDefaultsInternal(cancellationToken, program).ConfigureAwait(false);
  834. var info = _tvDtoService.GetSeriesTimerInfoDto(defaults, ActiveService, null);
  835. info.Days = new List<DayOfWeek>
  836. {
  837. program.StartDate.ToLocalTime().DayOfWeek
  838. };
  839. info.DayPattern = _tvDtoService.GetDayPattern(info.Days);
  840. info.Name = program.Name;
  841. info.ChannelId = programDto.ChannelId;
  842. info.ChannelName = programDto.ChannelName;
  843. info.EndDate = program.EndDate;
  844. info.StartDate = program.StartDate;
  845. info.Name = program.Name;
  846. info.Overview = program.Overview;
  847. info.ProgramId = programDto.Id;
  848. info.ExternalProgramId = programDto.ExternalId;
  849. return info;
  850. }
  851. public async Task CreateTimer(TimerInfoDto timer, CancellationToken cancellationToken)
  852. {
  853. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  854. var info = await _tvDtoService.GetTimerInfo(timer, true, this, cancellationToken).ConfigureAwait(false);
  855. // Set priority from default values
  856. var defaultValues = await service.GetNewTimerDefaultsAsync(cancellationToken).ConfigureAwait(false);
  857. info.Priority = defaultValues.Priority;
  858. await service.CreateTimerAsync(info, cancellationToken).ConfigureAwait(false);
  859. }
  860. public async Task CreateSeriesTimer(SeriesTimerInfoDto timer, CancellationToken cancellationToken)
  861. {
  862. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  863. var info = await _tvDtoService.GetSeriesTimerInfo(timer, true, this, cancellationToken).ConfigureAwait(false);
  864. // Set priority from default values
  865. var defaultValues = await service.GetNewTimerDefaultsAsync(cancellationToken).ConfigureAwait(false);
  866. info.Priority = defaultValues.Priority;
  867. await service.CreateSeriesTimerAsync(info, cancellationToken).ConfigureAwait(false);
  868. }
  869. public async Task UpdateTimer(TimerInfoDto timer, CancellationToken cancellationToken)
  870. {
  871. var info = await _tvDtoService.GetTimerInfo(timer, false, this, cancellationToken).ConfigureAwait(false);
  872. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  873. await service.UpdateTimerAsync(info, cancellationToken).ConfigureAwait(false);
  874. }
  875. public async Task UpdateSeriesTimer(SeriesTimerInfoDto timer, CancellationToken cancellationToken)
  876. {
  877. var info = await _tvDtoService.GetSeriesTimerInfo(timer, false, this, cancellationToken).ConfigureAwait(false);
  878. var service = string.IsNullOrEmpty(timer.ServiceName) ? ActiveService : GetServices(timer.ServiceName, null).First();
  879. await service.UpdateSeriesTimerAsync(info, cancellationToken).ConfigureAwait(false);
  880. }
  881. private IEnumerable<string> GetRecordingGroupNames(RecordingInfo recording)
  882. {
  883. var list = new List<string>();
  884. if (recording.IsSeries)
  885. {
  886. list.Add(recording.Name);
  887. }
  888. if (recording.IsKids)
  889. {
  890. list.Add("Kids");
  891. }
  892. if (recording.IsMovie)
  893. {
  894. list.Add("Movies");
  895. }
  896. if (recording.IsNews)
  897. {
  898. list.Add("News");
  899. }
  900. if (recording.IsSports)
  901. {
  902. list.Add("Sports");
  903. }
  904. if (!recording.IsSports && !recording.IsNews && !recording.IsMovie && !recording.IsKids && !recording.IsSeries)
  905. {
  906. list.Add("Others");
  907. }
  908. return list;
  909. }
  910. private List<Guid> GetRecordingGroupIds(RecordingInfo recording)
  911. {
  912. return GetRecordingGroupNames(recording).Select(i => i.ToLower()
  913. .GetMD5())
  914. .ToList();
  915. }
  916. public async Task<QueryResult<RecordingGroupDto>> GetRecordingGroups(RecordingGroupQuery query, CancellationToken cancellationToken)
  917. {
  918. var recordingResult = await GetRecordings(new RecordingQuery
  919. {
  920. UserId = query.UserId
  921. }, cancellationToken).ConfigureAwait(false);
  922. var recordings = recordingResult.Items;
  923. var groups = new List<RecordingGroupDto>();
  924. var series = recordings
  925. .Where(i => i.IsSeries)
  926. .ToLookup(i => i.Name, StringComparer.OrdinalIgnoreCase)
  927. .ToList();
  928. groups.AddRange(series.OrderByString(i => i.Key).Select(i => new RecordingGroupDto
  929. {
  930. Name = i.Key,
  931. RecordingCount = i.Count()
  932. }));
  933. groups.Add(new RecordingGroupDto
  934. {
  935. Name = "Kids",
  936. RecordingCount = recordings.Count(i => i.IsKids)
  937. });
  938. groups.Add(new RecordingGroupDto
  939. {
  940. Name = "Movies",
  941. RecordingCount = recordings.Count(i => i.IsMovie)
  942. });
  943. groups.Add(new RecordingGroupDto
  944. {
  945. Name = "News",
  946. RecordingCount = recordings.Count(i => i.IsNews)
  947. });
  948. groups.Add(new RecordingGroupDto
  949. {
  950. Name = "Sports",
  951. RecordingCount = recordings.Count(i => i.IsSports)
  952. });
  953. groups.Add(new RecordingGroupDto
  954. {
  955. Name = "Others",
  956. RecordingCount = recordings.Count(i => !i.IsSports && !i.IsNews && !i.IsMovie && !i.IsKids && !i.IsSeries)
  957. });
  958. groups = groups
  959. .Where(i => i.RecordingCount > 0)
  960. .ToList();
  961. foreach (var group in groups)
  962. {
  963. group.Id = group.Name.ToLower().GetMD5().ToString("N");
  964. }
  965. return new QueryResult<RecordingGroupDto>
  966. {
  967. Items = groups.ToArray(),
  968. TotalRecordCount = groups.Count
  969. };
  970. }
  971. public async Task CloseLiveStream(string id, CancellationToken cancellationToken)
  972. {
  973. await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  974. var service = ActiveService;
  975. _logger.Info("Closing live stream from {0}, stream Id: {1}", service.Name, id);
  976. try
  977. {
  978. await service.CloseLiveStream(id, cancellationToken).ConfigureAwait(false);
  979. }
  980. catch (Exception ex)
  981. {
  982. _logger.ErrorException("Error closing live stream", ex);
  983. throw;
  984. }
  985. finally
  986. {
  987. _liveStreamSemaphore.Release();
  988. }
  989. }
  990. public GuideInfo GetGuideInfo()
  991. {
  992. var programs = _programs.ToList();
  993. var startDate = programs.Select(i => i.Value.ProgramInfo.StartDate).Min();
  994. var endDate = programs.Select(i => i.Value.ProgramInfo.StartDate).Max();
  995. return new GuideInfo
  996. {
  997. StartDate = startDate,
  998. EndDate = endDate
  999. };
  1000. }
  1001. /// <summary>
  1002. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  1003. /// </summary>
  1004. public void Dispose()
  1005. {
  1006. Dispose(true);
  1007. }
  1008. private readonly object _disposeLock = new object();
  1009. /// <summary>
  1010. /// Releases unmanaged and - optionally - managed resources.
  1011. /// </summary>
  1012. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  1013. protected virtual void Dispose(bool dispose)
  1014. {
  1015. if (dispose)
  1016. {
  1017. lock (_disposeLock)
  1018. {
  1019. foreach (var stream in _openStreams.Values.ToList())
  1020. {
  1021. var task = CloseLiveStream(stream.Id, CancellationToken.None);
  1022. Task.WaitAll(task);
  1023. }
  1024. _openStreams.Clear();
  1025. }
  1026. }
  1027. }
  1028. public async Task<IEnumerable<LiveTvServiceInfo>> GetServiceInfos(CancellationToken cancellationToken)
  1029. {
  1030. var tasks = Services.Select(i => GetServiceInfo(i, cancellationToken));
  1031. return await Task.WhenAll(tasks).ConfigureAwait(false);
  1032. }
  1033. private async Task<LiveTvServiceInfo> GetServiceInfo(ILiveTvService service, CancellationToken cancellationToken)
  1034. {
  1035. var info = new LiveTvServiceInfo
  1036. {
  1037. Name = service.Name
  1038. };
  1039. try
  1040. {
  1041. var statusInfo = await service.GetStatusInfoAsync(cancellationToken).ConfigureAwait(false);
  1042. info.Status = statusInfo.Status;
  1043. info.StatusMessage = statusInfo.StatusMessage;
  1044. info.Version = statusInfo.Version;
  1045. info.HasUpdateAvailable = statusInfo.HasUpdateAvailable;
  1046. info.HomePageUrl = service.HomePageUrl;
  1047. }
  1048. catch (Exception ex)
  1049. {
  1050. _logger.ErrorException("Error getting service status info from {0}", ex, service.Name);
  1051. info.Status = LiveTvServiceStatus.Unavailable;
  1052. info.StatusMessage = ex.Message;
  1053. }
  1054. return info;
  1055. }
  1056. }
  1057. }