Video.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Text.Json.Serialization;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.LiveTv;
  12. using MediaBrowser.Controller.Persistence;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.IO;
  17. using MediaBrowser.Model.MediaInfo;
  18. namespace MediaBrowser.Controller.Entities
  19. {
  20. /// <summary>
  21. /// Class Video.
  22. /// </summary>
  23. public class Video : BaseItem,
  24. IHasAspectRatio,
  25. ISupportsPlaceHolders,
  26. IHasMediaSources
  27. {
  28. [JsonIgnore]
  29. public string PrimaryVersionId { get; set; }
  30. public string[] AdditionalParts { get; set; }
  31. public string[] LocalAlternateVersions { get; set; }
  32. public LinkedChild[] LinkedAlternateVersions { get; set; }
  33. [JsonIgnore]
  34. public override bool SupportsPlayedStatus => true;
  35. [JsonIgnore]
  36. public override bool SupportsPeople => true;
  37. [JsonIgnore]
  38. public override bool SupportsInheritedParentImages => true;
  39. [JsonIgnore]
  40. public override bool SupportsPositionTicksResume
  41. {
  42. get
  43. {
  44. var extraType = ExtraType;
  45. if (extraType.HasValue)
  46. {
  47. if (extraType.Value == Model.Entities.ExtraType.Sample)
  48. {
  49. return false;
  50. }
  51. if (extraType.Value == Model.Entities.ExtraType.ThemeVideo)
  52. {
  53. return false;
  54. }
  55. if (extraType.Value == Model.Entities.ExtraType.Trailer)
  56. {
  57. return false;
  58. }
  59. }
  60. return true;
  61. }
  62. }
  63. public void SetPrimaryVersionId(string id)
  64. {
  65. if (string.IsNullOrEmpty(id))
  66. {
  67. PrimaryVersionId = null;
  68. }
  69. else
  70. {
  71. PrimaryVersionId = id;
  72. }
  73. PresentationUniqueKey = CreatePresentationUniqueKey();
  74. }
  75. public override string CreatePresentationUniqueKey()
  76. {
  77. if (!string.IsNullOrEmpty(PrimaryVersionId))
  78. {
  79. return PrimaryVersionId;
  80. }
  81. return base.CreatePresentationUniqueKey();
  82. }
  83. [JsonIgnore]
  84. public override bool SupportsThemeMedia => true;
  85. /// <summary>
  86. /// Gets or sets the timestamp.
  87. /// </summary>
  88. /// <value>The timestamp.</value>
  89. public TransportStreamTimestamp? Timestamp { get; set; }
  90. /// <summary>
  91. /// Gets or sets the subtitle paths.
  92. /// </summary>
  93. /// <value>The subtitle paths.</value>
  94. public string[] SubtitleFiles { get; set; }
  95. /// <summary>
  96. /// Gets or sets a value indicating whether this instance has subtitles.
  97. /// </summary>
  98. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  99. public bool HasSubtitles { get; set; }
  100. public bool IsPlaceHolder { get; set; }
  101. /// <summary>
  102. /// Gets or sets the default index of the video stream.
  103. /// </summary>
  104. /// <value>The default index of the video stream.</value>
  105. public int? DefaultVideoStreamIndex { get; set; }
  106. /// <summary>
  107. /// Gets or sets the type of the video.
  108. /// </summary>
  109. /// <value>The type of the video.</value>
  110. public VideoType VideoType { get; set; }
  111. /// <summary>
  112. /// Gets or sets the type of the iso.
  113. /// </summary>
  114. /// <value>The type of the iso.</value>
  115. public IsoType? IsoType { get; set; }
  116. /// <summary>
  117. /// Gets or sets the video3 D format.
  118. /// </summary>
  119. /// <value>The video3 D format.</value>
  120. public Video3DFormat? Video3DFormat { get; set; }
  121. /// <summary>
  122. /// Gets or sets the aspect ratio.
  123. /// </summary>
  124. /// <value>The aspect ratio.</value>
  125. public string AspectRatio { get; set; }
  126. public Video()
  127. {
  128. AdditionalParts = Array.Empty<string>();
  129. LocalAlternateVersions = Array.Empty<string>();
  130. SubtitleFiles = Array.Empty<string>();
  131. LinkedAlternateVersions = Array.Empty<LinkedChild>();
  132. }
  133. public override bool CanDownload()
  134. {
  135. if (VideoType == VideoType.Dvd || VideoType == VideoType.BluRay)
  136. {
  137. return false;
  138. }
  139. return IsFileProtocol;
  140. }
  141. [JsonIgnore]
  142. public override bool SupportsAddingToPlaylist => true;
  143. [JsonIgnore]
  144. public int MediaSourceCount
  145. {
  146. get
  147. {
  148. if (!string.IsNullOrEmpty(PrimaryVersionId))
  149. {
  150. var item = LibraryManager.GetItemById(PrimaryVersionId);
  151. if (item is Video video)
  152. {
  153. return video.MediaSourceCount;
  154. }
  155. }
  156. return LinkedAlternateVersions.Length + LocalAlternateVersions.Length + 1;
  157. }
  158. }
  159. [JsonIgnore]
  160. public bool IsStacked => AdditionalParts.Length > 0;
  161. [JsonIgnore]
  162. public override bool HasLocalAlternateVersions => LocalAlternateVersions.Length > 0;
  163. public IEnumerable<Guid> GetAdditionalPartIds()
  164. {
  165. return AdditionalParts.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  166. }
  167. public IEnumerable<Guid> GetLocalAlternateVersionIds()
  168. {
  169. return LocalAlternateVersions.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  170. }
  171. public static ILiveTvManager LiveTvManager { get; set; }
  172. [JsonIgnore]
  173. public override SourceType SourceType
  174. {
  175. get
  176. {
  177. if (IsActiveRecording())
  178. {
  179. return SourceType.LiveTV;
  180. }
  181. return base.SourceType;
  182. }
  183. }
  184. protected override bool IsActiveRecording()
  185. {
  186. return LiveTvManager.GetActiveRecordingInfo(Path) != null;
  187. }
  188. public override bool CanDelete()
  189. {
  190. if (IsActiveRecording())
  191. {
  192. return false;
  193. }
  194. return base.CanDelete();
  195. }
  196. [JsonIgnore]
  197. public bool IsCompleteMedia
  198. {
  199. get
  200. {
  201. if (SourceType == SourceType.Channel)
  202. {
  203. return !Tags.Contains("livestream", StringComparer.OrdinalIgnoreCase);
  204. }
  205. return !IsActiveRecording();
  206. }
  207. }
  208. [JsonIgnore]
  209. protected virtual bool EnableDefaultVideoUserDataKeys => true;
  210. public override List<string> GetUserDataKeys()
  211. {
  212. var list = base.GetUserDataKeys();
  213. if (EnableDefaultVideoUserDataKeys)
  214. {
  215. if (ExtraType.HasValue)
  216. {
  217. var key = this.GetProviderId(MetadataProvider.Tmdb);
  218. if (!string.IsNullOrEmpty(key))
  219. {
  220. list.Insert(0, GetUserDataKey(key));
  221. }
  222. key = this.GetProviderId(MetadataProvider.Imdb);
  223. if (!string.IsNullOrEmpty(key))
  224. {
  225. list.Insert(0, GetUserDataKey(key));
  226. }
  227. }
  228. else
  229. {
  230. var key = this.GetProviderId(MetadataProvider.Imdb);
  231. if (!string.IsNullOrEmpty(key))
  232. {
  233. list.Insert(0, key);
  234. }
  235. key = this.GetProviderId(MetadataProvider.Tmdb);
  236. if (!string.IsNullOrEmpty(key))
  237. {
  238. list.Insert(0, key);
  239. }
  240. }
  241. }
  242. return list;
  243. }
  244. private string GetUserDataKey(string providerId)
  245. {
  246. var key = providerId + "-" + ExtraType.ToString().ToLowerInvariant();
  247. // Make sure different trailers have their own data.
  248. if (RunTimeTicks.HasValue)
  249. {
  250. key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture);
  251. }
  252. return key;
  253. }
  254. public IEnumerable<Video> GetLinkedAlternateVersions()
  255. {
  256. return LinkedAlternateVersions
  257. .Select(GetLinkedChild)
  258. .Where(i => i != null)
  259. .OfType<Video>()
  260. .OrderBy(i => i.SortName);
  261. }
  262. /// <summary>
  263. /// Gets the additional parts.
  264. /// </summary>
  265. /// <returns>IEnumerable{Video}.</returns>
  266. public IOrderedEnumerable<Video> GetAdditionalParts()
  267. {
  268. return GetAdditionalPartIds()
  269. .Select(i => LibraryManager.GetItemById(i))
  270. .Where(i => i != null)
  271. .OfType<Video>()
  272. .OrderBy(i => i.SortName);
  273. }
  274. [JsonIgnore]
  275. public override string ContainingFolderPath
  276. {
  277. get
  278. {
  279. if (IsStacked)
  280. {
  281. return System.IO.Path.GetDirectoryName(Path);
  282. }
  283. if (!IsPlaceHolder)
  284. {
  285. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  286. {
  287. return Path;
  288. }
  289. }
  290. return base.ContainingFolderPath;
  291. }
  292. }
  293. [JsonIgnore]
  294. public override string FileNameWithoutExtension
  295. {
  296. get
  297. {
  298. if (IsFileProtocol)
  299. {
  300. if (VideoType == VideoType.BluRay || VideoType == VideoType.Dvd)
  301. {
  302. return System.IO.Path.GetFileName(Path);
  303. }
  304. return System.IO.Path.GetFileNameWithoutExtension(Path);
  305. }
  306. return null;
  307. }
  308. }
  309. internal override ItemUpdateType UpdateFromResolvedItem(BaseItem newItem)
  310. {
  311. var updateType = base.UpdateFromResolvedItem(newItem);
  312. if (newItem is Video newVideo)
  313. {
  314. if (!AdditionalParts.SequenceEqual(newVideo.AdditionalParts, StringComparer.Ordinal))
  315. {
  316. AdditionalParts = newVideo.AdditionalParts;
  317. updateType |= ItemUpdateType.MetadataImport;
  318. }
  319. if (!LocalAlternateVersions.SequenceEqual(newVideo.LocalAlternateVersions, StringComparer.Ordinal))
  320. {
  321. LocalAlternateVersions = newVideo.LocalAlternateVersions;
  322. updateType |= ItemUpdateType.MetadataImport;
  323. }
  324. if (VideoType != newVideo.VideoType)
  325. {
  326. VideoType = newVideo.VideoType;
  327. updateType |= ItemUpdateType.MetadataImport;
  328. }
  329. }
  330. return updateType;
  331. }
  332. /// <summary>
  333. /// Gets a value indicating whether [is3 D].
  334. /// </summary>
  335. /// <value><c>true</c> if [is3 D]; otherwise, <c>false</c>.</value>
  336. [JsonIgnore]
  337. public bool Is3D => Video3DFormat.HasValue;
  338. /// <summary>
  339. /// Gets the type of the media.
  340. /// </summary>
  341. /// <value>The type of the media.</value>
  342. [JsonIgnore]
  343. public override string MediaType => Model.Entities.MediaType.Video;
  344. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  345. {
  346. var hasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  347. if (IsStacked)
  348. {
  349. var tasks = AdditionalParts
  350. .Select(i => RefreshMetadataForOwnedVideo(options, true, i, cancellationToken));
  351. await Task.WhenAll(tasks).ConfigureAwait(false);
  352. }
  353. // Must have a parent to have additional parts or alternate versions
  354. // In other words, it must be part of the Parent/Child tree
  355. // The additional parts won't have additional parts themselves
  356. if (IsFileProtocol && SupportsOwnedItems)
  357. {
  358. if (!IsStacked)
  359. {
  360. RefreshLinkedAlternateVersions();
  361. var tasks = LocalAlternateVersions
  362. .Select(i => RefreshMetadataForOwnedVideo(options, false, i, cancellationToken));
  363. await Task.WhenAll(tasks).ConfigureAwait(false);
  364. }
  365. }
  366. return hasChanges;
  367. }
  368. private void RefreshLinkedAlternateVersions()
  369. {
  370. foreach (var child in LinkedAlternateVersions)
  371. {
  372. // Reset the cached value
  373. if (child.ItemId.HasValue && child.ItemId.Value.Equals(Guid.Empty))
  374. {
  375. child.ItemId = null;
  376. }
  377. }
  378. }
  379. /// <inheritdoc />
  380. public override async Task UpdateToRepositoryAsync(ItemUpdateType updateReason, CancellationToken cancellationToken)
  381. {
  382. await base.UpdateToRepositoryAsync(updateReason, cancellationToken).ConfigureAwait(false);
  383. var localAlternates = GetLocalAlternateVersionIds()
  384. .Select(i => LibraryManager.GetItemById(i))
  385. .Where(i => i != null);
  386. foreach (var item in localAlternates)
  387. {
  388. item.ImageInfos = ImageInfos;
  389. item.Overview = Overview;
  390. item.ProductionYear = ProductionYear;
  391. item.PremiereDate = PremiereDate;
  392. item.CommunityRating = CommunityRating;
  393. item.OfficialRating = OfficialRating;
  394. item.Genres = Genres;
  395. item.ProviderIds = ProviderIds;
  396. await item.UpdateToRepositoryAsync(ItemUpdateType.MetadataDownload, cancellationToken).ConfigureAwait(false);
  397. }
  398. }
  399. public override IEnumerable<FileSystemMetadata> GetDeletePaths()
  400. {
  401. if (!IsInMixedFolder)
  402. {
  403. return new[]
  404. {
  405. new FileSystemMetadata
  406. {
  407. FullName = ContainingFolderPath,
  408. IsDirectory = true
  409. }
  410. };
  411. }
  412. return base.GetDeletePaths();
  413. }
  414. public virtual MediaStream GetDefaultVideoStream()
  415. {
  416. if (!DefaultVideoStreamIndex.HasValue)
  417. {
  418. return null;
  419. }
  420. return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
  421. {
  422. ItemId = Id,
  423. Index = DefaultVideoStreamIndex.Value
  424. }).FirstOrDefault();
  425. }
  426. protected override List<Tuple<BaseItem, MediaSourceType>> GetAllItemsForMediaSources()
  427. {
  428. var list = new List<Tuple<BaseItem, MediaSourceType>>();
  429. list.Add(new Tuple<BaseItem, MediaSourceType>(this, MediaSourceType.Default));
  430. list.AddRange(GetLinkedAlternateVersions().Select(i => new Tuple<BaseItem, MediaSourceType>(i, MediaSourceType.Grouping)));
  431. if (!string.IsNullOrEmpty(PrimaryVersionId))
  432. {
  433. var primary = LibraryManager.GetItemById(PrimaryVersionId) as Video;
  434. if (primary != null)
  435. {
  436. var existingIds = list.Select(i => i.Item1.Id).ToList();
  437. list.Add(new Tuple<BaseItem, MediaSourceType>(primary, MediaSourceType.Grouping));
  438. list.AddRange(primary.GetLinkedAlternateVersions().Where(i => !existingIds.Contains(i.Id)).Select(i => new Tuple<BaseItem, MediaSourceType>(i, MediaSourceType.Grouping)));
  439. }
  440. }
  441. var localAlternates = list
  442. .SelectMany(i =>
  443. {
  444. var video = i.Item1 as Video;
  445. return video == null ? new List<Guid>() : video.GetLocalAlternateVersionIds();
  446. })
  447. .Select(LibraryManager.GetItemById)
  448. .Where(i => i != null)
  449. .ToList();
  450. list.AddRange(localAlternates.Select(i => new Tuple<BaseItem, MediaSourceType>(i, MediaSourceType.Default)));
  451. return list;
  452. }
  453. }
  454. }