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. 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 a value indicating whether this instance has subtitles.
  84. /// </summary>
  85. /// <value><c>true</c> if this instance has subtitles; otherwise, <c>false</c>.</value>
  86. public bool HasSubtitles { get; set; }
  87. public bool IsPlaceHolder { get; set; }
  88. /// <summary>
  89. /// Gets or sets the default index of the video stream.
  90. /// </summary>
  91. /// <value>The default index of the video stream.</value>
  92. public int? DefaultVideoStreamIndex { get; set; }
  93. /// <summary>
  94. /// Gets or sets the type of the video.
  95. /// </summary>
  96. /// <value>The type of the video.</value>
  97. public VideoType VideoType { get; set; }
  98. /// <summary>
  99. /// Gets or sets the type of the iso.
  100. /// </summary>
  101. /// <value>The type of the iso.</value>
  102. public IsoType? IsoType { get; set; }
  103. /// <summary>
  104. /// Gets or sets the video3 D format.
  105. /// </summary>
  106. /// <value>The video3 D format.</value>
  107. public Video3DFormat? Video3DFormat { get; set; }
  108. /// <summary>
  109. /// Gets or sets the aspect ratio.
  110. /// </summary>
  111. /// <value>The aspect ratio.</value>
  112. public string AspectRatio { get; set; }
  113. [JsonIgnore]
  114. public override bool SupportsAddingToPlaylist => true;
  115. [JsonIgnore]
  116. public int MediaSourceCount
  117. {
  118. get
  119. {
  120. if (!string.IsNullOrEmpty(PrimaryVersionId))
  121. {
  122. var item = LibraryManager.GetItemById(PrimaryVersionId);
  123. if (item is Video video)
  124. {
  125. return video.MediaSourceCount;
  126. }
  127. }
  128. return LinkedAlternateVersions.Length + LocalAlternateVersions.Length + 1;
  129. }
  130. }
  131. [JsonIgnore]
  132. public bool IsStacked => AdditionalParts.Length > 0;
  133. [JsonIgnore]
  134. public override bool HasLocalAlternateVersions => LocalAlternateVersions.Length > 0;
  135. public static ILiveTvManager LiveTvManager { 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", StringComparer.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 string MediaType => Model.Entities.MediaType.Video;
  209. public override List<string> GetUserDataKeys()
  210. {
  211. var list = base.GetUserDataKeys();
  212. if (EnableDefaultVideoUserDataKeys)
  213. {
  214. if (ExtraType.HasValue)
  215. {
  216. var key = this.GetProviderId(MetadataProvider.Tmdb);
  217. if (!string.IsNullOrEmpty(key))
  218. {
  219. list.Insert(0, GetUserDataKey(key));
  220. }
  221. key = this.GetProviderId(MetadataProvider.Imdb);
  222. if (!string.IsNullOrEmpty(key))
  223. {
  224. list.Insert(0, GetUserDataKey(key));
  225. }
  226. }
  227. else
  228. {
  229. var key = this.GetProviderId(MetadataProvider.Imdb);
  230. if (!string.IsNullOrEmpty(key))
  231. {
  232. list.Insert(0, key);
  233. }
  234. key = this.GetProviderId(MetadataProvider.Tmdb);
  235. if (!string.IsNullOrEmpty(key))
  236. {
  237. list.Insert(0, key);
  238. }
  239. }
  240. }
  241. return list;
  242. }
  243. public void SetPrimaryVersionId(string id)
  244. {
  245. if (string.IsNullOrEmpty(id))
  246. {
  247. PrimaryVersionId = null;
  248. }
  249. else
  250. {
  251. PrimaryVersionId = id;
  252. }
  253. PresentationUniqueKey = CreatePresentationUniqueKey();
  254. }
  255. public override string CreatePresentationUniqueKey()
  256. {
  257. if (!string.IsNullOrEmpty(PrimaryVersionId))
  258. {
  259. return PrimaryVersionId;
  260. }
  261. return base.CreatePresentationUniqueKey();
  262. }
  263. public override bool CanDownload()
  264. {
  265. if (VideoType == VideoType.Dvd || VideoType == VideoType.BluRay)
  266. {
  267. return false;
  268. }
  269. return IsFileProtocol;
  270. }
  271. protected override bool IsActiveRecording()
  272. {
  273. return LiveTvManager.GetActiveRecordingInfo(Path) != null;
  274. }
  275. public override bool CanDelete()
  276. {
  277. if (IsActiveRecording())
  278. {
  279. return false;
  280. }
  281. return base.CanDelete();
  282. }
  283. public IEnumerable<Guid> GetAdditionalPartIds()
  284. {
  285. return AdditionalParts.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  286. }
  287. public IEnumerable<Guid> GetLocalAlternateVersionIds()
  288. {
  289. return LocalAlternateVersions.Select(i => LibraryManager.GetNewItemId(i, typeof(Video)));
  290. }
  291. private string GetUserDataKey(string providerId)
  292. {
  293. var key = providerId + "-" + ExtraType.ToString().ToLowerInvariant();
  294. // Make sure different trailers have their own data.
  295. if (RunTimeTicks.HasValue)
  296. {
  297. key += "-" + RunTimeTicks.Value.ToString(CultureInfo.InvariantCulture);
  298. }
  299. return key;
  300. }
  301. public IEnumerable<Video> GetLinkedAlternateVersions()
  302. {
  303. return LinkedAlternateVersions
  304. .Select(GetLinkedChild)
  305. .Where(i => i != null)
  306. .OfType<Video>()
  307. .OrderBy(i => i.SortName);
  308. }
  309. /// <summary>
  310. /// Gets the additional parts.
  311. /// </summary>
  312. /// <returns>IEnumerable{Video}.</returns>
  313. public IOrderedEnumerable<Video> GetAdditionalParts()
  314. {
  315. return GetAdditionalPartIds()
  316. .Select(i => LibraryManager.GetItemById(i))
  317. .Where(i => i != null)
  318. .OfType<Video>()
  319. .OrderBy(i => i.SortName);
  320. }
  321. internal override ItemUpdateType UpdateFromResolvedItem(BaseItem newItem)
  322. {
  323. var updateType = base.UpdateFromResolvedItem(newItem);
  324. if (newItem is Video newVideo)
  325. {
  326. if (!AdditionalParts.SequenceEqual(newVideo.AdditionalParts, StringComparer.Ordinal))
  327. {
  328. AdditionalParts = newVideo.AdditionalParts;
  329. updateType |= ItemUpdateType.MetadataImport;
  330. }
  331. if (!LocalAlternateVersions.SequenceEqual(newVideo.LocalAlternateVersions, StringComparer.Ordinal))
  332. {
  333. LocalAlternateVersions = newVideo.LocalAlternateVersions;
  334. updateType |= ItemUpdateType.MetadataImport;
  335. }
  336. if (VideoType != newVideo.VideoType)
  337. {
  338. VideoType = newVideo.VideoType;
  339. updateType |= ItemUpdateType.MetadataImport;
  340. }
  341. }
  342. return updateType;
  343. }
  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. }