Video.cs 17 KB

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