Video.cs 17 KB

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