using MediaBrowser.Common.MediaInfo;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Providers.MediaInfo
{
///
/// Uses ffmpeg to create video images
///
public class FFMpegAudioImageProvider : BaseFFMpegProvider
{
public FFMpegAudioImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IMediaEncoder mediaEncoder)
: base(logManager, configurationManager, mediaEncoder)
{
}
///
/// The true task result
///
protected static readonly Task TrueTaskResult = Task.FromResult(true);
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Last; }
}
///
/// The _locks
///
private readonly ConcurrentDictionary _locks = new ConcurrentDictionary();
///
/// Gets the lock.
///
/// The filename.
/// System.Object.
private SemaphoreSlim GetLock(string filename)
{
return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
}
///
/// Needses the refresh internal.
///
/// The item.
/// The provider info.
/// true if XXXX, false otherwise
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (!string.IsNullOrEmpty(item.PrimaryImagePath))
{
return false;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
///
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
///
/// The item.
/// if set to true [force].
/// The cancellation token.
/// Task{System.Boolean}.
public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
var success = ProviderRefreshStatus.Success;
if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
{
var album = item.ResolveArgs.Parent as MusicAlbum;
if (album != null)
{
// First try to use the parent's image
item.PrimaryImagePath = item.ResolveArgs.Parent.PrimaryImagePath;
}
// If it's still empty see if there's an embedded image
if (force || string.IsNullOrEmpty(item.PrimaryImagePath))
{
var audio = (Audio)item;
if (audio.MediaStreams != null && audio.MediaStreams.Any(s => s.Type == MediaStreamType.Video))
{
var filename = album != null && string.IsNullOrEmpty(audio.Album + album.DateModified.Ticks) ? (audio.Id.ToString() + audio.DateModified.Ticks) : audio.Album;
var path = Kernel.Instance.FFMpegManager.AudioImageCache.GetResourcePath(filename + "_primary", ".jpg");
if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
{
var semaphore = GetLock(path);
// Acquire a lock
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
// Check again
if (!Kernel.Instance.FFMpegManager.AudioImageCache.ContainsFilePath(path))
{
try
{
await MediaEncoder.ExtractImage(new[] { audio.Path }, InputType.AudioFile, null, path, cancellationToken).ConfigureAwait(false);
}
catch
{
success = ProviderRefreshStatus.Failure;
}
finally
{
semaphore.Release();
}
}
else
{
semaphore.Release();
}
}
if (success == ProviderRefreshStatus.Success)
{
// Image is already in the cache
audio.PrimaryImagePath = path;
}
}
}
}
SetLastRefreshed(item, DateTime.UtcNow, success);
return true;
}
}
}