2014-02-06 23:52:59 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2014-02-20 16:37:41 +00:00
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using System;
|
2014-02-06 23:52:59 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-06 23:52:59 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses ffmpeg to create video images
|
|
|
|
|
/// </summary>
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public class AudioImageProvider : IDynamicImageProvider, IHasChangeMonitor
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2014-02-06 23:52:59 +00:00
|
|
|
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _locks = new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
private readonly IIsoManager _isoManager;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-02-06 23:52:59 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
|
2014-02-06 23:52:59 +00:00
|
|
|
|
public AudioImageProvider(IIsoManager isoManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
_isoManager = isoManager;
|
2013-05-21 04:04:38 +00:00
|
|
|
|
_mediaEncoder = mediaEncoder;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
_config = config;
|
2014-02-06 23:52:59 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 16:51:53 +00:00
|
|
|
|
/// <summary>
|
2014-02-06 04:39:16 +00:00
|
|
|
|
/// The null mount task result
|
2013-05-22 16:51:53 +00:00
|
|
|
|
/// </summary>
|
2014-02-06 04:39:16 +00:00
|
|
|
|
protected readonly Task<IIsoMount> NullMountTaskResult = Task.FromResult<IIsoMount>(null);
|
2013-05-22 16:51:53 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-06 04:39:16 +00:00
|
|
|
|
/// Mounts the iso if needed.
|
2013-05-22 16:51:53 +00:00
|
|
|
|
/// </summary>
|
2014-02-06 04:39:16 +00:00
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{IIsoMount}.</returns>
|
|
|
|
|
protected Task<IIsoMount> MountIsoIfNeeded(Video item, CancellationToken cancellationToken)
|
2013-05-22 16:51:53 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
if (item.VideoType == VideoType.Iso)
|
2013-05-22 16:51:53 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return _isoManager.Mount(item.Path, cancellationToken);
|
2013-05-22 16:51:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return NullMountTaskResult;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return new List<ImageType> { ImageType.Primary };
|
2013-06-25 01:22:21 +00:00
|
|
|
|
}
|
2013-12-15 16:53:32 +00:00
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public Task<DynamicImageResponse> GetImage(IHasImages item, ImageType type, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2013-05-21 04:04:38 +00:00
|
|
|
|
var audio = (Audio)item;
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
// Can't extract if we didn't find a video stream in the file
|
|
|
|
|
if (!audio.HasEmbeddedImage)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return Task.FromResult(new DynamicImageResponse { HasImage = false });
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
2013-05-21 04:04:38 +00:00
|
|
|
|
|
2014-02-06 23:52:59 +00:00
|
|
|
|
return GetImage((Audio)item, cancellationToken);
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 23:52:59 +00:00
|
|
|
|
public async Task<DynamicImageResponse> GetImage(Audio item, CancellationToken cancellationToken)
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2014-02-06 23:52:59 +00:00
|
|
|
|
var path = GetAudioImagePath(item);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
{
|
2014-02-06 23:54:33 +00:00
|
|
|
|
var semaphore = GetLock(path);
|
2014-02-06 23:52:59 +00:00
|
|
|
|
|
2014-02-06 23:54:33 +00:00
|
|
|
|
// Acquire a lock
|
|
|
|
|
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
2014-02-06 23:52:59 +00:00
|
|
|
|
|
2014-02-06 23:54:33 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Check again in case it was saved while waiting for the lock
|
|
|
|
|
if (!File.Exists(path))
|
2014-02-06 23:52:59 +00:00
|
|
|
|
{
|
2014-02-06 23:54:33 +00:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path));
|
|
|
|
|
|
2014-03-27 19:30:21 +00:00
|
|
|
|
using (var stream = await _mediaEncoder.ExtractAudioImage(item.Path, cancellationToken).ConfigureAwait(false))
|
2014-02-06 23:52:59 +00:00
|
|
|
|
{
|
2014-02-06 23:54:33 +00:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
|
|
|
|
{
|
|
|
|
|
await stream.CopyToAsync(fileStream).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-02-06 23:52:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-06 23:54:33 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
2014-02-06 23:52:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-21 04:04:38 +00:00
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return new DynamicImageResponse
|
2013-04-15 15:10:12 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
HasImage = true,
|
2014-02-06 23:52:59 +00:00
|
|
|
|
Path = path
|
2014-02-06 04:39:16 +00:00
|
|
|
|
};
|
2013-05-21 04:04:38 +00:00
|
|
|
|
}
|
2013-04-15 15:10:12 +00:00
|
|
|
|
|
2014-02-06 23:52:59 +00:00
|
|
|
|
private string GetAudioImagePath(Audio item)
|
|
|
|
|
{
|
|
|
|
|
var album = item.Parent as MusicAlbum;
|
|
|
|
|
|
|
|
|
|
var filename = item.Album ?? string.Empty;
|
|
|
|
|
filename += item.Artists.FirstOrDefault() ?? string.Empty;
|
|
|
|
|
filename += album == null ? item.Id.ToString("N") + "_primary" + item.DateModified.Ticks : album.Id.ToString("N") + album.DateModified.Ticks + "_primary";
|
|
|
|
|
|
|
|
|
|
filename = filename.GetMD5() + ".jpg";
|
|
|
|
|
|
|
|
|
|
var prefix = filename.Substring(0, 1);
|
|
|
|
|
|
|
|
|
|
return Path.Combine(AudioImagesPath, prefix, filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string AudioImagesPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the lock.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
|
|
/// <returns>SemaphoreSlim.</returns>
|
|
|
|
|
private SemaphoreSlim GetLock(string filename)
|
|
|
|
|
{
|
|
|
|
|
return _locks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public string Name
|
2013-12-15 16:53:32 +00:00
|
|
|
|
{
|
2014-02-19 18:50:37 +00:00
|
|
|
|
get { return "Image Extractor"; }
|
2013-12-15 16:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-12-15 16:53:32 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return item.LocationType == LocationType.FileSystem && item is Audio;
|
2013-12-15 16:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
2013-05-21 04:04:38 +00:00
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
return item.DateModified > date;
|
2013-04-15 15:10:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|