TrickplayProvider.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Movies;
  6. using MediaBrowser.Controller.Entities.TV;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Controller.Trickplay;
  10. using MediaBrowser.Model.Configuration;
  11. namespace MediaBrowser.Providers.Trickplay;
  12. /// <summary>
  13. /// Class TrickplayProvider. Provides images and metadata for trickplay
  14. /// scrubbing previews.
  15. /// </summary>
  16. public class TrickplayProvider : ICustomMetadataProvider<Episode>,
  17. ICustomMetadataProvider<MusicVideo>,
  18. ICustomMetadataProvider<Movie>,
  19. ICustomMetadataProvider<Trailer>,
  20. ICustomMetadataProvider<Video>,
  21. IHasItemChangeMonitor,
  22. IHasOrder,
  23. IForcedProvider
  24. {
  25. private readonly IServerConfigurationManager _config;
  26. private readonly ITrickplayManager _trickplayManager;
  27. private readonly ILibraryManager _libraryManager;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="TrickplayProvider"/> class.
  30. /// </summary>
  31. /// <param name="config">The configuration manager.</param>
  32. /// <param name="trickplayManager">The trickplay manager.</param>
  33. /// <param name="libraryManager">The library manager.</param>
  34. public TrickplayProvider(
  35. IServerConfigurationManager config,
  36. ITrickplayManager trickplayManager,
  37. ILibraryManager libraryManager)
  38. {
  39. _config = config;
  40. _trickplayManager = trickplayManager;
  41. _libraryManager = libraryManager;
  42. }
  43. /// <inheritdoc />
  44. public string Name => "Trickplay Provider";
  45. /// <inheritdoc />
  46. public int Order => 100;
  47. /// <inheritdoc />
  48. public bool HasChanged(BaseItem item, IDirectoryService directoryService)
  49. {
  50. if (item.IsFileProtocol)
  51. {
  52. var file = directoryService.GetFile(item.Path);
  53. if (file is not null && item.DateModified != file.LastWriteTimeUtc)
  54. {
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. /// <inheritdoc />
  61. public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  62. {
  63. return FetchInternal(item, options, cancellationToken);
  64. }
  65. /// <inheritdoc />
  66. public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  67. {
  68. return FetchInternal(item, options, cancellationToken);
  69. }
  70. /// <inheritdoc />
  71. public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  72. {
  73. return FetchInternal(item, options, cancellationToken);
  74. }
  75. /// <inheritdoc />
  76. public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  77. {
  78. return FetchInternal(item, options, cancellationToken);
  79. }
  80. /// <inheritdoc />
  81. public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
  82. {
  83. return FetchInternal(item, options, cancellationToken);
  84. }
  85. private async Task<ItemUpdateType> FetchInternal(Video video, MetadataRefreshOptions options, CancellationToken cancellationToken)
  86. {
  87. var libraryOptions = _libraryManager.GetLibraryOptions(video);
  88. bool? enableDuringScan = libraryOptions?.ExtractTrickplayImagesDuringLibraryScan;
  89. bool replace = options.RegenerateTrickplay && options.MetadataRefreshMode > MetadataRefreshMode.Default;
  90. if (libraryOptions is null || !enableDuringScan.GetValueOrDefault(false))
  91. {
  92. return ItemUpdateType.None;
  93. }
  94. if (_config.Configuration.TrickplayOptions.ScanBehavior == TrickplayScanBehavior.Blocking)
  95. {
  96. await _trickplayManager.RefreshTrickplayDataAsync(video, replace, libraryOptions, cancellationToken).ConfigureAwait(false);
  97. }
  98. else
  99. {
  100. _ = _trickplayManager.RefreshTrickplayDataAsync(video, replace, libraryOptions, cancellationToken).ConfigureAwait(false);
  101. }
  102. // The core doesn't need to trigger any save operations over this
  103. return ItemUpdateType.None;
  104. }
  105. }