Merge pull request #6831 from 1337joe/image-provider-cleanup

This commit is contained in:
Cody Robibero 2021-11-15 15:45:33 -07:00 committed by GitHub
commit 4cfe8fe588
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 141 additions and 182 deletions

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.MediaInfo;
@ -95,10 +96,10 @@ namespace MediaBrowser.Controller.MediaEncoding
/// <param name="mediaSource">Media source information.</param> /// <param name="mediaSource">Media source information.</param>
/// <param name="imageStream">Media stream information.</param> /// <param name="imageStream">Media stream information.</param>
/// <param name="imageStreamIndex">Index of the stream to extract from.</param> /// <param name="imageStreamIndex">Index of the stream to extract from.</param>
/// <param name="outputExtension">The extension of the file to write, including the '.'.</param> /// <param name="targetFormat">The format of the file to write.</param>
/// <param name="cancellationToken">CancellationToken to use for operation.</param> /// <param name="cancellationToken">CancellationToken to use for operation.</param>
/// <returns>Location of video image.</returns> /// <returns>Location of video image.</returns>
Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, string outputExtension, CancellationToken cancellationToken); Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, ImageFormat? targetFormat, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Gets the media info. /// Gets the media info.

View File

@ -19,6 +19,7 @@ using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.MediaEncoding.Probing; using MediaBrowser.MediaEncoding.Probing;
using MediaBrowser.Model.Dlna; using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization; using MediaBrowser.Model.Globalization;
@ -478,17 +479,17 @@ namespace MediaBrowser.MediaEncoding.Encoder
Protocol = MediaProtocol.File Protocol = MediaProtocol.File
}; };
return ExtractImage(path, null, null, imageStreamIndex, mediaSource, true, null, null, ".jpg", cancellationToken); return ExtractImage(path, null, null, imageStreamIndex, mediaSource, true, null, null, ImageFormat.Jpg, cancellationToken);
} }
public Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream videoStream, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken) public Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream videoStream, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken)
{ {
return ExtractImage(inputFile, container, videoStream, null, mediaSource, false, threedFormat, offset, ".jpg", cancellationToken); return ExtractImage(inputFile, container, videoStream, null, mediaSource, false, threedFormat, offset, ImageFormat.Jpg, cancellationToken);
} }
public Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, string outputExtension, CancellationToken cancellationToken) public Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, ImageFormat? targetFormat, CancellationToken cancellationToken)
{ {
return ExtractImage(inputFile, container, imageStream, imageStreamIndex, mediaSource, false, null, null, outputExtension, cancellationToken); return ExtractImage(inputFile, container, imageStream, imageStreamIndex, mediaSource, false, null, null, targetFormat, cancellationToken);
} }
private async Task<string> ExtractImage( private async Task<string> ExtractImage(
@ -500,7 +501,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
bool isAudio, bool isAudio,
Video3DFormat? threedFormat, Video3DFormat? threedFormat,
TimeSpan? offset, TimeSpan? offset,
string outputExtension, ImageFormat? targetFormat,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var inputArgument = GetInputArgument(inputFile, mediaSource); var inputArgument = GetInputArgument(inputFile, mediaSource);
@ -510,7 +511,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
// The failure of HDR extraction usually occurs when using custom ffmpeg that does not contain the zscale filter. // The failure of HDR extraction usually occurs when using custom ffmpeg that does not contain the zscale filter.
try try
{ {
return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, true, outputExtension, cancellationToken).ConfigureAwait(false); return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, true, targetFormat, cancellationToken).ConfigureAwait(false);
} }
catch (ArgumentException) catch (ArgumentException)
{ {
@ -523,7 +524,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
try try
{ {
return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, true, outputExtension, cancellationToken).ConfigureAwait(false); return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, true, targetFormat, cancellationToken).ConfigureAwait(false);
} }
catch (ArgumentException) catch (ArgumentException)
{ {
@ -536,7 +537,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
try try
{ {
return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, false, outputExtension, cancellationToken).ConfigureAwait(false); return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, false, targetFormat, cancellationToken).ConfigureAwait(false);
} }
catch (ArgumentException) catch (ArgumentException)
{ {
@ -548,24 +549,25 @@ namespace MediaBrowser.MediaEncoding.Encoder
} }
} }
return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, false, outputExtension, cancellationToken).ConfigureAwait(false); return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, false, targetFormat, cancellationToken).ConfigureAwait(false);
} }
private async Task<string> ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int? imageStreamIndex, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, bool allowTonemap, string outputExtension, CancellationToken cancellationToken) private async Task<string> ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int? imageStreamIndex, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, bool allowTonemap, ImageFormat? targetFormat, CancellationToken cancellationToken)
{ {
if (string.IsNullOrEmpty(inputPath)) if (string.IsNullOrEmpty(inputPath))
{ {
throw new ArgumentNullException(nameof(inputPath)); throw new ArgumentNullException(nameof(inputPath));
} }
if (string.IsNullOrEmpty(outputExtension)) var outputExtension = targetFormat switch
{ {
outputExtension = ".jpg"; ImageFormat.Bmp => ".bmp",
} ImageFormat.Gif => ".gif",
else if (outputExtension[0] != '.') ImageFormat.Jpg => ".jpg",
{ ImageFormat.Png => ".png",
outputExtension = "." + outputExtension; ImageFormat.Webp => ".webp",
} _ => ".jpg"
};
var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension); var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension);
Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath)); Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));

