LiveTvManager.cs 46 KB

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