Video.cs 17 KB

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