View File

@ -721,15 +721,13 @@ namespace MediaBrowser.MediaEncoding.Probing
} }
else if (string.Equals(streamInfo.CodecType, "video", StringComparison.OrdinalIgnoreCase)) else if (string.Equals(streamInfo.CodecType, "video", StringComparison.OrdinalIgnoreCase))
{ {
stream.Type = isAudio || string.Equals(stream.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase) || string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase) || string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase)
? MediaStreamType.EmbeddedImage
: MediaStreamType.Video;
stream.AverageFrameRate = GetFrameRate(streamInfo.AverageFrameRate); stream.AverageFrameRate = GetFrameRate(streamInfo.AverageFrameRate);
stream.RealFrameRate = GetFrameRate(streamInfo.RFrameRate); stream.RealFrameRate = GetFrameRate(streamInfo.RFrameRate);
if (isAudio || string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase) || if (isAudio
string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase)) || string.Equals(stream.Codec, "mjpeg", StringComparison.OrdinalIgnoreCase)
|| string.Equals(stream.Codec, "gif", StringComparison.OrdinalIgnoreCase)
|| string.Equals(stream.Codec, "png", StringComparison.OrdinalIgnoreCase))
{ {
stream.Type = MediaStreamType.EmbeddedImage; stream.Type = MediaStreamType.EmbeddedImage;
} }

View File

