jellyfin-server/MediaBrowser.Providers/Trickplay/TrickplayProvider.cs

128 lines
4.7 KiB
C#
Raw Normal View History

2023-02-22 08:08:35 +00:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Trickplay;
2023-02-25 23:59:46 +00:00
using MediaBrowser.Model.Configuration;
2023-02-22 08:08:35 +00:00
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Trickplay
{
/// <summary>
/// Class TrickplayProvider. Provides images and metadata for trickplay
/// scrubbing previews.
/// </summary>
public class TrickplayProvider : ICustomMetadataProvider<Episode>,
ICustomMetadataProvider<MusicVideo>,
ICustomMetadataProvider<Movie>,
ICustomMetadataProvider<Trailer>,
ICustomMetadataProvider<Video>,
IHasItemChangeMonitor,
IHasOrder,
IForcedProvider
{
private readonly ILogger<TrickplayProvider> _logger;
2023-02-25 23:59:46 +00:00
private readonly IServerConfigurationManager _config;
2023-02-22 08:08:35 +00:00
private readonly ITrickplayManager _trickplayManager;
2023-02-24 00:04:35 +00:00
private readonly ILibraryManager _libraryManager;
2023-02-22 08:08:35 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="TrickplayProvider"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
2023-02-25 23:59:46 +00:00
/// <param name="config">The configuration manager.</param>
2023-02-22 08:08:35 +00:00
/// <param name="trickplayManager">The trickplay manager.</param>
2023-02-24 00:04:35 +00:00
/// <param name="libraryManager">The library manager.</param>
2023-02-22 08:08:35 +00:00
public TrickplayProvider(
ILogger<TrickplayProvider> logger,
2023-02-25 23:59:46 +00:00
IServerConfigurationManager config,
2023-02-24 00:04:35 +00:00
ITrickplayManager trickplayManager,
ILibraryManager libraryManager)
2023-02-22 08:08:35 +00:00
{
_logger = logger;
2023-02-25 23:59:46 +00:00
_config = config;
2023-02-22 08:08:35 +00:00
_trickplayManager = trickplayManager;
2023-02-24 00:04:35 +00:00
_libraryManager = libraryManager;
2023-02-22 08:08:35 +00:00
}
/// <inheritdoc />
2023-02-24 00:04:35 +00:00
public string Name => "Trickplay Provider";
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
2023-02-24 00:04:35 +00:00
public int Order => 100;
2023-02-22 08:08:35 +00:00
/// <inheritdoc />
public bool HasChanged(BaseItem item, IDirectoryService directoryService)
{
if (item.IsFileProtocol)
{
var file = directoryService.GetFile(item.Path);
if (file is not null && item.DateModified != file.LastWriteTimeUtc)
{
return true;
}
}
return false;
}
/// <inheritdoc />
public Task<ItemUpdateType> FetchAsync(Episode item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
return FetchInternal(item, options, cancellationToken);
}
/// <inheritdoc />
public Task<ItemUpdateType> FetchAsync(MusicVideo item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
return FetchInternal(item, options, cancellationToken);
}
/// <inheritdoc />
public Task<ItemUpdateType> FetchAsync(Movie item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
return FetchInternal(item, options, cancellationToken);
}
/// <inheritdoc />
public Task<ItemUpdateType> FetchAsync(Trailer item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
return FetchInternal(item, options, cancellationToken);
}
/// <inheritdoc />
public Task<ItemUpdateType> FetchAsync(Video item, MetadataRefreshOptions options, CancellationToken cancellationToken)
{
return FetchInternal(item, options, cancellationToken);
}
2023-02-24 00:04:35 +00:00
private async Task<ItemUpdateType> FetchInternal(Video video, MetadataRefreshOptions options, CancellationToken cancellationToken)
2023-02-22 08:08:35 +00:00
{
2023-02-24 00:04:35 +00:00
var libraryOptions = _libraryManager.GetLibraryOptions(video);
bool? enableDuringScan = libraryOptions?.ExtractTrickplayImagesDuringLibraryScan;
bool replace = options.ReplaceAllImages;
if (options.IsAutomated && !enableDuringScan.GetValueOrDefault(false))
{
return ItemUpdateType.None;
}
2023-02-25 23:59:46 +00:00
if (_config.Configuration.TrickplayOptions.ScanBehavior == TrickplayScanBehavior.Blocking)
2023-02-24 00:04:35 +00:00
{
await _trickplayManager.RefreshTrickplayData(video, replace, cancellationToken).ConfigureAwait(false);
}
2023-02-25 23:59:46 +00:00
else
{
_ = _trickplayManager.RefreshTrickplayData(video, replace, cancellationToken).ConfigureAwait(false);
}
2023-02-22 08:08:35 +00:00
// The core doesn't need to trigger any save operations over this
return ItemUpdateType.None;
}
}
}