Video.cs 17 KB

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