2
0

Video.cs 17 KB

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