LiveTvManager.cs 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.Extensions;
  4. using MediaBrowser.Common.Progress;
  5. using MediaBrowser.Common.ScheduledTasks;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.Drawing;
  8. using MediaBrowser.Controller.Dto;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.LiveTv;
  12. using MediaBrowser.Controller.Localization;
  13. using MediaBrowser.Controller.Persistence;
  14. using MediaBrowser.Controller.Providers;
  15. using MediaBrowser.Controller.Sorting;
  16. using MediaBrowser.Model.Dto;
  17. using MediaBrowser.Model.Entities;
  18. using MediaBrowser.Model.LiveTv;
  19. using MediaBrowser.Model.Logging;
  20. using MediaBrowser.Model.Querying;
  21. using MediaBrowser.Model.Serialization;
  22. using MoreLinq;
  23. using System;
  24. using System.Collections.Concurrent;
  25. using System.Collections.Generic;
  26. using System.IO;
  27. using System.Linq;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using CommonIO;
  31. using IniParser;
  32. using IniParser.Model;
  33. using MediaBrowser.Common.Events;
  34. using MediaBrowser.Common.Security;
  35. using MediaBrowser.Controller.Entities.Movies;
  36. using MediaBrowser.Controller.Entities.TV;
  37. using MediaBrowser.Model.Events;
  38. using MediaBrowser.Server.Implementations.LiveTv.Listings;
  39. namespace MediaBrowser.Server.Implementations.LiveTv
  40. {
  41. /// <summary>
  42. /// Class LiveTvManager
  43. /// </summary>
  44. public class LiveTvManager : ILiveTvManager, IDisposable
  45. {
  46. private readonly IServerConfigurationManager _config;
  47. private readonly ILogger _logger;
  48. private readonly IItemRepository _itemRepo;
  49. private readonly IUserManager _userManager;
  50. private readonly IUserDataManager _userDataManager;
  51. private readonly ILibraryManager _libraryManager;
  52. private readonly ITaskManager _taskManager;
  53. private readonly IJsonSerializer _jsonSerializer;
  54. private readonly IProviderManager _providerManager;
  55. private readonly ISecurityManager _security;
  56. private readonly IDtoService _dtoService;
  57. private readonly ILocalizationManager _localization;
  58. private readonly LiveTvDtoService _tvDtoService;
  59. private readonly List<ILiveTvService> _services = new List<ILiveTvService>();
  60. private readonly SemaphoreSlim _refreshRecordingsLock = new SemaphoreSlim(1, 1);
  61. private readonly List<ITunerHost> _tunerHosts = new List<ITunerHost>();
  62. private readonly List<IListingsProvider> _listingProviders = new List<IListingsProvider>();
  63. private readonly IFileSystem _fileSystem;
  64. public event EventHandler<GenericEventArgs<TimerEventInfo>> SeriesTimerCancelled;
  65. public event EventHandler<GenericEventArgs<TimerEventInfo>> TimerCancelled;
  66. public event EventHandler<GenericEventArgs<TimerEventInfo>> TimerCreated;
  67. public event EventHandler<GenericEventArgs<TimerEventInfo>> SeriesTimerCreated;
  68. public LiveTvManager(IApplicationHost appHost, IServerConfigurationManager config, ILogger logger, IItemRepository itemRepo, IImageProcessor imageProcessor, IUserDataManager userDataManager, IDtoService dtoService, IUserManager userManager, ILibraryManager libraryManager, ITaskManager taskManager, ILocalizationManager localization, IJsonSerializer jsonSerializer, IProviderManager providerManager, IFileSystem fileSystem, ISecurityManager security)
  69. {
  70. _config = config;
  71. _logger = logger;
  72. _itemRepo = itemRepo;
  73. _userManager = userManager;
  74. _libraryManager = libraryManager;
  75. _taskManager = taskManager;
  76. _localization = localization;
  77. _jsonSerializer = jsonSerializer;
  78. _providerManager = providerManager;
  79. _fileSystem = fileSystem;
  80. _security = security;
  81. _dtoService = dtoService;
  82. _userDataManager = userDataManager;
  83. _tvDtoService = new LiveTvDtoService(dtoService, userDataManager, imageProcessor, logger, appHost, _libraryManager);
  84. }
  85. /// <summary>
  86. /// Gets the services.
  87. /// </summary>
  88. /// <value>The services.</value>
  89. public IReadOnlyList<ILiveTvService> Services
  90. {
  91. get { return _services; }
  92. }
  93. private LiveTvOptions GetConfiguration()
  94. {
  95. return _config.GetConfiguration<LiveTvOptions>("livetv");
  96. }
  97. /// <summary>
  98. /// Adds the parts.
  99. /// </summary>
  100. /// <param name="services">The services.</param>
  101. /// <param name="tunerHosts">The tuner hosts.</param>
  102. /// <param name="listingProviders">The listing providers.</param>
  103. public void AddParts(IEnumerable<ILiveTvService> services, IEnumerable<ITunerHost> tunerHosts, IEnumerable<IListingsProvider> listingProviders)
  104. {
  105. _services.AddRange(services);
  106. _tunerHosts.AddRange(tunerHosts);
  107. _listingProviders.AddRange(listingProviders);
  108. foreach (var service in _services)
  109. {
  110. service.DataSourceChanged += service_DataSourceChanged;
  111. }
  112. }
  113. public List<ITunerHost> TunerHosts
  114. {
  115. get { return _tunerHosts; }
  116. }
  117. public List<IListingsProvider> ListingProviders
  118. {
  119. get { return _listingProviders; }
  120. }
  121. void service_DataSourceChanged(object sender, EventArgs e)
  122. {
  123. if (!_isDisposed)
  124. {
  125. _taskManager.CancelIfRunningAndQueue<RefreshChannelsScheduledTask>();
  126. }
  127. }
  128. public async Task<QueryResult<LiveTvChannel>> GetInternalChannels(LiveTvChannelQuery query, CancellationToken cancellationToken)
  129. {
  130. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId);
  131. var topFolder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false);
  132. var internalQuery = new InternalItemsQuery(user)
  133. {
  134. IsMovie = query.IsMovie,
  135. IsNews = query.IsNews,
  136. IsKids = query.IsKids,
  137. IsSports = query.IsSports,
  138. IsSeries = query.IsSeries,
  139. IncludeItemTypes = new[] { typeof(LiveTvChannel).Name },
  140. SortOrder = query.SortOrder ?? SortOrder.Ascending,
  141. TopParentIds = new[] { topFolder.Id.ToString("N") },
  142. IsFavorite = query.IsFavorite,
  143. IsLiked = query.IsLiked,
  144. StartIndex = query.StartIndex,
  145. Limit = query.Limit
  146. };
  147. internalQuery.OrderBy.AddRange(query.SortBy.Select(i => new Tuple<string, SortOrder>(i, query.SortOrder ?? SortOrder.Ascending)));
  148. if (query.EnableFavoriteSorting)
  149. {
  150. internalQuery.OrderBy.Insert(0, new Tuple<string, SortOrder>(ItemSortBy.IsFavoriteOrLiked, SortOrder.Descending));
  151. }
  152. if (!internalQuery.OrderBy.Any(i => string.Equals(i.Item1, ItemSortBy.SortName, StringComparison.OrdinalIgnoreCase)))
  153. {
  154. internalQuery.OrderBy.Add(new Tuple<string, SortOrder>(ItemSortBy.SortName, SortOrder.Ascending));
  155. }
  156. var channelResult = _libraryManager.GetItemsResult(internalQuery);
  157. var result = new QueryResult<LiveTvChannel>
  158. {
  159. Items = channelResult.Items.Cast<LiveTvChannel>().ToArray(),
  160. TotalRecordCount = channelResult.TotalRecordCount
  161. };
  162. return result;
  163. }
  164. public LiveTvChannel GetInternalChannel(string id)
  165. {
  166. return GetInternalChannel(new Guid(id));
  167. }
  168. private LiveTvChannel GetInternalChannel(Guid id)
  169. {
  170. return _libraryManager.GetItemById(id) as LiveTvChannel;
  171. }
  172. internal LiveTvProgram GetInternalProgram(string id)
  173. {
  174. return _libraryManager.GetItemById(id) as LiveTvProgram;
  175. }
  176. internal LiveTvProgram GetInternalProgram(Guid id)
  177. {
  178. return _libraryManager.GetItemById(id) as LiveTvProgram;
  179. }
  180. public async Task<BaseItem> GetInternalRecording(string id, CancellationToken cancellationToken)
  181. {
  182. if (string.IsNullOrWhiteSpace(id))
  183. {
  184. throw new ArgumentNullException("id");
  185. }
  186. var result = await GetInternalRecordings(new RecordingQuery
  187. {
  188. Id = id
  189. }, cancellationToken).ConfigureAwait(false);
  190. return result.Items.FirstOrDefault();
  191. }
  192. private readonly SemaphoreSlim _liveStreamSemaphore = new SemaphoreSlim(1, 1);
  193. public async Task<MediaSourceInfo> GetRecordingStream(string id, CancellationToken cancellationToken)
  194. {
  195. return await GetLiveStream(id, null, false, cancellationToken).ConfigureAwait(false);
  196. }
  197. public async Task<MediaSourceInfo> GetChannelStream(string id, string mediaSourceId, CancellationToken cancellationToken)
  198. {
  199. return await GetLiveStream(id, mediaSourceId, true, cancellationToken).ConfigureAwait(false);
  200. }
  201. public async Task<IEnumerable<MediaSourceInfo>> GetRecordingMediaSources(string id, CancellationToken cancellationToken)
  202. {
  203. var item = await GetInternalRecording(id, cancellationToken).ConfigureAwait(false);
  204. var service = GetService(item);
  205. return await service.GetRecordingStreamMediaSources(id, cancellationToken).ConfigureAwait(false);
  206. }
  207. public async Task<IEnumerable<MediaSourceInfo>> GetChannelMediaSources(string id, CancellationToken cancellationToken)
  208. {
  209. var item = GetInternalChannel(id);
  210. var service = GetService(item);
  211. var sources = await service.GetChannelStreamMediaSources(item.ExternalId, cancellationToken).ConfigureAwait(false);
  212. if (sources.Count == 0)
  213. {
  214. throw new NotImplementedException();
  215. }
  216. var list = sources.ToList();
  217. foreach (var source in list)
  218. {
  219. Normalize(source, service, item.ChannelType == ChannelType.TV);
  220. }
  221. return list;
  222. }
  223. private ILiveTvService GetService(ILiveTvRecording item)
  224. {
  225. return GetService(item.ServiceName);
  226. }
  227. private ILiveTvService GetService(BaseItem item)
  228. {
  229. return GetService(item.ServiceName);
  230. }
  231. private ILiveTvService GetService(string name)
  232. {
  233. return _services.FirstOrDefault(i => string.Equals(i.Name, name, StringComparison.OrdinalIgnoreCase));
  234. }
  235. private async Task<MediaSourceInfo> GetLiveStream(string id, string mediaSourceId, bool isChannel, CancellationToken cancellationToken)
  236. {
  237. await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  238. if (string.Equals(id, mediaSourceId, StringComparison.OrdinalIgnoreCase))
  239. {
  240. mediaSourceId = null;
  241. }
  242. try
  243. {
  244. MediaSourceInfo info;
  245. bool isVideo;
  246. ILiveTvService service;
  247. if (isChannel)
  248. {
  249. var channel = GetInternalChannel(id);
  250. isVideo = channel.ChannelType == ChannelType.TV;
  251. service = GetService(channel);
  252. _logger.Info("Opening channel stream from {0}, external channel Id: {1}", service.Name, channel.ExternalId);
  253. info = await service.GetChannelStream(channel.ExternalId, mediaSourceId, cancellationToken).ConfigureAwait(false);
  254. info.RequiresClosing = true;
  255. if (info.RequiresClosing)
  256. {
  257. var idPrefix = service.GetType().FullName.GetMD5().ToString("N") + "_";
  258. info.LiveStreamId = idPrefix + info.Id;
  259. }
  260. }
  261. else
  262. {
  263. var recording = await GetInternalRecording(id, cancellationToken).ConfigureAwait(false);
  264. isVideo = !string.Equals(recording.MediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase);
  265. service = GetService(recording);
  266. _logger.Info("Opening recording stream from {0}, external recording Id: {1}", service.Name, recording.ExternalId);
  267. info = await service.GetRecordingStream(recording.ExternalId, null, cancellationToken).ConfigureAwait(false);
  268. info.RequiresClosing = true;
  269. if (info.RequiresClosing)
  270. {
  271. var idPrefix = service.GetType().FullName.GetMD5().ToString("N") + "_";
  272. info.LiveStreamId = idPrefix + info.Id;
  273. }
  274. }
  275. _logger.Info("Live stream info: {0}", _jsonSerializer.SerializeToString(info));
  276. Normalize(info, service, isVideo);
  277. return info;
  278. }
  279. catch (Exception ex)
  280. {
  281. _logger.ErrorException("Error getting channel stream", ex);
  282. throw;
  283. }
  284. finally
  285. {
  286. _liveStreamSemaphore.Release();
  287. }
  288. }
  289. private void Normalize(MediaSourceInfo mediaSource, ILiveTvService service, bool isVideo)
  290. {
  291. if (mediaSource.MediaStreams.Count == 0)
  292. {
  293. if (isVideo)
  294. {
  295. mediaSource.MediaStreams.AddRange(new List<MediaStream>
  296. {
  297. new MediaStream
  298. {
  299. Type = MediaStreamType.Video,
  300. // Set the index to -1 because we don't know the exact index of the video stream within the container
  301. Index = -1,
  302. // Set to true if unknown to enable deinterlacing
  303. IsInterlaced = true
  304. },
  305. new MediaStream
  306. {
  307. Type = MediaStreamType.Audio,
  308. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  309. Index = -1
  310. }
  311. });
  312. }
  313. else
  314. {
  315. mediaSource.MediaStreams.AddRange(new List<MediaStream>
  316. {
  317. new MediaStream
  318. {
  319. Type = MediaStreamType.Audio,
  320. // Set the index to -1 because we don't know the exact index of the audio stream within the container
  321. Index = -1
  322. }
  323. });
  324. }
  325. }
  326. // Clean some bad data coming from providers
  327. foreach (var stream in mediaSource.MediaStreams)
  328. {
  329. if (stream.BitRate.HasValue && stream.BitRate <= 0)
  330. {
  331. stream.BitRate = null;
  332. }
  333. if (stream.Channels.HasValue && stream.Channels <= 0)
  334. {
  335. stream.Channels = null;
  336. }
  337. if (stream.AverageFrameRate.HasValue && stream.AverageFrameRate <= 0)
  338. {
  339. stream.AverageFrameRate = null;
  340. }
  341. if (stream.RealFrameRate.HasValue && stream.RealFrameRate <= 0)
  342. {
  343. stream.RealFrameRate = null;
  344. }
  345. if (stream.Width.HasValue && stream.Width <= 0)
  346. {
  347. stream.Width = null;
  348. }
  349. if (stream.Height.HasValue && stream.Height <= 0)
  350. {
  351. stream.Height = null;
  352. }
  353. if (stream.SampleRate.HasValue && stream.SampleRate <= 0)
  354. {
  355. stream.SampleRate = null;
  356. }
  357. if (stream.Level.HasValue && stream.Level <= 0)
  358. {
  359. stream.Level = null;
  360. }
  361. }
  362. var indexes = mediaSource.MediaStreams.Select(i => i.Index).Distinct().ToList();
  363. // If there are duplicate stream indexes, set them all to unknown
  364. if (indexes.Count != mediaSource.MediaStreams.Count)
  365. {
  366. foreach (var stream in mediaSource.MediaStreams)
  367. {
  368. stream.Index = -1;
  369. }
  370. }
  371. // Set the total bitrate if not already supplied
  372. if (!mediaSource.Bitrate.HasValue)
  373. {
  374. var total = mediaSource.MediaStreams.Select(i => i.BitRate ?? 0).Sum();
  375. if (total > 0)
  376. {
  377. mediaSource.Bitrate = total;
  378. }
  379. }
  380. if (!(service is EmbyTV.EmbyTV))
  381. {
  382. // We can't trust that we'll be able to direct stream it through emby server, no matter what the provider says
  383. mediaSource.SupportsDirectStream = false;
  384. mediaSource.SupportsTranscoding = true;
  385. foreach (var stream in mediaSource.MediaStreams)
  386. {
  387. if (stream.Type == MediaStreamType.Video && string.IsNullOrWhiteSpace(stream.NalLengthSize))
  388. {
  389. stream.NalLengthSize = "0";
  390. }
  391. }
  392. }
  393. }
  394. private async Task<LiveTvChannel> GetChannel(ChannelInfo channelInfo, string serviceName, Guid parentFolderId, CancellationToken cancellationToken)
  395. {
  396. var isNew = false;
  397. var forceUpdate = false;
  398. var id = _tvDtoService.GetInternalChannelId(serviceName, channelInfo.Id);
  399. var item = _itemRepo.RetrieveItem(id) as LiveTvChannel;
  400. if (item == null)
  401. {
  402. item = new LiveTvChannel
  403. {
  404. Name = channelInfo.Name,
  405. Id = id,
  406. DateCreated = DateTime.UtcNow,
  407. };
  408. isNew = true;
  409. }
  410. if (!string.Equals(channelInfo.Id, item.ExternalId))
  411. {
  412. isNew = true;
  413. }
  414. item.ExternalId = channelInfo.Id;
  415. if (!item.ParentId.Equals(parentFolderId))
  416. {
  417. isNew = true;
  418. }
  419. item.ParentId = parentFolderId;
  420. item.ChannelType = channelInfo.ChannelType;
  421. item.ServiceName = serviceName;
  422. item.Number = channelInfo.Number;
  423. //if (!string.Equals(item.ProviderImageUrl, channelInfo.ImageUrl, StringComparison.OrdinalIgnoreCase))
  424. //{
  425. // isNew = true;
  426. // replaceImages.Add(ImageType.Primary);
  427. //}
  428. //if (!string.Equals(item.ProviderImagePath, channelInfo.ImagePath, StringComparison.OrdinalIgnoreCase))
  429. //{
  430. // isNew = true;
  431. // replaceImages.Add(ImageType.Primary);
  432. //}
  433. if (!item.HasImage(ImageType.Primary))
  434. {
  435. if (!string.IsNullOrWhiteSpace(channelInfo.ImagePath))
  436. {
  437. item.SetImagePath(ImageType.Primary, channelInfo.ImagePath);
  438. forceUpdate = true;
  439. }
  440. else if (!string.IsNullOrWhiteSpace(channelInfo.ImageUrl))
  441. {
  442. item.SetImagePath(ImageType.Primary, channelInfo.ImageUrl);
  443. forceUpdate = true;
  444. }
  445. }
  446. if (string.IsNullOrEmpty(item.Name))
  447. {
  448. item.Name = channelInfo.Name;
  449. }
  450. if (isNew)
  451. {
  452. await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
  453. }
  454. else if (forceUpdate)
  455. {
  456. await _libraryManager.UpdateItem(item, ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false);
  457. }
  458. await item.RefreshMetadata(new MetadataRefreshOptions(_fileSystem)
  459. {
  460. ForceSave = isNew || forceUpdate
  461. }, cancellationToken);
  462. return item;
  463. }
  464. private async Task<LiveTvProgram> GetProgram(ProgramInfo info, LiveTvChannel channel, ChannelType channelType, string serviceName, CancellationToken cancellationToken)
  465. {
  466. var id = _tvDtoService.GetInternalProgramId(serviceName, info.Id);
  467. var item = _libraryManager.GetItemById(id) as LiveTvProgram;
  468. var isNew = false;
  469. var forceUpdate = false;
  470. if (item == null)
  471. {
  472. isNew = true;
  473. item = new LiveTvProgram
  474. {
  475. Name = info.Name,
  476. Id = id,
  477. DateCreated = DateTime.UtcNow,
  478. DateModified = DateTime.UtcNow,
  479. ExternalEtag = info.Etag
  480. };
  481. }
  482. var seriesId = info.SeriesId;
  483. if (string.IsNullOrWhiteSpace(seriesId) && info.IsSeries)
  484. {
  485. seriesId = info.Name.GetMD5().ToString("N");
  486. }
  487. if (!item.ParentId.Equals(channel.Id))
  488. {
  489. forceUpdate = true;
  490. }
  491. item.ParentId = channel.Id;
  492. //item.ChannelType = channelType;
  493. if (!string.Equals(item.ServiceName, serviceName, StringComparison.Ordinal))
  494. {
  495. forceUpdate = true;
  496. }
  497. item.ServiceName = serviceName;
  498. item.Audio = info.Audio;
  499. item.ChannelId = channel.Id.ToString("N");
  500. item.CommunityRating = item.CommunityRating ?? info.CommunityRating;
  501. item.EpisodeTitle = info.EpisodeTitle;
  502. item.ExternalId = info.Id;
  503. item.ExternalSeriesIdLegacy = seriesId;
  504. if (!string.IsNullOrWhiteSpace(seriesId) && !string.Equals(item.ExternalSeriesId, seriesId, StringComparison.Ordinal))
  505. {
  506. forceUpdate = true;
  507. }
  508. item.ExternalSeriesId = seriesId;
  509. item.Genres = info.Genres;
  510. item.IsHD = info.IsHD;
  511. item.IsKids = info.IsKids;
  512. item.IsLive = info.IsLive;
  513. item.IsMovie = info.IsMovie;
  514. item.IsNews = info.IsNews;
  515. item.IsPremiere = info.IsPremiere;
  516. item.IsRepeat = info.IsRepeat;
  517. item.IsSeries = info.IsSeries;
  518. item.IsSports = info.IsSports;
  519. item.Name = info.Name;
  520. item.OfficialRating = item.OfficialRating ?? info.OfficialRating;
  521. item.Overview = item.Overview ?? info.Overview;
  522. item.RunTimeTicks = (info.EndDate - info.StartDate).Ticks;
  523. if (item.StartDate != info.StartDate)
  524. {
  525. forceUpdate = true;
  526. }
  527. item.StartDate = info.StartDate;
  528. if (item.EndDate != info.EndDate)
  529. {
  530. forceUpdate = true;
  531. }
  532. item.EndDate = info.EndDate;
  533. item.HomePageUrl = info.HomePageUrl;
  534. item.ProductionYear = info.ProductionYear;
  535. if (!info.IsSeries || info.IsRepeat)
  536. {
  537. item.PremiereDate = info.OriginalAirDate;
  538. }
  539. item.IndexNumber = info.EpisodeNumber;
  540. item.ParentIndexNumber = info.SeasonNumber;
  541. if (!item.HasImage(ImageType.Primary))
  542. {
  543. if (!string.IsNullOrWhiteSpace(info.ImagePath))
  544. {
  545. item.SetImage(new ItemImageInfo
  546. {
  547. Path = info.ImagePath,
  548. Type = ImageType.Primary,
  549. IsPlaceholder = true
  550. }, 0);
  551. }
  552. else if (!string.IsNullOrWhiteSpace(info.ImageUrl))
  553. {
  554. item.SetImage(new ItemImageInfo
  555. {
  556. Path = info.ImageUrl,
  557. Type = ImageType.Primary,
  558. IsPlaceholder = true
  559. }, 0);
  560. }
  561. }
  562. if (isNew)
  563. {
  564. await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
  565. }
  566. else if (forceUpdate || string.IsNullOrWhiteSpace(info.Etag))
  567. {
  568. await _libraryManager.UpdateItem(item, ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false);
  569. }
  570. else
  571. {
  572. // Increment this whenver some internal change deems it necessary
  573. var etag = info.Etag + "4";
  574. if (!string.Equals(etag, item.ExternalEtag, StringComparison.OrdinalIgnoreCase))
  575. {
  576. item.ExternalEtag = etag;
  577. await _libraryManager.UpdateItem(item, ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false);
  578. }
  579. }
  580. _providerManager.QueueRefresh(item.Id, new MetadataRefreshOptions(_fileSystem));
  581. return item;
  582. }
  583. private async Task<Guid> CreateRecordingRecord(RecordingInfo info, string serviceName, Guid parentFolderId, CancellationToken cancellationToken)
  584. {
  585. var isNew = false;
  586. var id = _tvDtoService.GetInternalRecordingId(serviceName, info.Id);
  587. var item = _itemRepo.RetrieveItem(id);
  588. if (item == null)
  589. {
  590. if (info.ChannelType == ChannelType.TV)
  591. {
  592. item = new LiveTvVideoRecording
  593. {
  594. Name = info.Name,
  595. Id = id,
  596. DateCreated = DateTime.UtcNow,
  597. DateModified = DateTime.UtcNow,
  598. VideoType = VideoType.VideoFile
  599. };
  600. }
  601. else
  602. {
  603. item = new LiveTvAudioRecording
  604. {
  605. Name = info.Name,
  606. Id = id,
  607. DateCreated = DateTime.UtcNow,
  608. DateModified = DateTime.UtcNow
  609. };
  610. }
  611. isNew = true;
  612. }
  613. item.ChannelId = _tvDtoService.GetInternalChannelId(serviceName, info.ChannelId).ToString("N");
  614. item.CommunityRating = info.CommunityRating;
  615. item.OfficialRating = info.OfficialRating;
  616. item.Overview = info.Overview;
  617. item.EndDate = info.EndDate;
  618. item.Genres = info.Genres;
  619. item.PremiereDate = info.OriginalAirDate;
  620. var recording = (ILiveTvRecording)item;
  621. recording.ExternalId = info.Id;
  622. var dataChanged = false;
  623. recording.Audio = info.Audio;
  624. recording.EndDate = info.EndDate;
  625. recording.EpisodeTitle = info.EpisodeTitle;
  626. recording.IsHD = info.IsHD;
  627. recording.IsKids = info.IsKids;
  628. recording.IsLive = info.IsLive;
  629. recording.IsMovie = info.IsMovie;
  630. recording.IsNews = info.IsNews;
  631. recording.IsPremiere = info.IsPremiere;
  632. recording.IsRepeat = info.IsRepeat;
  633. recording.IsSports = info.IsSports;
  634. recording.SeriesTimerId = info.SeriesTimerId;
  635. recording.StartDate = info.StartDate;
  636. if (!dataChanged)
  637. {
  638. dataChanged = recording.IsSeries != info.IsSeries;
  639. }
  640. recording.IsSeries = info.IsSeries;
  641. if (!item.ParentId.Equals(parentFolderId))
  642. {
  643. dataChanged = true;
  644. }
  645. item.ParentId = parentFolderId;
  646. if (!item.HasImage(ImageType.Primary))
  647. {
  648. if (!string.IsNullOrWhiteSpace(info.ImagePath))
  649. {
  650. item.SetImage(new ItemImageInfo
  651. {
  652. Path = info.ImagePath,
  653. Type = ImageType.Primary,
  654. IsPlaceholder = true
  655. }, 0);
  656. }
  657. else if (!string.IsNullOrWhiteSpace(info.ImageUrl))
  658. {
  659. item.SetImage(new ItemImageInfo
  660. {
  661. Path = info.ImageUrl,
  662. Type = ImageType.Primary,
  663. IsPlaceholder = true
  664. }, 0);
  665. }
  666. }
  667. var statusChanged = info.Status != recording.Status;
  668. recording.Status = info.Status;
  669. recording.ServiceName = serviceName;
  670. if (!string.IsNullOrEmpty(info.Path))
  671. {
  672. if (!dataChanged)
  673. {
  674. dataChanged = !string.Equals(item.Path, info.Path);
  675. }
  676. var fileInfo = _fileSystem.GetFileInfo(info.Path);
  677. recording.DateCreated = _fileSystem.GetCreationTimeUtc(fileInfo);
  678. recording.DateModified = _fileSystem.GetLastWriteTimeUtc(fileInfo);
  679. item.Path = info.Path;
  680. }
  681. else if (!string.IsNullOrEmpty(info.Url))
  682. {
  683. if (!dataChanged)
  684. {
  685. dataChanged = !string.Equals(item.Path, info.Url);
  686. }
  687. item.Path = info.Url;
  688. }
  689. var metadataRefreshMode = MetadataRefreshMode.Default;
  690. if (isNew)
  691. {
  692. await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
  693. }
  694. else if (dataChanged || info.DateLastUpdated > recording.DateLastSaved || statusChanged)
  695. {
  696. metadataRefreshMode = MetadataRefreshMode.FullRefresh;
  697. await _libraryManager.UpdateItem(item, ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false);
  698. }
  699. if (info.Status != RecordingStatus.InProgress)
  700. {
  701. _providerManager.QueueRefresh(item.Id, new MetadataRefreshOptions(_fileSystem)
  702. {
  703. MetadataRefreshMode = metadataRefreshMode
  704. });
  705. }
  706. return item.Id;
  707. }
  708. public async Task<BaseItemDto> GetProgram(string id, CancellationToken cancellationToken, User user = null)
  709. {
  710. var program = GetInternalProgram(id);
  711. var dto = _dtoService.GetBaseItemDto(program, new DtoOptions(), user);
  712. var list = new List<Tuple<BaseItemDto, string, string, string>>();
  713. list.Add(new Tuple<BaseItemDto, string, string, string>(dto, program.ServiceName, program.ExternalId, program.ExternalSeriesIdLegacy));
  714. await AddRecordingInfo(list, cancellationToken).ConfigureAwait(false);
  715. return dto;
  716. }
  717. public async Task<QueryResult<BaseItemDto>> GetPrograms(ProgramQuery query, DtoOptions options, CancellationToken cancellationToken)
  718. {
  719. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId);
  720. var topFolder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false);
  721. if (query.SortBy.Length == 0)
  722. {
  723. // Unless something else was specified, order by start date to take advantage of a specialized index
  724. query.SortBy = new[] { ItemSortBy.StartDate };
  725. }
  726. var internalQuery = new InternalItemsQuery(user)
  727. {
  728. IncludeItemTypes = new[] { typeof(LiveTvProgram).Name },
  729. MinEndDate = query.MinEndDate,
  730. MinStartDate = query.MinStartDate,
  731. MaxEndDate = query.MaxEndDate,
  732. MaxStartDate = query.MaxStartDate,
  733. ChannelIds = query.ChannelIds,
  734. IsMovie = query.IsMovie,
  735. IsSeries = query.IsSeries,
  736. IsSports = query.IsSports,
  737. IsKids = query.IsKids,
  738. IsNews = query.IsNews,
  739. Genres = query.Genres,
  740. StartIndex = query.StartIndex,
  741. Limit = query.Limit,
  742. SortBy = query.SortBy,
  743. SortOrder = query.SortOrder ?? SortOrder.Ascending,
  744. EnableTotalRecordCount = query.EnableTotalRecordCount,
  745. TopParentIds = new[] { topFolder.Id.ToString("N") }
  746. };
  747. if (!string.IsNullOrWhiteSpace(query.SeriesTimerId))
  748. {
  749. var seriesTimers = await GetSeriesTimersInternal(new SeriesTimerQuery {}, cancellationToken).ConfigureAwait(false);
  750. var seriesTimer = seriesTimers.Items.FirstOrDefault(i => string.Equals(_tvDtoService.GetInternalSeriesTimerId(i.ServiceName, i.Id).ToString("N"), query.SeriesTimerId, StringComparison.OrdinalIgnoreCase));
  751. if (seriesTimer != null)
  752. {
  753. internalQuery.ExternalSeriesId = seriesTimer.SeriesId;
  754. if (string.IsNullOrWhiteSpace(seriesTimer.SeriesId))
  755. {
  756. // Better to return nothing than every program in the database
  757. return new QueryResult<BaseItemDto>();
  758. }
  759. }
  760. else
  761. {
  762. // Better to return nothing than every program in the database
  763. return new QueryResult<BaseItemDto>();
  764. }
  765. }
  766. if (query.HasAired.HasValue)
  767. {
  768. if (query.HasAired.Value)
  769. {
  770. internalQuery.MaxEndDate = DateTime.UtcNow;
  771. }
  772. else
  773. {
  774. internalQuery.MinEndDate = DateTime.UtcNow;
  775. }
  776. }
  777. var queryResult = _libraryManager.QueryItems(internalQuery);
  778. RemoveFields(options);
  779. var returnArray = (await _dtoService.GetBaseItemDtos(queryResult.Items, options, user).ConfigureAwait(false)).ToArray();
  780. var result = new QueryResult<BaseItemDto>
  781. {
  782. Items = returnArray,
  783. TotalRecordCount = queryResult.TotalRecordCount
  784. };
  785. return result;
  786. }
  787. public async Task<QueryResult<LiveTvProgram>> GetRecommendedProgramsInternal(RecommendedProgramQuery query, CancellationToken cancellationToken)
  788. {
  789. var user = _userManager.GetUserById(query.UserId);
  790. var topFolder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false);
  791. var internalQuery = new InternalItemsQuery(user)
  792. {
  793. IncludeItemTypes = new[] { typeof(LiveTvProgram).Name },
  794. IsAiring = query.IsAiring,
  795. IsNews = query.IsNews,
  796. IsMovie = query.IsMovie,
  797. IsSeries = query.IsSeries,
  798. IsSports = query.IsSports,
  799. IsKids = query.IsKids,
  800. EnableTotalRecordCount = query.EnableTotalRecordCount,
  801. SortBy = new[] { ItemSortBy.StartDate },
  802. TopParentIds = new[] { topFolder.Id.ToString("N") }
  803. };
  804. if (query.Limit.HasValue)
  805. {
  806. internalQuery.Limit = Math.Max(query.Limit.Value * 4, 200);
  807. }
  808. if (query.HasAired.HasValue)
  809. {
  810. if (query.HasAired.Value)
  811. {
  812. internalQuery.MaxEndDate = DateTime.UtcNow;
  813. }
  814. else
  815. {
  816. internalQuery.MinEndDate = DateTime.UtcNow;
  817. }
  818. }
  819. IEnumerable<LiveTvProgram> programs = _libraryManager.QueryItems(internalQuery).Items.Cast<LiveTvProgram>();
  820. var programList = programs.ToList();
  821. var factorChannelWatchCount = (query.IsAiring ?? false) || (query.IsKids ?? false) || (query.IsSports ?? false) || (query.IsMovie ?? false) || (query.IsNews ?? false) || (query.IsSeries ?? false);
  822. programs = programList.OrderBy(i => i.StartDate.Date)
  823. .ThenByDescending(i => GetRecommendationScore(i, user.Id, factorChannelWatchCount))
  824. .ThenBy(i => i.StartDate);
  825. if (query.Limit.HasValue)
  826. {
  827. programs = programs.Take(query.Limit.Value);
  828. }
  829. programList = programs.ToList();
  830. var returnArray = programList.ToArray();
  831. var result = new QueryResult<LiveTvProgram>
  832. {
  833. Items = returnArray,
  834. TotalRecordCount = returnArray.Length
  835. };
  836. return result;
  837. }
  838. public async Task<QueryResult<BaseItemDto>> GetRecommendedPrograms(RecommendedProgramQuery query, DtoOptions options, CancellationToken cancellationToken)
  839. {
  840. var internalResult = await GetRecommendedProgramsInternal(query, cancellationToken).ConfigureAwait(false);
  841. var user = _userManager.GetUserById(query.UserId);
  842. RemoveFields(options);
  843. var returnArray = (await _dtoService.GetBaseItemDtos(internalResult.Items, options, user).ConfigureAwait(false)).ToArray();
  844. var result = new QueryResult<BaseItemDto>
  845. {
  846. Items = returnArray,
  847. TotalRecordCount = internalResult.TotalRecordCount
  848. };
  849. return result;
  850. }
  851. private int GetRecommendationScore(LiveTvProgram program, Guid userId, bool factorChannelWatchCount)
  852. {
  853. var score = 0;
  854. if (program.IsLive)
  855. {
  856. score++;
  857. }
  858. if (program.IsSeries && !program.IsRepeat)
  859. {
  860. score++;
  861. }
  862. var channel = GetInternalChannel(program.ChannelId);
  863. var channelUserdata = _userDataManager.GetUserData(userId, channel);
  864. if (channelUserdata.Likes ?? false)
  865. {
  866. score += 2;
  867. }
  868. else if (!(channelUserdata.Likes ?? true))
  869. {
  870. score -= 2;
  871. }
  872. if (channelUserdata.IsFavorite)
  873. {
  874. score += 3;
  875. }
  876. if (factorChannelWatchCount)
  877. {
  878. score += channelUserdata.PlayCount;
  879. }
  880. return score;
  881. }
  882. private async Task AddRecordingInfo(IEnumerable<Tuple<BaseItemDto, string, string, string>> programs, CancellationToken cancellationToken)
  883. {
  884. var timers = new Dictionary<string, List<TimerInfo>>();
  885. var seriesTimers = new Dictionary<string, List<SeriesTimerInfo>>();
  886. foreach (var programTuple in programs)
  887. {
  888. var program = programTuple.Item1;
  889. var serviceName = programTuple.Item2;
  890. var externalProgramId = programTuple.Item3;
  891. string externalSeriesId = programTuple.Item4;
  892. if (string.IsNullOrWhiteSpace(serviceName))
  893. {
  894. continue;
  895. }
  896. List<TimerInfo> timerList;
  897. if (!timers.TryGetValue(serviceName, out timerList))
  898. {
  899. try
  900. {
  901. var tempTimers = await GetService(serviceName).GetTimersAsync(cancellationToken).ConfigureAwait(false);
  902. timers[serviceName] = timerList = tempTimers.ToList();
  903. }
  904. catch (Exception ex)
  905. {
  906. _logger.ErrorException("Error getting timer infos", ex);
  907. timers[serviceName] = timerList = new List<TimerInfo>();
  908. }
  909. }
  910. var timer = timerList.FirstOrDefault(i => string.Equals(i.ProgramId, externalProgramId, StringComparison.OrdinalIgnoreCase));
  911. var foundSeriesTimer = false;
  912. if (timer != null)
  913. {
  914. program.TimerId = _tvDtoService.GetInternalTimerId(serviceName, timer.Id)
  915. .ToString("N");
  916. if (!string.IsNullOrEmpty(timer.SeriesTimerId))
  917. {
  918. program.SeriesTimerId = _tvDtoService.GetInternalSeriesTimerId(serviceName, timer.SeriesTimerId)
  919. .ToString("N");
  920. foundSeriesTimer = true;
  921. }
  922. }
  923. if (foundSeriesTimer || string.IsNullOrWhiteSpace(externalSeriesId))
  924. {
  925. continue;
  926. }
  927. List<SeriesTimerInfo> seriesTimerList;
  928. if (!seriesTimers.TryGetValue(serviceName, out seriesTimerList))
  929. {
  930. try
  931. {
  932. var tempTimers = await GetService(serviceName).GetSeriesTimersAsync(cancellationToken).ConfigureAwait(false);
  933. seriesTimers[serviceName] = seriesTimerList = tempTimers.ToList();
  934. }
  935. catch (Exception ex)
  936. {
  937. _logger.ErrorException("Error getting series timer infos", ex);
  938. seriesTimers[serviceName] = seriesTimerList = new List<SeriesTimerInfo>();
  939. }
  940. }
  941. var seriesTimer = seriesTimerList.FirstOrDefault(i => string.Equals(i.SeriesId, externalSeriesId, StringComparison.OrdinalIgnoreCase));
  942. if (seriesTimer != null)
  943. {
  944. program.SeriesTimerId = _tvDtoService.GetInternalSeriesTimerId(serviceName, seriesTimer.Id)
  945. .ToString("N");
  946. }
  947. }
  948. }
  949. internal Task RefreshChannels(IProgress<double> progress, CancellationToken cancellationToken)
  950. {
  951. return RefreshChannelsInternal(progress, cancellationToken);
  952. }
  953. private async Task RefreshChannelsInternal(IProgress<double> progress, CancellationToken cancellationToken)
  954. {
  955. EmbyTV.EmbyTV.Current.CreateRecordingFolders();
  956. var numComplete = 0;
  957. double progressPerService = _services.Count == 0
  958. ? 0
  959. : 1 / _services.Count;
  960. var newChannelIdList = new List<Guid>();
  961. var newProgramIdList = new List<Guid>();
  962. foreach (var service in _services)
  963. {
  964. cancellationToken.ThrowIfCancellationRequested();
  965. _logger.Debug("Refreshing guide from {0}", service.Name);
  966. try
  967. {
  968. var innerProgress = new ActionableProgress<double>();
  969. innerProgress.RegisterAction(p => progress.Report(p * progressPerService));
  970. var idList = await RefreshChannelsInternal(service, innerProgress, cancellationToken).ConfigureAwait(false);
  971. newChannelIdList.AddRange(idList.Item1);
  972. newProgramIdList.AddRange(idList.Item2);
  973. }
  974. catch (OperationCanceledException)
  975. {
  976. throw;
  977. }
  978. catch (Exception ex)
  979. {
  980. _logger.ErrorException("Error refreshing channels for service", ex);
  981. }
  982. numComplete++;
  983. double percent = numComplete;
  984. percent /= _services.Count;
  985. progress.Report(100 * percent);
  986. }
  987. await CleanDatabaseInternal(newChannelIdList, new[] { typeof(LiveTvChannel).Name }, progress, cancellationToken).ConfigureAwait(false);
  988. await CleanDatabaseInternal(newProgramIdList, new[] { typeof(LiveTvProgram).Name }, progress, cancellationToken).ConfigureAwait(false);
  989. var coreService = _services.OfType<EmbyTV.EmbyTV>().FirstOrDefault();
  990. if (coreService != null)
  991. {
  992. await coreService.RefreshSeriesTimers(cancellationToken, new Progress<double>()).ConfigureAwait(false);
  993. }
  994. // Load these now which will prefetch metadata
  995. var dtoOptions = new DtoOptions();
  996. dtoOptions.Fields.Remove(ItemFields.SyncInfo);
  997. dtoOptions.Fields.Remove(ItemFields.BasicSyncInfo);
  998. await GetRecordings(new RecordingQuery(), dtoOptions, cancellationToken).ConfigureAwait(false);
  999. progress.Report(100);
  1000. }
  1001. private async Task<Tuple<List<Guid>, List<Guid>>> RefreshChannelsInternal(ILiveTvService service, IProgress<double> progress, CancellationToken cancellationToken)
  1002. {
  1003. progress.Report(10);
  1004. var allChannels = await GetChannels(service, cancellationToken).ConfigureAwait(false);
  1005. var allChannelsList = allChannels.ToList();
  1006. var list = new List<LiveTvChannel>();
  1007. var numComplete = 0;
  1008. var parentFolder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false);
  1009. var parentFolderId = parentFolder.Id;
  1010. foreach (var channelInfo in allChannelsList)
  1011. {
  1012. cancellationToken.ThrowIfCancellationRequested();
  1013. try
  1014. {
  1015. var item = await GetChannel(channelInfo.Item2, channelInfo.Item1, parentFolderId, cancellationToken).ConfigureAwait(false);
  1016. list.Add(item);
  1017. }
  1018. catch (OperationCanceledException)
  1019. {
  1020. throw;
  1021. }
  1022. catch (Exception ex)
  1023. {
  1024. _logger.ErrorException("Error getting channel information for {0}", ex, channelInfo.Item2.Name);
  1025. }
  1026. numComplete++;
  1027. double percent = numComplete;
  1028. percent /= allChannelsList.Count;
  1029. progress.Report(5 * percent + 10);
  1030. }
  1031. progress.Report(15);
  1032. numComplete = 0;
  1033. var programs = new List<Guid>();
  1034. var channels = new List<Guid>();
  1035. var guideDays = GetGuideDays();
  1036. _logger.Info("Refreshing guide with {0} days of guide data", guideDays);
  1037. cancellationToken.ThrowIfCancellationRequested();
  1038. foreach (var currentChannel in list)
  1039. {
  1040. channels.Add(currentChannel.Id);
  1041. cancellationToken.ThrowIfCancellationRequested();
  1042. try
  1043. {
  1044. var start = DateTime.UtcNow.AddHours(-1);
  1045. var end = start.AddDays(guideDays);
  1046. var isMovie = false;
  1047. var isSports = false;
  1048. var isNews = false;
  1049. var isKids = false;
  1050. var iSSeries = false;
  1051. var channelPrograms = await service.GetProgramsAsync(currentChannel.ExternalId, start, end, cancellationToken).ConfigureAwait(false);
  1052. foreach (var program in channelPrograms)
  1053. {
  1054. var programItem = await GetProgram(program, currentChannel, currentChannel.ChannelType, service.Name, cancellationToken).ConfigureAwait(false);
  1055. programs.Add(programItem.Id);
  1056. if (program.IsMovie)
  1057. {
  1058. isMovie = true;
  1059. }
  1060. if (program.IsSeries)
  1061. {
  1062. iSSeries = true;
  1063. }
  1064. if (program.IsSports)
  1065. {
  1066. isSports = true;
  1067. }
  1068. if (program.IsNews)
  1069. {
  1070. isNews = true;
  1071. }
  1072. if (program.IsKids)
  1073. {
  1074. isKids = true;
  1075. }
  1076. }
  1077. currentChannel.IsMovie = isMovie;
  1078. currentChannel.IsNews = isNews;
  1079. currentChannel.IsSports = isSports;
  1080. currentChannel.IsKids = isKids;
  1081. currentChannel.IsSeries = iSSeries;
  1082. await currentChannel.UpdateToRepository(ItemUpdateType.MetadataImport, cancellationToken).ConfigureAwait(false);
  1083. }
  1084. catch (OperationCanceledException)
  1085. {
  1086. throw;
  1087. }
  1088. catch (Exception ex)
  1089. {
  1090. _logger.ErrorException("Error getting programs for channel {0}", ex, currentChannel.Name);
  1091. }
  1092. numComplete++;
  1093. double percent = numComplete;
  1094. percent /= allChannelsList.Count;
  1095. progress.Report(80 * percent + 10);
  1096. }
  1097. progress.Report(100);
  1098. return new Tuple<List<Guid>, List<Guid>>(channels, programs);
  1099. }
  1100. private async Task CleanDatabaseInternal(List<Guid> currentIdList, string[] validTypes, IProgress<double> progress, CancellationToken cancellationToken)
  1101. {
  1102. var list = _itemRepo.GetItemIdsList(new InternalItemsQuery
  1103. {
  1104. IncludeItemTypes = validTypes
  1105. }).ToList();
  1106. var numComplete = 0;
  1107. foreach (var itemId in list)
  1108. {
  1109. cancellationToken.ThrowIfCancellationRequested();
  1110. if (itemId == Guid.Empty)
  1111. {
  1112. // Somehow some invalid data got into the db. It probably predates the boundary checking
  1113. continue;
  1114. }
  1115. if (!currentIdList.Contains(itemId))
  1116. {
  1117. var item = _libraryManager.GetItemById(itemId);
  1118. if (item != null)
  1119. {
  1120. await _libraryManager.DeleteItem(item, new DeleteOptions
  1121. {
  1122. DeleteFileLocation = false
  1123. }).ConfigureAwait(false);
  1124. }
  1125. }
  1126. numComplete++;
  1127. double percent = numComplete;
  1128. percent /= list.Count;
  1129. progress.Report(100 * percent);
  1130. }
  1131. }
  1132. private const int MaxGuideDays = 14;
  1133. private double GetGuideDays()
  1134. {
  1135. var config = GetConfiguration();
  1136. if (config.GuideDays.HasValue)
  1137. {
  1138. return Math.Max(1, Math.Min(config.GuideDays.Value, MaxGuideDays));
  1139. }
  1140. return 7;
  1141. }
  1142. private async Task<IEnumerable<Tuple<string, ChannelInfo>>> GetChannels(ILiveTvService service, CancellationToken cancellationToken)
  1143. {
  1144. var channels = await service.GetChannelsAsync(cancellationToken).ConfigureAwait(false);
  1145. return channels.Select(i => new Tuple<string, ChannelInfo>(service.Name, i));
  1146. }
  1147. private DateTime _lastRecordingRefreshTime;
  1148. private async Task RefreshRecordings(CancellationToken cancellationToken)
  1149. {
  1150. const int cacheMinutes = 5;
  1151. if ((DateTime.UtcNow - _lastRecordingRefreshTime).TotalMinutes < cacheMinutes)
  1152. {
  1153. return;
  1154. }
  1155. await _refreshRecordingsLock.WaitAsync(cancellationToken).ConfigureAwait(false);
  1156. try
  1157. {
  1158. if ((DateTime.UtcNow - _lastRecordingRefreshTime).TotalMinutes < cacheMinutes)
  1159. {
  1160. return;
  1161. }
  1162. var tasks = _services.Select(async i =>
  1163. {
  1164. try
  1165. {
  1166. var recs = await i.GetRecordingsAsync(cancellationToken).ConfigureAwait(false);
  1167. return recs.Select(r => new Tuple<RecordingInfo, ILiveTvService>(r, i));
  1168. }
  1169. catch (Exception ex)
  1170. {
  1171. _logger.ErrorException("Error getting recordings", ex);
  1172. return new List<Tuple<RecordingInfo, ILiveTvService>>();
  1173. }
  1174. });
  1175. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  1176. var folder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false);
  1177. var parentFolderId = folder.Id;
  1178. var recordingTasks = results.SelectMany(i => i.ToList()).Select(i => CreateRecordingRecord(i.Item1, i.Item2.Name, parentFolderId, cancellationToken));
  1179. var idList = await Task.WhenAll(recordingTasks).ConfigureAwait(false);
  1180. await CleanDatabaseInternal(idList.ToList(), new[] { typeof(LiveTvVideoRecording).Name, typeof(LiveTvAudioRecording).Name }, new Progress<double>(), cancellationToken).ConfigureAwait(false);
  1181. _lastRecordingRefreshTime = DateTime.UtcNow;
  1182. }
  1183. finally
  1184. {
  1185. _refreshRecordingsLock.Release();
  1186. }
  1187. }
  1188. private QueryResult<BaseItem> GetEmbyRecordings(RecordingQuery query, User user)
  1189. {
  1190. if (user == null || (query.IsInProgress ?? false))
  1191. {
  1192. return new QueryResult<BaseItem>();
  1193. }
  1194. var folders = EmbyTV.EmbyTV.Current.GetRecordingFolders()
  1195. .SelectMany(i => i.Locations)
  1196. .Distinct(StringComparer.OrdinalIgnoreCase)
  1197. .Select(i => _libraryManager.FindByPath(i, true))
  1198. .Where(i => i != null)
  1199. .Where(i => i.IsVisibleStandalone(user))
  1200. .ToList();
  1201. if (folders.Count == 0)
  1202. {
  1203. return new QueryResult<BaseItem>();
  1204. }
  1205. var includeItemTypes = new List<string>();
  1206. var excludeItemTypes = new List<string>();
  1207. var genres = new List<string>();
  1208. if (query.IsMovie.HasValue)
  1209. {
  1210. if (query.IsMovie.Value)
  1211. {
  1212. includeItemTypes.Add(typeof(Movie).Name);
  1213. }
  1214. else
  1215. {
  1216. excludeItemTypes.Add(typeof(Movie).Name);
  1217. }
  1218. }
  1219. if (query.IsSeries.HasValue)
  1220. {
  1221. if (query.IsSeries.Value)
  1222. {
  1223. includeItemTypes.Add(typeof(Episode).Name);
  1224. }
  1225. else
  1226. {
  1227. excludeItemTypes.Add(typeof(Episode).Name);
  1228. }
  1229. }
  1230. if (query.IsSports.HasValue)
  1231. {
  1232. if (query.IsSports.Value)
  1233. {
  1234. genres.Add("Sports");
  1235. }
  1236. }
  1237. if (query.IsKids.HasValue)
  1238. {
  1239. if (query.IsKids.Value)
  1240. {
  1241. genres.Add("Kids");
  1242. genres.Add("Children");
  1243. genres.Add("Family");
  1244. }
  1245. }
  1246. return _libraryManager.GetItemsResult(new InternalItemsQuery(user)
  1247. {
  1248. MediaTypes = new[] { MediaType.Video },
  1249. Recursive = true,
  1250. AncestorIds = folders.Select(i => i.Id.ToString("N")).ToArray(),
  1251. IsFolder = false,
  1252. ExcludeLocationTypes = new[] { LocationType.Virtual },
  1253. Limit = query.Limit,
  1254. SortBy = new[] { ItemSortBy.DateCreated },
  1255. SortOrder = SortOrder.Descending,
  1256. EnableTotalRecordCount = query.EnableTotalRecordCount,
  1257. IncludeItemTypes = includeItemTypes.ToArray(),
  1258. ExcludeItemTypes = excludeItemTypes.ToArray(),
  1259. Genres = genres.ToArray()
  1260. });
  1261. }
  1262. public async Task<QueryResult<BaseItemDto>> GetRecordingSeries(RecordingQuery query, DtoOptions options, CancellationToken cancellationToken)
  1263. {
  1264. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId);
  1265. if (user != null && !IsLiveTvEnabled(user))
  1266. {
  1267. return new QueryResult<BaseItemDto>();
  1268. }
  1269. if (_services.Count > 1)
  1270. {
  1271. return new QueryResult<BaseItemDto>();
  1272. }
  1273. if (user == null || (query.IsInProgress ?? false))
  1274. {
  1275. return new QueryResult<BaseItemDto>();
  1276. }
  1277. var folders = EmbyTV.EmbyTV.Current.GetRecordingFolders()
  1278. .SelectMany(i => i.Locations)
  1279. .Distinct(StringComparer.OrdinalIgnoreCase)
  1280. .Select(i => _libraryManager.FindByPath(i, true))
  1281. .Where(i => i != null)
  1282. .Where(i => i.IsVisibleStandalone(user))
  1283. .ToList();
  1284. if (folders.Count == 0)
  1285. {
  1286. return new QueryResult<BaseItemDto>();
  1287. }
  1288. var includeItemTypes = new List<string>();
  1289. var excludeItemTypes = new List<string>();
  1290. includeItemTypes.Add(typeof(Series).Name);
  1291. var internalResult = _libraryManager.GetItemsResult(new InternalItemsQuery(user)
  1292. {
  1293. Recursive = true,
  1294. AncestorIds = folders.Select(i => i.Id.ToString("N")).ToArray(),
  1295. Limit = query.Limit,
  1296. SortBy = new[] { ItemSortBy.DateCreated },
  1297. SortOrder = SortOrder.Descending,
  1298. EnableTotalRecordCount = query.EnableTotalRecordCount,
  1299. IncludeItemTypes = includeItemTypes.ToArray(),
  1300. ExcludeItemTypes = excludeItemTypes.ToArray()
  1301. });
  1302. RemoveFields(options);
  1303. var returnArray = (await _dtoService.GetBaseItemDtos(internalResult.Items, options, user).ConfigureAwait(false)).ToArray();
  1304. return new QueryResult<BaseItemDto>
  1305. {
  1306. Items = returnArray,
  1307. TotalRecordCount = internalResult.TotalRecordCount
  1308. };
  1309. }
  1310. public async Task<QueryResult<BaseItem>> GetInternalRecordings(RecordingQuery query, CancellationToken cancellationToken)
  1311. {
  1312. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId);
  1313. if (user != null && !IsLiveTvEnabled(user))
  1314. {
  1315. return new QueryResult<BaseItem>();
  1316. }
  1317. if (_services.Count == 1)
  1318. {
  1319. return GetEmbyRecordings(query, user);
  1320. }
  1321. await RefreshRecordings(cancellationToken).ConfigureAwait(false);
  1322. var internalQuery = new InternalItemsQuery(user)
  1323. {
  1324. IncludeItemTypes = new[] { typeof(LiveTvVideoRecording).Name, typeof(LiveTvAudioRecording).Name }
  1325. };
  1326. if (!string.IsNullOrEmpty(query.ChannelId))
  1327. {
  1328. internalQuery.ChannelIds = new[] { query.ChannelId };
  1329. }
  1330. var queryResult = _libraryManager.GetItemList(internalQuery);
  1331. IEnumerable<ILiveTvRecording> recordings = queryResult.Cast<ILiveTvRecording>();
  1332. if (!string.IsNullOrWhiteSpace(query.Id))
  1333. {
  1334. var guid = new Guid(query.Id);
  1335. recordings = recordings
  1336. .Where(i => i.Id == guid);
  1337. }
  1338. if (!string.IsNullOrWhiteSpace(query.GroupId))
  1339. {
  1340. var guid = new Guid(query.GroupId);
  1341. recordings = recordings.Where(i => GetRecordingGroupIds(i).Contains(guid));
  1342. }
  1343. if (query.IsInProgress.HasValue)
  1344. {
  1345. var val = query.IsInProgress.Value;
  1346. recordings = recordings.Where(i => i.Status == RecordingStatus.InProgress == val);
  1347. }
  1348. if (query.Status.HasValue)
  1349. {
  1350. var val = query.Status.Value;
  1351. recordings = recordings.Where(i => i.Status == val);
  1352. }
  1353. if (query.IsMovie.HasValue)
  1354. {
  1355. var val = query.IsMovie.Value;
  1356. recordings = recordings.Where(i => i.IsMovie == val);
  1357. }
  1358. if (query.IsNews.HasValue)
  1359. {
  1360. var val = query.IsNews.Value;
  1361. recordings = recordings.Where(i => i.IsNews == val);
  1362. }
  1363. if (query.IsSeries.HasValue)
  1364. {
  1365. var val = query.IsSeries.Value;
  1366. recordings = recordings.Where(i => i.IsSeries == val);
  1367. }
  1368. if (query.IsKids.HasValue)
  1369. {
  1370. var val = query.IsKids.Value;
  1371. recordings = recordings.Where(i => i.IsKids == val);
  1372. }
  1373. if (query.IsSports.HasValue)
  1374. {
  1375. var val = query.IsSports.Value;
  1376. recordings = recordings.Where(i => i.IsSports == val);
  1377. }
  1378. if (!string.IsNullOrEmpty(query.SeriesTimerId))
  1379. {
  1380. var guid = new Guid(query.SeriesTimerId);
  1381. recordings = recordings
  1382. .Where(i => _tvDtoService.GetInternalSeriesTimerId(i.ServiceName, i.SeriesTimerId) == guid);
  1383. }
  1384. recordings = recordings.OrderByDescending(i => i.StartDate);
  1385. var entityList = recordings.ToList();
  1386. IEnumerable<ILiveTvRecording> entities = entityList;
  1387. if (query.StartIndex.HasValue)
  1388. {
  1389. entities = entities.Skip(query.StartIndex.Value);
  1390. }
  1391. if (query.Limit.HasValue)
  1392. {
  1393. entities = entities.Take(query.Limit.Value);
  1394. }
  1395. return new QueryResult<BaseItem>
  1396. {
  1397. Items = entities.Cast<BaseItem>().ToArray(),
  1398. TotalRecordCount = entityList.Count
  1399. };
  1400. }
  1401. public async Task AddInfoToProgramDto(List<Tuple<BaseItem, BaseItemDto>> tuples, List<ItemFields> fields, User user = null)
  1402. {
  1403. var recordingTuples = new List<Tuple<BaseItemDto, string, string, string>>();
  1404. foreach (var tuple in tuples)
  1405. {
  1406. var program = (LiveTvProgram)tuple.Item1;
  1407. var dto = tuple.Item2;
  1408. dto.StartDate = program.StartDate;
  1409. dto.EpisodeTitle = program.EpisodeTitle;
  1410. if (program.IsRepeat)
  1411. {
  1412. dto.IsRepeat = program.IsRepeat;
  1413. }
  1414. if (program.IsMovie)
  1415. {
  1416. dto.IsMovie = program.IsMovie;
  1417. }
  1418. if (program.IsSeries)
  1419. {
  1420. dto.IsSeries = program.IsSeries;
  1421. }
  1422. if (program.IsSports)
  1423. {
  1424. dto.IsSports = program.IsSports;
  1425. }
  1426. if (program.IsLive)
  1427. {
  1428. dto.IsLive = program.IsLive;
  1429. }
  1430. if (program.IsNews)
  1431. {
  1432. dto.IsNews = program.IsNews;
  1433. }
  1434. if (program.IsKids)
  1435. {
  1436. dto.IsKids = program.IsKids;
  1437. }
  1438. if (program.IsPremiere)
  1439. {
  1440. dto.IsPremiere = program.IsPremiere;
  1441. }
  1442. if (fields.Contains(ItemFields.ChannelInfo))
  1443. {
  1444. var channel = GetInternalChannel(program.ChannelId);
  1445. if (channel != null)
  1446. {
  1447. dto.ChannelName = channel.Name;
  1448. dto.MediaType = channel.MediaType;
  1449. dto.ChannelNumber = channel.Number;
  1450. if (channel.HasImage(ImageType.Primary))
  1451. {
  1452. dto.ChannelPrimaryImageTag = _tvDtoService.GetImageTag(channel);
  1453. }
  1454. }
  1455. }
  1456. var service = GetService(program);
  1457. var serviceName = service == null ? null : service.Name;
  1458. if (fields.Contains(ItemFields.ServiceName))
  1459. {
  1460. dto.ServiceName = serviceName;
  1461. }
  1462. recordingTuples.Add(new Tuple<BaseItemDto, string, string, string>(dto, serviceName, program.ExternalId, program.ExternalSeriesIdLegacy));
  1463. }
  1464. await AddRecordingInfo(recordingTuples, CancellationToken.None).ConfigureAwait(false);
  1465. }
  1466. public void AddInfoToRecordingDto(BaseItem item, BaseItemDto dto, User user = null)
  1467. {
  1468. var recording = (ILiveTvRecording)item;
  1469. var service = GetService(recording);
  1470. var channel = string.IsNullOrWhiteSpace(recording.ChannelId) ? null : GetInternalChannel(recording.ChannelId);
  1471. var info = recording;
  1472. dto.SeriesTimerId = string.IsNullOrEmpty(info.SeriesTimerId)
  1473. ? null
  1474. : _tvDtoService.GetInternalSeriesTimerId(service.Name, info.SeriesTimerId).ToString("N");
  1475. dto.StartDate = info.StartDate;
  1476. dto.RecordingStatus = info.Status;
  1477. dto.IsRepeat = info.IsRepeat;
  1478. dto.EpisodeTitle = info.EpisodeTitle;
  1479. dto.IsMovie = info.IsMovie;
  1480. dto.IsSeries = info.IsSeries;
  1481. dto.IsSports = info.IsSports;
  1482. dto.IsLive = info.IsLive;
  1483. dto.IsNews = info.IsNews;
  1484. dto.IsKids = info.IsKids;
  1485. dto.IsPremiere = info.IsPremiere;
  1486. dto.CanDelete = user == null
  1487. ? recording.CanDelete()
  1488. : recording.CanDelete(user);
  1489. if (dto.MediaSources == null)
  1490. {
  1491. dto.MediaSources = recording.GetMediaSources(true).ToList();
  1492. }
  1493. if (dto.MediaStreams == null)
  1494. {
  1495. dto.MediaStreams = dto.MediaSources.SelectMany(i => i.MediaStreams).ToList();
  1496. }
  1497. if (info.Status == RecordingStatus.InProgress && info.EndDate.HasValue)
  1498. {
  1499. var now = DateTime.UtcNow.Ticks;
  1500. var start = info.StartDate.Ticks;
  1501. var end = info.EndDate.Value.Ticks;
  1502. var pct = now - start;
  1503. pct /= end;
  1504. pct *= 100;
  1505. dto.CompletionPercentage = pct;
  1506. }
  1507. if (channel != null)
  1508. {
  1509. dto.ChannelName = channel.Name;
  1510. if (channel.HasImage(ImageType.Primary))
  1511. {
  1512. dto.ChannelPrimaryImageTag = _tvDtoService.GetImageTag(channel);
  1513. }
  1514. }
  1515. }
  1516. public async Task<QueryResult<BaseItemDto>> GetRecordings(RecordingQuery query, DtoOptions options, CancellationToken cancellationToken)
  1517. {
  1518. var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId);
  1519. var internalResult = await GetInternalRecordings(query, cancellationToken).ConfigureAwait(false);
  1520. RemoveFields(options);
  1521. var returnArray = (await _dtoService.GetBaseItemDtos(internalResult.Items, options, user).ConfigureAwait(false)).ToArray();
  1522. return new QueryResult<BaseItemDto>
  1523. {
  1524. Items = returnArray,
  1525. TotalRecordCount = internalResult.TotalRecordCount
  1526. };
  1527. }
  1528. public async Task<QueryResult<TimerInfoDto>> GetTimers(TimerQuery query, CancellationToken cancellationToken)
  1529. {
  1530. var tasks = _services.Select(async i =>
  1531. {
  1532. try
  1533. {
  1534. var recs = await i.GetTimersAsync(cancellationToken).ConfigureAwait(false);
  1535. return recs.Select(r => new Tuple<TimerInfo, ILiveTvService>(r, i));
  1536. }
  1537. catch (Exception ex)
  1538. {
  1539. _logger.ErrorException("Error getting recordings", ex);
  1540. return new List<Tuple<TimerInfo, ILiveTvService>>();
  1541. }
  1542. });
  1543. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  1544. var timers = results.SelectMany(i => i.ToList());
  1545. if (query.IsActive.HasValue)
  1546. {
  1547. if (query.IsActive.Value)
  1548. {
  1549. timers = timers.Where(i => i.Item1.Status == RecordingStatus.InProgress);
  1550. }
  1551. else
  1552. {
  1553. timers = timers.Where(i => i.Item1.Status != RecordingStatus.InProgress);
  1554. }
  1555. }
  1556. if (!string.IsNullOrEmpty(query.ChannelId))
  1557. {
  1558. var guid = new Guid(query.ChannelId);
  1559. timers = timers.Where(i => guid == _tvDtoService.GetInternalChannelId(i.Item2.Name, i.Item1.ChannelId));
  1560. }
  1561. if (!string.IsNullOrEmpty(query.SeriesTimerId))
  1562. {
  1563. var guid = new Guid(query.SeriesTimerId);
  1564. timers = timers
  1565. .Where(i => _tvDtoService.GetInternalSeriesTimerId(i.Item2.Name, i.Item1.SeriesTimerId) == guid);
  1566. }
  1567. var returnList = new List<TimerInfoDto>();
  1568. foreach (var i in timers)
  1569. {
  1570. var program = string.IsNullOrEmpty(i.Item1.ProgramId) ?
  1571. null :
  1572. GetInternalProgram(_tvDtoService.GetInternalProgramId(i.Item2.Name, i.Item1.ProgramId).ToString("N"));
  1573. var channel = string.IsNullOrEmpty(i.Item1.ChannelId) ? null : GetInternalChannel(_tvDtoService.GetInternalChannelId(i.Item2.Name, i.Item1.ChannelId));
  1574. returnList.Add(_tvDtoService.GetTimerInfoDto(i.Item1, i.Item2, program, channel));
  1575. }
  1576. var returnArray = returnList
  1577. .OrderBy(i => i.StartDate)
  1578. .ToArray();
  1579. return new QueryResult<TimerInfoDto>
  1580. {
  1581. Items = returnArray,
  1582. TotalRecordCount = returnArray.Length
  1583. };
  1584. }
  1585. public Task OnRecordingFileDeleted(BaseItem recording)
  1586. {
  1587. var service = GetService(recording);
  1588. if (service is EmbyTV.EmbyTV)
  1589. {
  1590. // We can't trust that we'll be able to direct stream it through emby server, no matter what the provider says
  1591. return service.DeleteRecordingAsync(recording.ExternalId, CancellationToken.None);
  1592. }
  1593. return Task.FromResult(true);
  1594. }
  1595. public async Task DeleteRecording(string recordingId)
  1596. {
  1597. var recording = await GetInternalRecording(recordingId, CancellationToken.None).ConfigureAwait(false);
  1598. if (recording == null)
  1599. {
  1600. throw new ResourceNotFoundException(string.Format("Recording with Id {0} not found", recordingId));
  1601. }
  1602. await DeleteRecording((BaseItem)recording).ConfigureAwait(false);
  1603. }
  1604. public async Task DeleteRecording(BaseItem recording)
  1605. {
  1606. var service = GetService(recording.ServiceName);
  1607. try
  1608. {
  1609. await service.DeleteRecordingAsync(recording.ExternalId, CancellationToken.None).ConfigureAwait(false);
  1610. }
  1611. catch (ResourceNotFoundException)
  1612. {
  1613. }
  1614. _lastRecordingRefreshTime = DateTime.MinValue;
  1615. // This is the responsibility of the live tv service
  1616. await _libraryManager.DeleteItem((BaseItem)recording, new DeleteOptions
  1617. {
  1618. DeleteFileLocation = false
  1619. }).ConfigureAwait(false);
  1620. _lastRecordingRefreshTime = DateTime.MinValue;
  1621. }
  1622. public async Task CancelTimer(string id)
  1623. {
  1624. var timer = await GetTimer(id, CancellationToken.None).ConfigureAwait(false);
  1625. if (timer == null)
  1626. {
  1627. throw new ResourceNotFoundException(string.Format("Timer with Id {0} not found", id));
  1628. }
  1629. var service = GetService(timer.ServiceName);
  1630. await service.CancelTimerAsync(timer.ExternalId, CancellationToken.None).ConfigureAwait(false);
  1631. _lastRecordingRefreshTime = DateTime.MinValue;
  1632. EventHelper.QueueEventIfNotNull(TimerCancelled, this, new GenericEventArgs<TimerEventInfo>
  1633. {
  1634. Argument = new TimerEventInfo
  1635. {
  1636. Id = id
  1637. }
  1638. }, _logger);
  1639. }
  1640. public async Task CancelSeriesTimer(string id)
  1641. {
  1642. var timer = await GetSeriesTimer(id, CancellationToken.None).ConfigureAwait(false);
  1643. if (timer == null)
  1644. {
  1645. throw new ResourceNotFoundException(string.Format("Timer with Id {0} not found", id));
  1646. }
  1647. var service = GetService(timer.ServiceName);
  1648. await service.CancelSeriesTimerAsync(timer.ExternalId, CancellationToken.None).ConfigureAwait(false);
  1649. _lastRecordingRefreshTime = DateTime.MinValue;
  1650. EventHelper.QueueEventIfNotNull(SeriesTimerCancelled, this, new GenericEventArgs<TimerEventInfo>
  1651. {
  1652. Argument = new TimerEventInfo
  1653. {
  1654. Id = id
  1655. }
  1656. }, _logger);
  1657. }
  1658. public async Task<BaseItemDto> GetRecording(string id, DtoOptions options, CancellationToken cancellationToken, User user = null)
  1659. {
  1660. var item = await GetInternalRecording(id, cancellationToken).ConfigureAwait(false);
  1661. if (item == null)
  1662. {
  1663. return null;
  1664. }
  1665. return _dtoService.GetBaseItemDto((BaseItem)item, options, user);
  1666. }
  1667. public async Task<TimerInfoDto> GetTimer(string id, CancellationToken cancellationToken)
  1668. {
  1669. var results = await GetTimers(new TimerQuery(), cancellationToken).ConfigureAwait(false);
  1670. return results.Items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
  1671. }
  1672. public async Task<SeriesTimerInfoDto> GetSeriesTimer(string id, CancellationToken cancellationToken)
  1673. {
  1674. var results = await GetSeriesTimers(new SeriesTimerQuery(), cancellationToken).ConfigureAwait(false);
  1675. return results.Items.FirstOrDefault(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
  1676. }
  1677. private async Task<QueryResult<SeriesTimerInfo>> GetSeriesTimersInternal(SeriesTimerQuery query, CancellationToken cancellationToken)
  1678. {
  1679. var tasks = _services.Select(async i =>
  1680. {
  1681. try
  1682. {
  1683. var recs = await i.GetSeriesTimersAsync(cancellationToken).ConfigureAwait(false);
  1684. return recs.Select(r =>
  1685. {
  1686. r.ServiceName = i.Name;
  1687. return new Tuple<SeriesTimerInfo, ILiveTvService>(r, i);
  1688. });
  1689. }
  1690. catch (Exception ex)
  1691. {
  1692. _logger.ErrorException("Error getting recordings", ex);
  1693. return new List<Tuple<SeriesTimerInfo, ILiveTvService>>();
  1694. }
  1695. });
  1696. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  1697. var timers = results.SelectMany(i => i.ToList());
  1698. if (string.Equals(query.SortBy, "Priority", StringComparison.OrdinalIgnoreCase))
  1699. {
  1700. timers = query.SortOrder == SortOrder.Descending ?
  1701. timers.OrderBy(i => i.Item1.Priority).ThenByStringDescending(i => i.Item1.Name) :
  1702. timers.OrderByDescending(i => i.Item1.Priority).ThenByString(i => i.Item1.Name);
  1703. }
  1704. else
  1705. {
  1706. timers = query.SortOrder == SortOrder.Descending ?
  1707. timers.OrderByStringDescending(i => i.Item1.Name) :
  1708. timers.OrderByString(i => i.Item1.Name);
  1709. }
  1710. var returnArray = timers
  1711. .Select(i =>
  1712. {
  1713. return i.Item1;
  1714. })
  1715. .ToArray();
  1716. return new QueryResult<SeriesTimerInfo>
  1717. {
  1718. Items = returnArray,
  1719. TotalRecordCount = returnArray.Length
  1720. };
  1721. }
  1722. public async Task<QueryResult<SeriesTimerInfoDto>> GetSeriesTimers(SeriesTimerQuery query, CancellationToken cancellationToken)
  1723. {
  1724. var tasks = _services.Select(async i =>
  1725. {
  1726. try
  1727. {
  1728. var recs = await i.GetSeriesTimersAsync(cancellationToken).ConfigureAwait(false);
  1729. return recs.Select(r => new Tuple<SeriesTimerInfo, ILiveTvService>(r, i));
  1730. }
  1731. catch (Exception ex)
  1732. {
  1733. _logger.ErrorException("Error getting recordings", ex);
  1734. return new List<Tuple<SeriesTimerInfo, ILiveTvService>>();
  1735. }
  1736. });
  1737. var results = await Task.WhenAll(tasks).ConfigureAwait(false);
  1738. var timers = results.SelectMany(i => i.ToList());
  1739. if (string.Equals(query.SortBy, "Priority", StringComparison.OrdinalIgnoreCase))
  1740. {
  1741. timers = query.SortOrder == SortOrder.Descending ?
  1742. timers.OrderBy(i => i.Item1.Priority).ThenByStringDescending(i => i.Item1.Name) :
  1743. timers.OrderByDescending(i => i.Item1.Priority).ThenByString(i => i.Item1.Name);
  1744. }
  1745. else
  1746. {
  1747. timers = query.SortOrder == SortOrder.Descending ?
  1748. timers.OrderByStringDescending(i => i.Item1.Name) :
  1749. timers.OrderByString(i => i.Item1.Name);
  1750. }
  1751. var returnArray = timers
  1752. .Select(i =>
  1753. {
  1754. string channelName = null;
  1755. if (!string.IsNullOrEmpty(i.Item1.ChannelId))
  1756. {
  1757. var internalChannelId = _tvDtoService.GetInternalChannelId(i.Item2.Name, i.Item1.ChannelId);
  1758. var channel = GetInternalChannel(internalChannelId);
  1759. channelName = channel == null ? null : channel.Name;
  1760. }
  1761. return _tvDtoService.GetSeriesTimerInfoDto(i.Item1, i.Item2, channelName);
  1762. })
  1763. .ToArray();
  1764. return new QueryResult<SeriesTimerInfoDto>
  1765. {
  1766. Items = returnArray,
  1767. TotalRecordCount = returnArray.Length
  1768. };
  1769. }
  1770. public void AddChannelInfo(List<Tuple<BaseItemDto, LiveTvChannel>> tuples, DtoOptions options, User user)
  1771. {
  1772. var now = DateTime.UtcNow;
  1773. var channelIds = tuples.Select(i => i.Item2.Id.ToString("N")).Distinct().ToArray();
  1774. var programs = options.AddCurrentProgram ? _libraryManager.GetItemList(new InternalItemsQuery(user)
  1775. {
  1776. IncludeItemTypes = new[] { typeof(LiveTvProgram).Name },
  1777. ChannelIds = channelIds,
  1778. MaxStartDate = now,
  1779. MinEndDate = now,
  1780. Limit = channelIds.Length,
  1781. SortBy = new[] { "StartDate" },
  1782. TopParentIds = new[] { GetInternalLiveTvFolder(CancellationToken.None).Result.Id.ToString("N") }
  1783. }).ToList() : new List<BaseItem>();
  1784. RemoveFields(options);
  1785. foreach (var tuple in tuples)
  1786. {
  1787. var dto = tuple.Item1;
  1788. var channel = tuple.Item2;
  1789. dto.Number = channel.Number;
  1790. dto.ChannelNumber = channel.Number;
  1791. dto.ChannelType = channel.ChannelType;
  1792. dto.ServiceName = channel.ServiceName;
  1793. if (options.Fields.Contains(ItemFields.MediaSources))
  1794. {
  1795. dto.MediaSources = channel.GetMediaSources(true).ToList();
  1796. }
  1797. if (options.AddCurrentProgram)
  1798. {
  1799. var channelIdString = channel.Id.ToString("N");
  1800. var currentProgram = programs.FirstOrDefault(i => string.Equals(i.ChannelId, channelIdString));
  1801. if (currentProgram != null)
  1802. {
  1803. dto.CurrentProgram = _dtoService.GetBaseItemDto(currentProgram, options, user);
  1804. }
  1805. }
  1806. }
  1807. }
  1808. private async Task<Tuple<SeriesTimerInfo, ILiveTvService>> GetNewTimerDefaultsInternal(CancellationToken cancellationToken, LiveTvProgram program = null)
  1809. {
  1810. var service = program != null && !string.IsNullOrWhiteSpace(program.ServiceName) ?
  1811. GetService(program) :
  1812. _services.FirstOrDefault();
  1813. ProgramInfo programInfo = null;
  1814. if (program != null)
  1815. {
  1816. var channel = GetInternalChannel(program.ChannelId);
  1817. programInfo = new ProgramInfo
  1818. {
  1819. Audio = program.Audio,
  1820. ChannelId = channel.ExternalId,
  1821. CommunityRating = program.CommunityRating,
  1822. EndDate = program.EndDate ?? DateTime.MinValue,
  1823. EpisodeTitle = program.EpisodeTitle,
  1824. Genres = program.Genres,
  1825. Id = program.ExternalId,
  1826. IsHD = program.IsHD,
  1827. IsKids = program.IsKids,
  1828. IsLive = program.IsLive,
  1829. IsMovie = program.IsMovie,
  1830. IsNews = program.IsNews,
  1831. IsPremiere = program.IsPremiere,
  1832. IsRepeat = program.IsRepeat,
  1833. IsSeries = program.IsSeries,
  1834. IsSports = program.IsSports,
  1835. OriginalAirDate = program.PremiereDate,
  1836. Overview = program.Overview,
  1837. StartDate = program.StartDate,
  1838. //ImagePath = program.ExternalImagePath,
  1839. Name = program.Name,
  1840. OfficialRating = program.OfficialRating
  1841. };
  1842. }
  1843. var info = await service.GetNewTimerDefaultsAsync(cancellationToken, programInfo).ConfigureAwait(false);
  1844. info.Id = null;
  1845. return new Tuple<SeriesTimerInfo, ILiveTvService>(info, service);
  1846. }
  1847. public async Task<SeriesTimerInfoDto> GetNewTimerDefaults(CancellationToken cancellationToken)
  1848. {
  1849. var info = await GetNewTimerDefaultsInternal(cancellationToken).ConfigureAwait(false);
  1850. var obj = _tvDtoService.GetSeriesTimerInfoDto(info.Item1, info.Item2, null);
  1851. return obj;
  1852. }
  1853. public async Task<SeriesTimerInfoDto> GetNewTimerDefaults(string programId, CancellationToken cancellationToken)
  1854. {
  1855. var program = GetInternalProgram(programId);
  1856. var programDto = await GetProgram(programId, cancellationToken).ConfigureAwait(false);
  1857. var defaults = await GetNewTimerDefaultsInternal(cancellationToken, program).ConfigureAwait(false);
  1858. var info = _tvDtoService.GetSeriesTimerInfoDto(defaults.Item1, defaults.Item2, null);
  1859. info.Days = defaults.Item1.Days;
  1860. info.DayPattern = _tvDtoService.GetDayPattern(info.Days);
  1861. info.Name = program.Name;
  1862. info.ChannelId = programDto.ChannelId;
  1863. info.ChannelName = programDto.ChannelName;
  1864. info.StartDate = program.StartDate;
  1865. info.Name = program.Name;
  1866. info.Overview = program.Overview;
  1867. info.ProgramId = programDto.Id;
  1868. info.ExternalProgramId = program.ExternalId;
  1869. if (program.EndDate.HasValue)
  1870. {
  1871. info.EndDate = program.EndDate.Value;
  1872. }
  1873. return info;
  1874. }
  1875. public async Task CreateTimer(TimerInfoDto timer, CancellationToken cancellationToken)
  1876. {
  1877. var service = GetService(timer.ServiceName);
  1878. var info = await _tvDtoService.GetTimerInfo(timer, true, this, cancellationToken).ConfigureAwait(false);
  1879. // Set priority from default values
  1880. var defaultValues = await service.GetNewTimerDefaultsAsync(cancellationToken).ConfigureAwait(false);
  1881. info.Priority = defaultValues.Priority;
  1882. string newTimerId = null;
  1883. var supportsNewTimerIds = service as ISupportsNewTimerIds;
  1884. if (supportsNewTimerIds != null)
  1885. {
  1886. newTimerId = await supportsNewTimerIds.CreateTimer(info, cancellationToken).ConfigureAwait(false);
  1887. newTimerId = _tvDtoService.GetInternalTimerId(timer.ServiceName, newTimerId).ToString("N");
  1888. }
  1889. else
  1890. {
  1891. await service.CreateTimerAsync(info, cancellationToken).ConfigureAwait(false);
  1892. }
  1893. _lastRecordingRefreshTime = DateTime.MinValue;
  1894. _logger.Info("New recording scheduled");
  1895. EventHelper.QueueEventIfNotNull(TimerCreated, this, new GenericEventArgs<TimerEventInfo>
  1896. {
  1897. Argument = new TimerEventInfo
  1898. {
  1899. ProgramId = _tvDtoService.GetInternalProgramId(timer.ServiceName, info.ProgramId).ToString("N"),
  1900. Id = newTimerId
  1901. }
  1902. }, _logger);
  1903. }
  1904. public async Task CreateSeriesTimer(SeriesTimerInfoDto timer, CancellationToken cancellationToken)
  1905. {
  1906. var registration = await GetRegistrationInfo("seriesrecordings").ConfigureAwait(false);
  1907. if (!registration.IsValid)
  1908. {
  1909. _logger.Info("Creating series recordings requires an active Emby Premiere subscription.");
  1910. return;
  1911. }
  1912. var service = GetService(timer.ServiceName);
  1913. var info = await _tvDtoService.GetSeriesTimerInfo(timer, true, this, cancellationToken).ConfigureAwait(false);
  1914. // Set priority from default values
  1915. var defaultValues = await service.GetNewTimerDefaultsAsync(cancellationToken).ConfigureAwait(false);
  1916. info.Priority = defaultValues.Priority;
  1917. string newTimerId = null;
  1918. var supportsNewTimerIds = service as ISupportsNewTimerIds;
  1919. if (supportsNewTimerIds != null)
  1920. {
  1921. newTimerId = await supportsNewTimerIds.CreateSeriesTimer(info, cancellationToken).ConfigureAwait(false);
  1922. newTimerId = _tvDtoService.GetInternalSeriesTimerId(timer.ServiceName, newTimerId).ToString("N");
  1923. }
  1924. else
  1925. {
  1926. await service.CreateSeriesTimerAsync(info, cancellationToken).ConfigureAwait(false);
  1927. }
  1928. _lastRecordingRefreshTime = DateTime.MinValue;
  1929. EventHelper.QueueEventIfNotNull(SeriesTimerCreated, this, new GenericEventArgs<TimerEventInfo>
  1930. {
  1931. Argument = new TimerEventInfo
  1932. {
  1933. ProgramId = _tvDtoService.GetInternalProgramId(timer.ServiceName, info.ProgramId).ToString("N"),
  1934. Id = newTimerId
  1935. }
  1936. }, _logger);
  1937. }
  1938. public async Task UpdateTimer(TimerInfoDto timer, CancellationToken cancellationToken)
  1939. {
  1940. var info = await _tvDtoService.GetTimerInfo(timer, false, this, cancellationToken).ConfigureAwait(false);
  1941. var service = GetService(timer.ServiceName);
  1942. await service.UpdateTimerAsync(info, cancellationToken).ConfigureAwait(false);
  1943. _lastRecordingRefreshTime = DateTime.MinValue;
  1944. }
  1945. public async Task UpdateSeriesTimer(SeriesTimerInfoDto timer, CancellationToken cancellationToken)
  1946. {
  1947. var info = await _tvDtoService.GetSeriesTimerInfo(timer, false, this, cancellationToken).ConfigureAwait(false);
  1948. var service = GetService(timer.ServiceName);
  1949. await service.UpdateSeriesTimerAsync(info, cancellationToken).ConfigureAwait(false);
  1950. _lastRecordingRefreshTime = DateTime.MinValue;
  1951. }
  1952. private IEnumerable<string> GetRecordingGroupNames(ILiveTvRecording recording)
  1953. {
  1954. var list = new List<string>();
  1955. if (recording.IsSeries)
  1956. {
  1957. list.Add(recording.Name);
  1958. }
  1959. if (recording.IsKids)
  1960. {
  1961. list.Add("Kids");
  1962. }
  1963. if (recording.IsMovie)
  1964. {
  1965. list.Add("Movies");
  1966. }
  1967. if (recording.IsNews)
  1968. {
  1969. list.Add("News");
  1970. }
  1971. if (recording.IsSports)
  1972. {
  1973. list.Add("Sports");
  1974. }
  1975. if (!recording.IsSports && !recording.IsNews && !recording.IsMovie && !recording.IsKids && !recording.IsSeries)
  1976. {
  1977. list.Add("Others");
  1978. }
  1979. return list;
  1980. }
  1981. private List<Guid> GetRecordingGroupIds(ILiveTvRecording recording)
  1982. {
  1983. return GetRecordingGroupNames(recording).Select(i => i.ToLower()
  1984. .GetMD5())
  1985. .ToList();
  1986. }
  1987. public async Task<QueryResult<BaseItemDto>> GetRecordingGroups(RecordingGroupQuery query, CancellationToken cancellationToken)
  1988. {
  1989. var recordingResult = await GetInternalRecordings(new RecordingQuery
  1990. {
  1991. UserId = query.UserId
  1992. }, cancellationToken).ConfigureAwait(false);
  1993. var recordings = recordingResult.Items.OfType<ILiveTvRecording>().ToList();
  1994. var groups = new List<BaseItemDto>();
  1995. var series = recordings
  1996. .Where(i => i.IsSeries)
  1997. .ToLookup(i => i.Name, StringComparer.OrdinalIgnoreCase)
  1998. .ToList();
  1999. groups.AddRange(series.OrderByString(i => i.Key).Select(i => new BaseItemDto
  2000. {
  2001. Name = i.Key,
  2002. RecordingCount = i.Count()
  2003. }));
  2004. groups.Add(new BaseItemDto
  2005. {
  2006. Name = "Kids",
  2007. RecordingCount = recordings.Count(i => i.IsKids)
  2008. });
  2009. groups.Add(new BaseItemDto
  2010. {
  2011. Name = "Movies",
  2012. RecordingCount = recordings.Count(i => i.IsMovie)
  2013. });
  2014. groups.Add(new BaseItemDto
  2015. {
  2016. Name = "News",
  2017. RecordingCount = recordings.Count(i => i.IsNews)
  2018. });
  2019. groups.Add(new BaseItemDto
  2020. {
  2021. Name = "Sports",
  2022. RecordingCount = recordings.Count(i => i.IsSports)
  2023. });
  2024. groups.Add(new BaseItemDto
  2025. {
  2026. Name = "Others",
  2027. RecordingCount = recordings.Count(i => !i.IsSports && !i.IsNews && !i.IsMovie && !i.IsKids && !i.IsSeries)
  2028. });
  2029. groups = groups
  2030. .Where(i => i.RecordingCount > 0)
  2031. .ToList();
  2032. foreach (var group in groups)
  2033. {
  2034. group.Id = group.Name.ToLower().GetMD5().ToString("N");
  2035. }
  2036. return new QueryResult<BaseItemDto>
  2037. {
  2038. Items = groups.ToArray(),
  2039. TotalRecordCount = groups.Count
  2040. };
  2041. }
  2042. class LiveStreamData
  2043. {
  2044. internal MediaSourceInfo Info;
  2045. internal string ItemId;
  2046. internal bool IsChannel;
  2047. }
  2048. public async Task CloseLiveStream(string id)
  2049. {
  2050. await _liveStreamSemaphore.WaitAsync().ConfigureAwait(false);
  2051. try
  2052. {
  2053. var parts = id.Split(new[] { '_' }, 2);
  2054. var service = _services.FirstOrDefault(i => string.Equals(i.GetType().FullName.GetMD5().ToString("N"), parts[0], StringComparison.OrdinalIgnoreCase));
  2055. if (service == null)
  2056. {
  2057. throw new ArgumentException("Service not found.");
  2058. }
  2059. id = parts[1];
  2060. _logger.Info("Closing live stream from {0}, stream Id: {1}", service.Name, id);
  2061. await service.CloseLiveStream(id, CancellationToken.None).ConfigureAwait(false);
  2062. }
  2063. catch (Exception ex)
  2064. {
  2065. _logger.ErrorException("Error closing live stream", ex);
  2066. throw;
  2067. }
  2068. finally
  2069. {
  2070. _liveStreamSemaphore.Release();
  2071. }
  2072. }
  2073. public GuideInfo GetGuideInfo()
  2074. {
  2075. var startDate = DateTime.UtcNow;
  2076. var endDate = startDate.AddDays(14);
  2077. return new GuideInfo
  2078. {
  2079. StartDate = startDate,
  2080. EndDate = endDate
  2081. };
  2082. }
  2083. /// <summary>
  2084. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  2085. /// </summary>
  2086. public void Dispose()
  2087. {
  2088. Dispose(true);
  2089. }
  2090. private bool _isDisposed = false;
  2091. /// <summary>
  2092. /// Releases unmanaged and - optionally - managed resources.
  2093. /// </summary>
  2094. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  2095. protected virtual void Dispose(bool dispose)
  2096. {
  2097. if (dispose)
  2098. {
  2099. _isDisposed = true;
  2100. }
  2101. }
  2102. private async Task<IEnumerable<LiveTvServiceInfo>> GetServiceInfos(CancellationToken cancellationToken)
  2103. {
  2104. var tasks = Services.Select(i => GetServiceInfo(i, cancellationToken));
  2105. return await Task.WhenAll(tasks).ConfigureAwait(false);
  2106. }
  2107. private async Task<LiveTvServiceInfo> GetServiceInfo(ILiveTvService service, CancellationToken cancellationToken)
  2108. {
  2109. var info = new LiveTvServiceInfo
  2110. {
  2111. Name = service.Name
  2112. };
  2113. var tunerIdPrefix = service.GetType().FullName.GetMD5().ToString("N") + "_";
  2114. try
  2115. {
  2116. var statusInfo = await service.GetStatusInfoAsync(cancellationToken).ConfigureAwait(false);
  2117. info.Status = statusInfo.Status;
  2118. info.StatusMessage = statusInfo.StatusMessage;
  2119. info.Version = statusInfo.Version;
  2120. info.HasUpdateAvailable = statusInfo.HasUpdateAvailable;
  2121. info.HomePageUrl = service.HomePageUrl;
  2122. info.IsVisible = statusInfo.IsVisible;
  2123. info.Tuners = statusInfo.Tuners.Select(i =>
  2124. {
  2125. string channelName = null;
  2126. if (!string.IsNullOrEmpty(i.ChannelId))
  2127. {
  2128. var internalChannelId = _tvDtoService.GetInternalChannelId(service.Name, i.ChannelId);
  2129. var channel = GetInternalChannel(internalChannelId);
  2130. channelName = channel == null ? null : channel.Name;
  2131. }
  2132. var dto = _tvDtoService.GetTunerInfoDto(service.Name, i, channelName);
  2133. dto.Id = tunerIdPrefix + dto.Id;
  2134. return dto;
  2135. }).ToList();
  2136. }
  2137. catch (Exception ex)
  2138. {
  2139. _logger.ErrorException("Error getting service status info from {0}", ex, service.Name ?? string.Empty);
  2140. info.Status = LiveTvServiceStatus.Unavailable;
  2141. info.StatusMessage = ex.Message;
  2142. }
  2143. return info;
  2144. }
  2145. public async Task<LiveTvInfo> GetLiveTvInfo(CancellationToken cancellationToken)
  2146. {
  2147. var services = await GetServiceInfos(CancellationToken.None).ConfigureAwait(false);
  2148. var servicesList = services.ToList();
  2149. var info = new LiveTvInfo
  2150. {
  2151. Services = servicesList.ToList(),
  2152. IsEnabled = servicesList.Count > 0
  2153. };
  2154. info.EnabledUsers = _userManager.Users
  2155. .Where(IsLiveTvEnabled)
  2156. .Select(i => i.Id.ToString("N"))
  2157. .ToList();
  2158. return info;
  2159. }
  2160. private bool IsLiveTvEnabled(User user)
  2161. {
  2162. return user.Policy.EnableLiveTvAccess && (Services.Count > 1 || GetConfiguration().TunerHosts.Count(i => i.IsEnabled) > 0);
  2163. }
  2164. public IEnumerable<User> GetEnabledUsers()
  2165. {
  2166. return _userManager.Users
  2167. .Where(IsLiveTvEnabled);
  2168. }
  2169. /// <summary>
  2170. /// Resets the tuner.
  2171. /// </summary>
  2172. /// <param name="id">The identifier.</param>
  2173. /// <param name="cancellationToken">The cancellation token.</param>
  2174. /// <returns>Task.</returns>
  2175. public Task ResetTuner(string id, CancellationToken cancellationToken)
  2176. {
  2177. var parts = id.Split(new[] { '_' }, 2);
  2178. var service = _services.FirstOrDefault(i => string.Equals(i.GetType().FullName.GetMD5().ToString("N"), parts[0], StringComparison.OrdinalIgnoreCase));
  2179. if (service == null)
  2180. {
  2181. throw new ArgumentException("Service not found.");
  2182. }
  2183. return service.ResetTuner(parts[1], cancellationToken);
  2184. }
  2185. public async Task<BaseItemDto> GetLiveTvFolder(string userId, CancellationToken cancellationToken)
  2186. {
  2187. var user = string.IsNullOrEmpty(userId) ? null : _userManager.GetUserById(userId);
  2188. var folder = await GetInternalLiveTvFolder(cancellationToken).ConfigureAwait(false);
  2189. return _dtoService.GetBaseItemDto(folder, new DtoOptions(), user);
  2190. }
  2191. private void RemoveFields(DtoOptions options)
  2192. {
  2193. options.Fields.Remove(ItemFields.CanDelete);
  2194. options.Fields.Remove(ItemFields.CanDownload);
  2195. options.Fields.Remove(ItemFields.DisplayPreferencesId);
  2196. options.Fields.Remove(ItemFields.Etag);
  2197. }
  2198. public async Task<Folder> GetInternalLiveTvFolder(CancellationToken cancellationToken)
  2199. {
  2200. var name = _localization.GetLocalizedString("ViewTypeLiveTV");
  2201. return await _libraryManager.GetNamedView(name, CollectionType.LiveTv, name, cancellationToken).ConfigureAwait(false);
  2202. }
  2203. public async Task<TunerHostInfo> SaveTunerHost(TunerHostInfo info, bool dataSourceChanged = true)
  2204. {
  2205. info = (TunerHostInfo)_jsonSerializer.DeserializeFromString(_jsonSerializer.SerializeToString(info), typeof(TunerHostInfo));
  2206. var provider = _tunerHosts.FirstOrDefault(i => string.Equals(info.Type, i.Type, StringComparison.OrdinalIgnoreCase));
  2207. if (provider == null)
  2208. {
  2209. throw new ResourceNotFoundException();
  2210. }
  2211. var configurable = provider as IConfigurableTunerHost;
  2212. if (configurable != null)
  2213. {
  2214. await configurable.Validate(info).ConfigureAwait(false);
  2215. }
  2216. var config = GetConfiguration();
  2217. var index = config.TunerHosts.FindIndex(i => string.Equals(i.Id, info.Id, StringComparison.OrdinalIgnoreCase));
  2218. if (index == -1 || string.IsNullOrWhiteSpace(info.Id))
  2219. {
  2220. info.Id = Guid.NewGuid().ToString("N");
  2221. config.TunerHosts.Add(info);
  2222. }
  2223. else
  2224. {
  2225. config.TunerHosts[index] = info;
  2226. }
  2227. _config.SaveConfiguration("livetv", config);
  2228. if (dataSourceChanged)
  2229. {
  2230. _taskManager.CancelIfRunningAndQueue<RefreshChannelsScheduledTask>();
  2231. }
  2232. return info;
  2233. }
  2234. public async Task<ListingsProviderInfo> SaveListingProvider(ListingsProviderInfo info, bool validateLogin, bool validateListings)
  2235. {
  2236. info = (ListingsProviderInfo)_jsonSerializer.DeserializeFromString(_jsonSerializer.SerializeToString(info), typeof(ListingsProviderInfo));
  2237. var provider = _listingProviders.FirstOrDefault(i => string.Equals(info.Type, i.Type, StringComparison.OrdinalIgnoreCase));
  2238. if (provider == null)
  2239. {
  2240. throw new ResourceNotFoundException();
  2241. }
  2242. await provider.Validate(info, validateLogin, validateListings).ConfigureAwait(false);
  2243. var config = GetConfiguration();
  2244. var index = config.ListingProviders.FindIndex(i => string.Equals(i.Id, info.Id, StringComparison.OrdinalIgnoreCase));
  2245. if (index == -1 || string.IsNullOrWhiteSpace(info.Id))
  2246. {
  2247. info.Id = Guid.NewGuid().ToString("N");
  2248. config.ListingProviders.Add(info);
  2249. }
  2250. else
  2251. {
  2252. config.ListingProviders[index] = info;
  2253. }
  2254. _config.SaveConfiguration("livetv", config);
  2255. _taskManager.CancelIfRunningAndQueue<RefreshChannelsScheduledTask>();
  2256. return info;
  2257. }
  2258. public void DeleteListingsProvider(string id)
  2259. {
  2260. var config = GetConfiguration();
  2261. config.ListingProviders = config.ListingProviders.Where(i => !string.Equals(id, i.Id, StringComparison.OrdinalIgnoreCase)).ToList();
  2262. _config.SaveConfiguration("livetv", config);
  2263. _taskManager.CancelIfRunningAndQueue<RefreshChannelsScheduledTask>();
  2264. }
  2265. public async Task<TunerChannelMapping> SetChannelMapping(string providerId, string tunerChannelNumber, string providerChannelNumber)
  2266. {
  2267. var config = GetConfiguration();
  2268. var listingsProviderInfo = config.ListingProviders.First(i => string.Equals(providerId, i.Id, StringComparison.OrdinalIgnoreCase));
  2269. listingsProviderInfo.ChannelMappings = listingsProviderInfo.ChannelMappings.Where(i => !string.Equals(i.Name, tunerChannelNumber, StringComparison.OrdinalIgnoreCase)).ToArray();
  2270. if (!string.Equals(tunerChannelNumber, providerChannelNumber, StringComparison.OrdinalIgnoreCase))
  2271. {
  2272. var list = listingsProviderInfo.ChannelMappings.ToList();
  2273. list.Add(new NameValuePair
  2274. {
  2275. Name = tunerChannelNumber,
  2276. Value = providerChannelNumber
  2277. });
  2278. listingsProviderInfo.ChannelMappings = list.ToArray();
  2279. }
  2280. _config.SaveConfiguration("livetv", config);
  2281. var tunerChannels = await GetChannelsForListingsProvider(providerId, CancellationToken.None)
  2282. .ConfigureAwait(false);
  2283. var providerChannels = await GetChannelsFromListingsProviderData(providerId, CancellationToken.None)
  2284. .ConfigureAwait(false);
  2285. var mappings = listingsProviderInfo.ChannelMappings.ToList();
  2286. var tunerChannelMappings =
  2287. tunerChannels.Select(i => GetTunerChannelMapping(i, mappings, providerChannels)).ToList();
  2288. _taskManager.CancelIfRunningAndQueue<RefreshChannelsScheduledTask>();
  2289. return tunerChannelMappings.First(i => string.Equals(i.Number, tunerChannelNumber, StringComparison.OrdinalIgnoreCase));
  2290. }
  2291. public TunerChannelMapping GetTunerChannelMapping(ChannelInfo channel, List<NameValuePair> mappings, List<ChannelInfo> providerChannels)
  2292. {
  2293. var result = new TunerChannelMapping
  2294. {
  2295. Name = channel.Number + " " + channel.Name,
  2296. Number = channel.Number
  2297. };
  2298. var mapping = mappings.FirstOrDefault(i => string.Equals(i.Name, channel.Number, StringComparison.OrdinalIgnoreCase));
  2299. var providerChannelNumber = channel.Number;
  2300. if (mapping != null)
  2301. {
  2302. providerChannelNumber = mapping.Value;
  2303. }
  2304. var providerChannel = providerChannels.FirstOrDefault(i => string.Equals(i.Number, providerChannelNumber, StringComparison.OrdinalIgnoreCase));
  2305. if (providerChannel != null)
  2306. {
  2307. result.ProviderChannelNumber = providerChannel.Number;
  2308. result.ProviderChannelName = providerChannel.Name;
  2309. }
  2310. return result;
  2311. }
  2312. public Task<List<NameIdPair>> GetLineups(string providerType, string providerId, string country, string location)
  2313. {
  2314. var config = GetConfiguration();
  2315. if (string.IsNullOrWhiteSpace(providerId))
  2316. {
  2317. var provider = _listingProviders.FirstOrDefault(i => string.Equals(providerType, i.Type, StringComparison.OrdinalIgnoreCase));
  2318. if (provider == null)
  2319. {
  2320. throw new ResourceNotFoundException();
  2321. }
  2322. return provider.GetLineups(null, country, location);
  2323. }
  2324. else
  2325. {
  2326. var info = config.ListingProviders.FirstOrDefault(i => string.Equals(i.Id, providerId, StringComparison.OrdinalIgnoreCase));
  2327. var provider = _listingProviders.FirstOrDefault(i => string.Equals(info.Type, i.Type, StringComparison.OrdinalIgnoreCase));
  2328. if (provider == null)
  2329. {
  2330. throw new ResourceNotFoundException();
  2331. }
  2332. return provider.GetLineups(info, country, location);
  2333. }
  2334. }
  2335. public Task<MBRegistrationRecord> GetRegistrationInfo(string feature)
  2336. {
  2337. if (string.Equals(feature, "seriesrecordings", StringComparison.OrdinalIgnoreCase))
  2338. {
  2339. feature = "embytvseriesrecordings";
  2340. }
  2341. if (string.Equals(feature, "dvr-l", StringComparison.OrdinalIgnoreCase))
  2342. {
  2343. var config = GetConfiguration();
  2344. if (config.TunerHosts.Count(i => i.IsEnabled) > 0 &&
  2345. config.ListingProviders.Count(i => (i.EnableAllTuners || i.EnabledTuners.Length > 0) && string.Equals(i.Type, SchedulesDirect.TypeName, StringComparison.OrdinalIgnoreCase)) > 0)
  2346. {
  2347. return Task.FromResult(new MBRegistrationRecord
  2348. {
  2349. IsRegistered = true,
  2350. IsValid = true
  2351. });
  2352. }
  2353. }
  2354. return _security.GetRegistrationStatus(feature);
  2355. }
  2356. public List<NameValuePair> GetSatIniMappings()
  2357. {
  2358. var names = GetType().Assembly.GetManifestResourceNames().Where(i => i.IndexOf("SatIp.ini", StringComparison.OrdinalIgnoreCase) != -1).ToList();
  2359. return names.Select(GetSatIniMappings).Where(i => i != null).DistinctBy(i => i.Value.Split('|')[0]).ToList();
  2360. }
  2361. public NameValuePair GetSatIniMappings(string resource)
  2362. {
  2363. using (var stream = GetType().Assembly.GetManifestResourceStream(resource))
  2364. {
  2365. using (var reader = new StreamReader(stream))
  2366. {
  2367. var parser = new StreamIniDataParser();
  2368. IniData data = parser.ReadData(reader);
  2369. var satType1 = data["SATTYPE"]["1"];
  2370. var satType2 = data["SATTYPE"]["2"];
  2371. if (string.IsNullOrWhiteSpace(satType2))
  2372. {
  2373. return null;
  2374. }
  2375. var srch = "SatIp.ini.";
  2376. var filename = Path.GetFileName(resource);
  2377. return new NameValuePair
  2378. {
  2379. Name = satType1 + " " + satType2,
  2380. Value = satType2 + "|" + filename.Substring(filename.IndexOf(srch) + srch.Length)
  2381. };
  2382. }
  2383. }
  2384. }
  2385. public Task<List<ChannelInfo>> GetSatChannelScanResult(TunerHostInfo info, CancellationToken cancellationToken)
  2386. {
  2387. return new TunerHosts.SatIp.ChannelScan(_logger).Scan(info, cancellationToken);
  2388. }
  2389. public Task<List<ChannelInfo>> GetChannelsForListingsProvider(string id, CancellationToken cancellationToken)
  2390. {
  2391. var info = GetConfiguration().ListingProviders.First(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
  2392. return EmbyTV.EmbyTV.Current.GetChannelsForListingsProvider(info, cancellationToken);
  2393. }
  2394. public Task<List<ChannelInfo>> GetChannelsFromListingsProviderData(string id, CancellationToken cancellationToken)
  2395. {
  2396. var info = GetConfiguration().ListingProviders.First(i => string.Equals(i.Id, id, StringComparison.OrdinalIgnoreCase));
  2397. var provider = _listingProviders.First(i => string.Equals(i.Type, info.Type, StringComparison.OrdinalIgnoreCase));
  2398. return provider.GetChannels(info, cancellationToken);
  2399. }
  2400. }
  2401. }