@ -15,6 +15,7 @@ using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.MediaInfo; using MediaBrowser.Model.MediaInfo;
using MediaBrowser.Model.Net; using MediaBrowser.Model.Net;
using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.MediaInfo namespace MediaBrowser.Providers.MediaInfo
{ {
@ -45,14 +46,17 @@ namespace MediaBrowser.Providers.MediaInfo
}; };
private readonly IMediaEncoder _mediaEncoder; private readonly IMediaEncoder _mediaEncoder;
private readonly ILogger<EmbeddedImageProvider> _logger;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="EmbeddedImageProvider"/> class. /// Initializes a new instance of the <see cref="EmbeddedImageProvider"/> class.
/// </summary> /// </summary>
/// <param name="mediaEncoder">The media encoder for extracting attached/embedded images.</param> /// <param name="mediaEncoder">The media encoder for extracting attached/embedded images.</param>
public EmbeddedImageProvider(IMediaEncoder mediaEncoder) /// <param name="logger">The logger.</param>
public EmbeddedImageProvider(IMediaEncoder mediaEncoder, ILogger<EmbeddedImageProvider> logger)
{ {
_mediaEncoder = mediaEncoder; _mediaEncoder = mediaEncoder;
_logger = logger;
} }
/// <inheritdoc /> /// <inheritdoc />
@ -114,9 +118,15 @@ namespace MediaBrowser.Providers.MediaInfo
ImageType.Primary => _primaryImageFileNames, ImageType.Primary => _primaryImageFileNames,
ImageType.Backdrop => _backdropImageFileNames, ImageType.Backdrop => _backdropImageFileNames,
ImageType.Logo => _logoImageFileNames, ImageType.Logo => _logoImageFileNames,
_ => _primaryImageFileNames _ => Array.Empty<string>()
}; };
if (imageFileNames.Length == 0)
{
_logger.LogWarning("Attempted to load unexpected image type: {Type}", type);
return new DynamicImageResponse { HasImage = false };
}
// Try attachments first // Try attachments first
var attachmentStream = item.GetMediaSources(false) var attachmentStream = item.GetMediaSources(false)
.SelectMany(source => source.MediaAttachments) .SelectMany(source => source.MediaAttachments)
@ -156,13 +166,21 @@ namespace MediaBrowser.Providers.MediaInfo
} }
} }
var format = imageStream.Codec switch
{
"mjpeg" => ImageFormat.Jpg,
"png" => ImageFormat.Png,
"gif" => ImageFormat.Gif,
_ => ImageFormat.Jpg
};
string extractedImagePath = string extractedImagePath =
await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, ".jpg", cancellationToken) await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, imageStream, imageStream.Index, format, cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
return new DynamicImageResponse return new DynamicImageResponse
{ {
Format = ImageFormat.Jpg, Format = format,
HasImage = true, HasImage = true,
Path = extractedImagePath, Path = extractedImagePath,
Protocol = MediaProtocol.File Protocol = MediaProtocol.File
@ -180,10 +198,6 @@ namespace MediaBrowser.Providers.MediaInfo
extension = ".jpg"; extension = ".jpg";
} }
string extractedAttachmentPath =
await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, extension, cancellationToken)
.ConfigureAwait(false);
ImageFormat format = extension switch ImageFormat format = extension switch
{ {
".bmp" => ImageFormat.Bmp, ".bmp" => ImageFormat.Bmp,
@ -194,6 +208,10 @@ namespace MediaBrowser.Providers.MediaInfo
_ => ImageFormat.Jpg _ => ImageFormat.Jpg
}; };
string extractedAttachmentPath =
await _mediaEncoder.ExtractVideoImage(item.Path, item.Container, mediaSource, null, attachmentStream.Index, format, cancellationToken)
.ConfigureAwait(false);
return new DynamicImageResponse return new DynamicImageResponse
{ {
Format = format, Format = format,

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -10,6 +11,7 @@ using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Providers.MediaInfo; using MediaBrowser.Providers.MediaInfo;
using Microsoft.Extensions.Logging.Abstractions;
using Moq; using Moq;
using Xunit; using Xunit;
@ -17,47 +19,25 @@ namespace Jellyfin.Providers.Tests.MediaInfo
{ {
public class EmbeddedImageProviderTests public class EmbeddedImageProviderTests
{ {
private static TheoryData<BaseItem> GetSupportedImages_UnsupportedBaseItems_ReturnsEmpty_TestData()
{
return new ()
{
new AudioBook(),
new BoxSet(),
new Series(),
new Season(),
};
}
[Theory] [Theory]
[MemberData(nameof(GetSupportedImages_UnsupportedBaseItems_ReturnsEmpty_TestData))] [InlineData(typeof(AudioBook))]
public void GetSupportedImages_UnsupportedBaseItems_ReturnsEmpty(BaseItem item) [InlineData(typeof(BoxSet))]
[InlineData(typeof(Series))]
[InlineData(typeof(Season))]
[InlineData(typeof(Episode), ImageType.Primary)]
[InlineData(typeof(Movie), ImageType.Logo, ImageType.Backdrop, ImageType.Primary)]
public void GetSupportedImages_AnyBaseItem_ReturnsExpected(Type type, params ImageType[] expected)
{ {
var embeddedImageProvider = GetEmbeddedImageProvider(null); BaseItem item = (BaseItem)Activator.CreateInstance(type)!;
Assert.Empty(embeddedImageProvider.GetSupportedImages(item)); var embeddedImageProvider = new EmbeddedImageProvider(Mock.Of<IMediaEncoder>(), new NullLogger<EmbeddedImageProvider>());
}
private static TheoryData<BaseItem, IEnumerable<ImageType>> GetSupportedImages_SupportedBaseItems_ReturnsPopulated_TestData()
{
return new TheoryData<BaseItem, IEnumerable<ImageType>>
{
{ new Episode(), new List<ImageType> { ImageType.Primary } },
{ new Movie(), new List<ImageType> { ImageType.Logo, ImageType.Backdrop, ImageType.Primary } },
};
}
[Theory]
[MemberData(nameof(GetSupportedImages_SupportedBaseItems_ReturnsPopulated_TestData))]
public void GetSupportedImages_SupportedBaseItems_ReturnsPopulated(BaseItem item, IEnumerable<ImageType> expected)
{
var embeddedImageProvider = GetEmbeddedImageProvider(null);
var actual = embeddedImageProvider.GetSupportedImages(item); var actual = embeddedImageProvider.GetSupportedImages(item);
Assert.Equal(expected.OrderBy(i => i.ToString()), actual.OrderBy(i => i.ToString())); Assert.Equal(expected.OrderBy(i => i.ToString()), actual.OrderBy(i => i.ToString()));
} }
[Fact] [Fact]
public async void GetImage_InputWithNoStreams_ReturnsNoImage() public async void GetImage_NoStreams_ReturnsNoImage()
{ {
var embeddedImageProvider = GetEmbeddedImageProvider(null); var embeddedImageProvider = new EmbeddedImageProvider(null, new NullLogger<EmbeddedImageProvider>());
var input = GetMovie(new List<MediaAttachment>(), new List<MediaStream>()); var input = GetMovie(new List<MediaAttachment>(), new List<MediaStream>());
@ -66,136 +46,96 @@ namespace Jellyfin.Providers.Tests.MediaInfo
Assert.False(actual.HasImage); Assert.False(actual.HasImage);
} }
[Fact] [Theory]
public async void GetImage_InputWithUnlabeledAttachments_ReturnsNoImage() [InlineData("chapter", null, 1, ImageType.Chapter, null)] // unexpected type, nothing found
[InlineData("unmatched", null, 1, ImageType.Primary, null)] // doesn't default on no match
[InlineData("clearlogo.png", null, 1, ImageType.Logo, ImageFormat.Png)] // extract extension from name
[InlineData("backdrop", "image/bmp", 2, ImageType.Backdrop, ImageFormat.Bmp)] // extract extension from mimetype
[InlineData("poster", null, 3, ImageType.Primary, ImageFormat.Jpg)] // default extension to jpg
public async void GetImage_Attachment_ReturnsCorrectSelection(string filename, string mimetype, int targetIndex, ImageType type, ImageFormat? expectedFormat)
{ {
var embeddedImageProvider = GetEmbeddedImageProvider(null); var attachments = new List<MediaAttachment>();
string pathPrefix = "path";
// add an attachment without a filename - has a list to look through but finds nothing for (int i = 1; i <= targetIndex; i++)
var input = GetMovie( {
new List<MediaAttachment> { new () }, var name = i == targetIndex ? filename : "unmatched";
new List<MediaStream>()); attachments.Add(new ()
{
var actual = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None); FileName = name,
Assert.NotNull(actual); MimeType = mimetype,
Assert.False(actual.HasImage); Index = i
} });
}
[Fact]
public async void GetImage_InputWithLabeledAttachments_ReturnsCorrectSelection()
{
// first tests file extension detection, second uses mimetype, third defaults to jpg
MediaAttachment sampleAttachment1 = new () { FileName = "clearlogo.png", Index = 1 };
MediaAttachment sampleAttachment2 = new () { FileName = "backdrop", MimeType = "image/bmp", Index = 2 };
MediaAttachment sampleAttachment3 = new () { FileName = "poster", Index = 3 };
string targetPath1 = "path1.png";
string targetPath2 = "path2.bmp";
string targetPath3 = "path2.jpg";
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict); var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), 1, ".png", CancellationToken.None)) mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(targetPath1)); .Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + "." + ext));
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), 2, ".bmp", CancellationToken.None)) var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
.Returns(Task.FromResult(targetPath2));
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), 3, ".jpg", CancellationToken.None))
.Returns(Task.FromResult(targetPath3));
var embeddedImageProvider = GetEmbeddedImageProvider(mediaEncoder.Object);
var input = GetMovie( var input = GetMovie(attachments, new List<MediaStream>());
new List<MediaAttachment> { sampleAttachment1, sampleAttachment2, sampleAttachment3 },
new List<MediaStream>());
var actualLogo = await embeddedImageProvider.GetImage(input, ImageType.Logo, CancellationToken.None); var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
Assert.NotNull(actualLogo);
Assert.True(actualLogo.HasImage);
Assert.Equal(targetPath1, actualLogo.Path);
Assert.Equal(ImageFormat.Png, actualLogo.Format);
var actualBackdrop = await embeddedImageProvider.GetImage(input, ImageType.Backdrop, CancellationToken.None);
Assert.NotNull(actualBackdrop);
Assert.True(actualBackdrop.HasImage);
Assert.Equal(targetPath2, actualBackdrop.Path);
Assert.Equal(ImageFormat.Bmp, actualBackdrop.Format);
var actualPrimary = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actualPrimary);
Assert.True(actualPrimary.HasImage);
Assert.Equal(targetPath3, actualPrimary.Path);
Assert.Equal(ImageFormat.Jpg, actualPrimary.Format);
}
[Fact]
public async void GetImage_InputWithUnlabeledEmbeddedImages_BackdropReturnsNoImage()
{
var embeddedImageProvider = GetEmbeddedImageProvider(null);
var input = GetMovie(
new List<MediaAttachment>(),
new List<MediaStream> { new () { Type = MediaStreamType.EmbeddedImage } });
var actual = await embeddedImageProvider.GetImage(input, ImageType.Backdrop, CancellationToken.None);
Assert.NotNull(actual); Assert.NotNull(actual);
Assert.False(actual.HasImage); if (expectedFormat == null)
{
Assert.False(actual.HasImage);
}
else
{
Assert.True(actual.HasImage);
Assert.Equal(pathPrefix + targetIndex + "." + expectedFormat, actual.Path, StringComparer.OrdinalIgnoreCase);
Assert.Equal(expectedFormat, actual.Format);
}
} }
[Fact] [Theory]
public async void GetImage_InputWithUnlabeledEmbeddedImages_PrimaryReturnsImage() [InlineData("chapter", null, 1, ImageType.Chapter, null)] // unexpected type, nothing found
[InlineData(null, null, 1, ImageType.Backdrop, null)] // no label, can only find primary
[InlineData(null, null, 1, ImageType.Primary, ImageFormat.Jpg)] // no label, finds primary
[InlineData("backdrop", null, 2, ImageType.Backdrop, ImageFormat.Jpg)] // uses label to find index 2, not just pulling first stream
[InlineData("cover", null, 2, ImageType.Primary, ImageFormat.Jpg)] // uses label to find index 2, not just pulling first stream
[InlineData(null, "mjpeg", 1, ImageType.Primary, ImageFormat.Jpg)]
[InlineData(null, "png", 1, ImageType.Primary, ImageFormat.Png)]
[InlineData(null, "gif", 1, ImageType.Primary, ImageFormat.Gif)]
public async void GetImage_Embedded_ReturnsCorrectSelection(string label, string? codec, int targetIndex, ImageType type, ImageFormat? expectedFormat)
{ {
MediaStream sampleStream = new () { Type = MediaStreamType.EmbeddedImage, Index = 1 }; var streams = new List<MediaStream>();
string targetPath = "path"; for (int i = 1; i <= targetIndex; i++)
{
var comment = i == targetIndex ? label : "unmatched";
streams.Add(new ()
{
Type = MediaStreamType.EmbeddedImage,
Index = i,
Comment = comment,
Codec = codec
});
}
var pathPrefix = "path";
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict); var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), sampleStream, 1, ".jpg", CancellationToken.None)) mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<int>(), It.IsAny<ImageFormat>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(targetPath)); .Returns<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, stream, index, ext, _) =>
var embeddedImageProvider = GetEmbeddedImageProvider(mediaEncoder.Object); {
Assert.Equal(streams[index - 1], stream);
return Task.FromResult(pathPrefix + index + "." + ext);
});
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
var input = GetMovie( var input = GetMovie(new List<MediaAttachment>(), streams);
new List<MediaAttachment>(),
new List<MediaStream> { sampleStream });
var actual = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None); var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
Assert.NotNull(actual); Assert.NotNull(actual);
Assert.True(actual.HasImage); if (expectedFormat == null)
Assert.Equal(targetPath, actual.Path); {
Assert.Equal(ImageFormat.Jpg, actual.Format); Assert.False(actual.HasImage);
} }
else
[Fact] {
public async void GetImage_InputWithLabeledEmbeddedImages_ReturnsCorrectSelection() Assert.True(actual.HasImage);
{ Assert.Equal(pathPrefix + targetIndex + "." + expectedFormat, actual.Path, StringComparer.OrdinalIgnoreCase);
// primary is second stream to ensure it's not defaulting, backdrop is first Assert.Equal(expectedFormat, actual.Format);
MediaStream sampleStream1 = new () { Type = MediaStreamType.EmbeddedImage, Index = 1, Comment = "backdrop" }; }
MediaStream sampleStream2 = new () { Type = MediaStreamType.EmbeddedImage, Index = 2, Comment = "cover" };
string targetPath1 = "path1.jpg";
string targetPath2 = "path2.jpg";
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), sampleStream1, 1, ".jpg", CancellationToken.None))
.Returns(Task.FromResult(targetPath1));
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), sampleStream2, 2, ".jpg", CancellationToken.None))
.Returns(Task.FromResult(targetPath2));
var embeddedImageProvider = GetEmbeddedImageProvider(mediaEncoder.Object);
var input = GetMovie(
new List<MediaAttachment>(),
new List<MediaStream> { sampleStream1, sampleStream2 });
var actualPrimary = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actualPrimary);
Assert.True(actualPrimary.HasImage);
Assert.Equal(targetPath2, actualPrimary.Path);
Assert.Equal(ImageFormat.Jpg, actualPrimary.Format);
var actualBackdrop = await embeddedImageProvider.GetImage(input, ImageType.Backdrop, CancellationToken.None);
Assert.NotNull(actualBackdrop);
Assert.True(actualBackdrop.HasImage);
Assert.Equal(targetPath1, actualBackdrop.Path);
Assert.Equal(ImageFormat.Jpg, actualBackdrop.Format);
}
private static EmbeddedImageProvider GetEmbeddedImageProvider(IMediaEncoder? mediaEncoder)
{
return new EmbeddedImageProvider(mediaEncoder);
} }
private static Movie GetMovie(List<MediaAttachment> mediaAttachments, List<MediaStream> mediaStreams) private static Movie GetMovie(List<MediaAttachment> mediaAttachments, List<MediaStream> mediaStreams)