Merge pull request #6862 from 1337joe/query-instead-of-filtering

This commit is contained in:
Cody Robibero 2021-11-20 08:50:39 -07:00 committed by GitHub
commit b2b4bd82d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 134 additions and 162 deletions

View File

@ -126,15 +126,6 @@ namespace MediaBrowser.Controller.Entities.Audio
return base.GetBlockUnratedType();
}
public List<MediaStream> GetMediaStreams(MediaStreamType type)
{
return MediaSourceManager.GetMediaStreams(new MediaStreamQuery
{
ItemId = Id,
Type = type
});
}
public SongInfo GetLookupInfo()
{
var info = GetItemLookupInfo<SongInfo>();

View File

@ -1,7 +1,5 @@
#nullable disable
#pragma warning disable CA1002, CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
@ -13,7 +11,9 @@ using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
@ -21,38 +21,49 @@ using MediaBrowser.Model.IO;
namespace MediaBrowser.Providers.MediaInfo
{
/// <summary>
/// Uses ffmpeg to create video images.
/// Uses <see cref="IMediaEncoder"/> to extract embedded images.
/// </summary>
public class AudioImageProvider : IDynamicImageProvider
{
private readonly IMediaSourceManager _mediaSourceManager;
private readonly IMediaEncoder _mediaEncoder;
private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem;
public AudioImageProvider(IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
/// <summary>
/// Initializes a new instance of the <see cref="AudioImageProvider"/> class.
/// </summary>
/// <param name="mediaSourceManager">The media source manager for fetching item streams.</param>
/// <param name="mediaEncoder">The media encoder for extracting embedded images.</param>
/// <param name="config">The server configuration manager for getting image paths.</param>
/// <param name="fileSystem">The filesystem.</param>
public AudioImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, IServerConfigurationManager config, IFileSystem fileSystem)
{
_mediaSourceManager = mediaSourceManager;
_mediaEncoder = mediaEncoder;
_config = config;
_fileSystem = fileSystem;
}
public string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
private string AudioImagesPath => Path.Combine(_config.ApplicationPaths.CachePath, "extracted-audio-images");
/// <inheritdoc />
public string Name => "Image Extractor";
/// <inheritdoc />
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new List<ImageType> { ImageType.Primary };
return new[] { ImageType.Primary };
}
/// <inheritdoc />
public Task<DynamicImageResponse> GetImage(BaseItem item, ImageType type, CancellationToken cancellationToken)
{
var audio = (Audio)item;
var imageStreams =
audio.GetMediaStreams(MediaStreamType.EmbeddedImage)
.Where(i => i.Type == MediaStreamType.EmbeddedImage)
.ToList();
var imageStreams = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery
{
ItemId = item.Id,
Type = MediaStreamType.EmbeddedImage
});
// Can't extract if we didn't find a video stream in the file
if (imageStreams.Count == 0)
@ -63,7 +74,7 @@ namespace MediaBrowser.Providers.MediaInfo
return GetImage((Audio)item, imageStreams, cancellationToken);
}
public async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
private async Task<DynamicImageResponse> GetImage(Audio item, List<MediaStream> imageStreams, CancellationToken cancellationToken)
{
var path = GetAudioImagePath(item);
@ -75,7 +86,7 @@ namespace MediaBrowser.Providers.MediaInfo
imageStreams.FirstOrDefault(i => (i.Comment ?? string.Empty).IndexOf("cover", StringComparison.OrdinalIgnoreCase) != -1) ??
imageStreams.FirstOrDefault();
var imageStreamIndex = imageStream == null ? (int?)null : imageStream.Index;
var imageStreamIndex = imageStream?.Index;
var tempFile = await _mediaEncoder.ExtractAudioImage(item.Path, imageStreamIndex, cancellationToken).ConfigureAwait(false);
@ -127,6 +138,7 @@ namespace MediaBrowser.Providers.MediaInfo
return Path.Join(AudioImagesPath, prefix, filename);
}
/// <inheritdoc />
public bool Supports(BaseItem item)
{
if (item.IsShortcut)

View File

@ -8,7 +8,9 @@ using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
@ -45,16 +47,19 @@ namespace MediaBrowser.Providers.MediaInfo
"logo",
};
private readonly IMediaSourceManager _mediaSourceManager;
private readonly IMediaEncoder _mediaEncoder;
private readonly ILogger<EmbeddedImageProvider> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="EmbeddedImageProvider"/> class.
/// </summary>
/// <param name="mediaSourceManager">The media source manager for fetching item streams and attachments.</param>
/// <param name="mediaEncoder">The media encoder for extracting attached/embedded images.</param>
/// <param name="logger">The logger.</param>
public EmbeddedImageProvider(IMediaEncoder mediaEncoder, ILogger<EmbeddedImageProvider> logger)
public EmbeddedImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, ILogger<EmbeddedImageProvider> logger)
{
_mediaSourceManager = mediaSourceManager;
_mediaEncoder = mediaEncoder;
_logger = logger;
}
@ -128,8 +133,7 @@ namespace MediaBrowser.Providers.MediaInfo
}
// Try attachments first
var attachmentStream = item.GetMediaSources(false)
.SelectMany(source => source.MediaAttachments)
var attachmentStream = _mediaSourceManager.GetMediaAttachments(item.Id)
.FirstOrDefault(attachment => !string.IsNullOrEmpty(attachment.FileName)
&& imageFileNames.Any(name => attachment.FileName.Contains(name, StringComparison.OrdinalIgnoreCase)));
@ -139,7 +143,11 @@ namespace MediaBrowser.Providers.MediaInfo
}
// Fall back to EmbeddedImage streams
var imageStreams = item.GetMediaStreams().FindAll(i => i.Type == MediaStreamType.EmbeddedImage);
var imageStreams = _mediaSourceManager.GetMediaStreams(new MediaStreamQuery
{
ItemId = item.Id,
Type = MediaStreamType.EmbeddedImage
});
if (imageStreams.Count == 0)
{

View File

@ -4,7 +4,9 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
@ -19,16 +21,19 @@ namespace MediaBrowser.Providers.MediaInfo
/// </summary>
public class VideoImageProvider : IDynamicImageProvider, IHasOrder
{
private readonly IMediaSourceManager _mediaSourceManager;
private readonly IMediaEncoder _mediaEncoder;
private readonly ILogger<VideoImageProvider> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="VideoImageProvider"/> class.
/// </summary>
/// <param name="mediaSourceManager">The media source manager for fetching item streams.</param>
/// <param name="mediaEncoder">The media encoder for capturing images.</param>
/// <param name="logger">The logger.</param>
public VideoImageProvider(IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> logger)
public VideoImageProvider(IMediaSourceManager mediaSourceManager, IMediaEncoder mediaEncoder, ILogger<VideoImageProvider> logger)
{
_mediaSourceManager = mediaSourceManager;
_mediaEncoder = mediaEncoder;
_logger = logger;
}
@ -78,12 +83,18 @@ namespace MediaBrowser.Providers.MediaInfo
// If we know the duration, grab it from 10% into the video. Otherwise just 10 seconds in.
// Always use 10 seconds for dvd because our duration could be out of whack
var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks.HasValue &&
item.RunTimeTicks.Value > 0
var imageOffset = item.VideoType != VideoType.Dvd && item.RunTimeTicks > 0
? TimeSpan.FromTicks(item.RunTimeTicks.Value / 10)
: TimeSpan.FromSeconds(10);
var videoStream = item.GetDefaultVideoStream() ?? item.GetMediaStreams().FirstOrDefault(i => i.Type == MediaStreamType.Video);
var query = new MediaStreamQuery { ItemId = item.Id, Index = item.DefaultVideoStreamIndex };
var videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
if (videoStream == null)
{
query.Type = MediaStreamType.Video;
query.Index = null;
videoStream = _mediaSourceManager.GetMediaStreams(query).FirstOrDefault();
}
if (videoStream == null)
{

View File

@ -6,7 +6,9 @@ using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
@ -29,7 +31,7 @@ namespace Jellyfin.Providers.Tests.MediaInfo
public void GetSupportedImages_AnyBaseItem_ReturnsExpected(Type type, params ImageType[] expected)
{
BaseItem item = (BaseItem)Activator.CreateInstance(type)!;
var embeddedImageProvider = new EmbeddedImageProvider(Mock.Of<IMediaEncoder>(), new NullLogger<EmbeddedImageProvider>());
var embeddedImageProvider = new EmbeddedImageProvider(Mock.Of<IMediaSourceManager>(), Mock.Of<IMediaEncoder>(), new NullLogger<EmbeddedImageProvider>());
var actual = embeddedImageProvider.GetSupportedImages(item);
Assert.Equal(expected.OrderBy(i => i.ToString()), actual.OrderBy(i => i.ToString()));
}
@ -37,9 +39,10 @@ namespace Jellyfin.Providers.Tests.MediaInfo
[Fact]
public async void GetImage_NoStreams_ReturnsNoImage()
{
var embeddedImageProvider = new EmbeddedImageProvider(null, new NullLogger<EmbeddedImageProvider>());
var input = new Movie();
var input = GetMovie(new List<MediaAttachment>(), new List<MediaStream>());
var mediaSourceManager = GetMediaSourceManager(input, new List<MediaAttachment>(), new List<MediaStream>());
var embeddedImageProvider = new EmbeddedImageProvider(mediaSourceManager, null, new NullLogger<EmbeddedImageProvider>());
var actual = await embeddedImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actual);
@ -67,12 +70,13 @@ namespace Jellyfin.Providers.Tests.MediaInfo
});
}
var input = new Movie();
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
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<string, string, MediaSourceInfo, MediaStream, int, ImageFormat, CancellationToken>((_, _, _, _, index, ext, _) => Task.FromResult(pathPrefix + index + "." + ext));
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
var input = GetMovie(attachments, new List<MediaStream>());
var mediaSourceManager = GetMediaSourceManager(input, attachments, new List<MediaStream>());
var embeddedImageProvider = new EmbeddedImageProvider(mediaSourceManager, mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
Assert.NotNull(actual);
@ -112,6 +116,8 @@ namespace Jellyfin.Providers.Tests.MediaInfo
});
}
var input = new Movie();
var pathPrefix = "path";
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
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>()))
@ -120,9 +126,8 @@ namespace Jellyfin.Providers.Tests.MediaInfo
Assert.Equal(streams[index - 1], stream);
return Task.FromResult(pathPrefix + index + "." + ext);
});
var embeddedImageProvider = new EmbeddedImageProvider(mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
var input = GetMovie(new List<MediaAttachment>(), streams);
var mediaSourceManager = GetMediaSourceManager(input, new List<MediaAttachment>(), streams);
var embeddedImageProvider = new EmbeddedImageProvider(mediaSourceManager, mediaEncoder.Object, new NullLogger<EmbeddedImageProvider>());
var actual = await embeddedImageProvider.GetImage(input, type, CancellationToken.None);
Assert.NotNull(actual);
@ -138,19 +143,14 @@ namespace Jellyfin.Providers.Tests.MediaInfo
}
}
private static Movie GetMovie(List<MediaAttachment> mediaAttachments, List<MediaStream> mediaStreams)
private static IMediaSourceManager GetMediaSourceManager(BaseItem item, List<MediaAttachment> mediaAttachments, List<MediaStream> mediaStreams)
{
// Mocking IMediaSourceManager GetMediaAttachments and GetMediaStreams instead of mocking Movie works, but
// has concurrency problems between this and VideoImageProviderTests due to BaseItem.MediaSourceManager
// being static
var movie = new Mock<Movie>();
movie.Setup(item => item.GetMediaSources(It.IsAny<bool>()))
.Returns(new List<MediaSourceInfo> { new () { MediaAttachments = mediaAttachments } } );
movie.Setup(item => item.GetMediaStreams())
var mediaSourceManager = new Mock<IMediaSourceManager>(MockBehavior.Strict);
mediaSourceManager.Setup(i => i.GetMediaAttachments(item.Id))
.Returns(mediaAttachments);
mediaSourceManager.Setup(i => i.GetMediaStreams(It.Is<MediaStreamQuery>(q => q.ItemId == item.Id && q.Type == MediaStreamType.EmbeddedImage)))
.Returns(mediaStreams);
return movie.Object;
return mediaSourceManager.Object;
}
}
}

View File

@ -2,8 +2,11 @@ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.MediaEncoding;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Drawing;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
@ -16,64 +19,56 @@ namespace Jellyfin.Providers.Tests.MediaInfo
{
public class VideoImageProviderTests
{
[Fact]
public async void GetImage_InputIsPlaceholder_ReturnsNoImage()
private static TheoryData<Video> GetImage_UnsupportedInput_ReturnsNoImage_TestData()
{
var videoImageProvider = GetVideoImageProvider(null);
var input = new Movie
return new ()
{
IsPlaceHolder = true
new Movie { IsPlaceHolder = true },
new Movie { DefaultVideoStreamIndex = null },
// set a default index but don't put anything there (invalid input, but provider shouldn't break)
new Movie { DefaultVideoStreamIndex = 0 }
};
}
[Theory]
[MemberData(nameof(GetImage_UnsupportedInput_ReturnsNoImage_TestData))]
public async void GetImage_UnsupportedInput_ReturnsNoImage(Video input)
{
var mediaSourceManager = GetMediaSourceManager(input, null, new List<MediaStream>());
var videoImageProvider = new VideoImageProvider(mediaSourceManager, Mock.Of<IMediaEncoder>(), new NullLogger<VideoImageProvider>());
var actual = await videoImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actual);
Assert.False(actual.HasImage);
}
[Fact]
public async void GetImage_NoDefaultVideoStream_ReturnsNoImage()
[Theory]
[InlineData(1, 1)] // default not first stream
[InlineData(5, 0)] // default out of valid range
public async void GetImage_DefaultVideoStreams_ReturnsCorrectStreamImage(int defaultIndex, int targetIndex)
{
var videoImageProvider = GetVideoImageProvider(null);
var input = new Movie { DefaultVideoStreamIndex = defaultIndex };
var input = new Movie
{
DefaultVideoStreamIndex = null
};
var actual = await videoImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actual);
Assert.False(actual.HasImage);
}
[Fact]
public async void GetImage_DefaultSetButNoVideoStream_ReturnsNoImage()
{
var videoImageProvider = GetVideoImageProvider(null);
// set a default index but don't put anything there (invalid input, but provider shouldn't break)
var input = GetMovie(0, null, new List<MediaStream>());
var actual = await videoImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actual);
Assert.False(actual.HasImage);
}
[Fact]
public async void GetImage_DefaultSetMultipleVideoStreams_ReturnsDefaultStreamImage()
{
MediaStream firstStream = new () { Type = MediaStreamType.Video, Index = 0 };
MediaStream targetStream = new () { Type = MediaStreamType.Video, Index = 1 };
string targetPath = "path.jpg";
var mediaStreams = new List<MediaStream>();
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), firstStream, It.IsAny<Video3DFormat?>(), It.IsAny<TimeSpan?>(), CancellationToken.None))
.Returns(Task.FromResult("wrong stream called!"));
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), targetStream, It.IsAny<Video3DFormat?>(), It.IsAny<TimeSpan?>(), CancellationToken.None))
.Returns(Task.FromResult(targetPath));
var videoImageProvider = GetVideoImageProvider(mediaEncoder.Object);
var input = GetMovie(1, targetStream, new List<MediaStream> { firstStream, targetStream } );
for (int i = 0; i <= targetIndex; i++)
{
var mediaStream = new MediaStream { Type = MediaStreamType.Video, Index = i };
mediaStreams.Add(mediaStream);
var path = i == targetIndex ? targetPath : "wrong stream called!";
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), mediaStream, It.IsAny<Video3DFormat?>(), It.IsAny<TimeSpan?>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(path));
}
var defaultStream = defaultIndex < mediaStreams.Count ? mediaStreams[targetIndex] : null;
var mediaSourceManager = GetMediaSourceManager(input, defaultStream, mediaStreams);
var videoImageProvider = new VideoImageProvider(mediaSourceManager, mediaEncoder.Object, new NullLogger<VideoImageProvider>());
var actual = await videoImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actual);
@ -82,31 +77,19 @@ namespace Jellyfin.Providers.Tests.MediaInfo
Assert.Equal(ImageFormat.Jpg, actual.Format);
}
[Fact]
public async void GetImage_InvalidDefaultSingleVideoStream_ReturnsFirstVideoStreamImage()
[Theory]
[InlineData(null, 10)] // default time
[InlineData(500, 50)] // calculated time
public async void GetImage_TimeSpan_SelectsCorrectTime(int? runTimeSeconds, long expectedSeconds)
{
MediaStream targetStream = new () { Type = MediaStreamType.Video, Index = 0 };
string targetPath = "path.jpg";
var input = new Movie
{
DefaultVideoStreamIndex = 0,
RunTimeTicks = runTimeSeconds * TimeSpan.TicksPerSecond
};
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), targetStream, It.IsAny<Video3DFormat?>(), It.IsAny<TimeSpan?>(), CancellationToken.None))
.Returns(Task.FromResult(targetPath));
var videoImageProvider = GetVideoImageProvider(mediaEncoder.Object);
// provide query results for default (empty) and all streams (populated)
var input = GetMovie(5, null, new List<MediaStream> { targetStream });
var actual = await videoImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.NotNull(actual);
Assert.True(actual.HasImage);
Assert.Equal(targetPath, actual.Path);
Assert.Equal(ImageFormat.Jpg, actual.Format);
}
[Fact]
public async void GetImage_NoTimeSpanSet_CallsEncoderWithDefaultTime()
{
MediaStream targetStream = new () { Type = MediaStreamType.Video, Index = 0 };
var mediaSourceManager = GetMediaSourceManager(input, targetStream, new List<MediaStream> { targetStream });
// use a callback to catch the actual value
// provides more information on failure than verifying a specific input was called on the mock
@ -115,62 +98,29 @@ namespace Jellyfin.Providers.Tests.MediaInfo
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<Video3DFormat?>(), It.IsAny<TimeSpan?>(), CancellationToken.None))
.Callback<string, string, MediaSourceInfo, MediaStream, Video3DFormat?, TimeSpan?, CancellationToken>((_, _, _, _, _, timeSpan, _) => actualTimeSpan = timeSpan)
.Returns(Task.FromResult("path"));
var videoImageProvider = GetVideoImageProvider(mediaEncoder.Object);
var input = GetMovie(0, targetStream, new List<MediaStream> { targetStream });
var videoImageProvider = new VideoImageProvider(mediaSourceManager, mediaEncoder.Object, new NullLogger<VideoImageProvider>());
// not testing return, just verifying what gets requested for time span
await videoImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.Equal(TimeSpan.FromSeconds(10), actualTimeSpan);
Assert.Equal(TimeSpan.FromSeconds(expectedSeconds), actualTimeSpan);
}
[Fact]
public async void GetImage_TimeSpanSet_CallsEncoderWithCalculatedTime()
private static IMediaSourceManager GetMediaSourceManager(Video item, MediaStream? defaultStream, List<MediaStream> mediaStreams)
{
MediaStream targetStream = new () { Type = MediaStreamType.Video, Index = 0 };
TimeSpan? actualTimeSpan = null;
var mediaEncoder = new Mock<IMediaEncoder>(MockBehavior.Strict);
mediaEncoder.Setup(encoder => encoder.ExtractVideoImage(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MediaSourceInfo>(), It.IsAny<MediaStream>(), It.IsAny<Video3DFormat?>(), It.IsAny<TimeSpan?>(), CancellationToken.None))
.Callback<string, string, MediaSourceInfo, MediaStream, Video3DFormat?, TimeSpan?, CancellationToken>((_, _, _, _, _, timeSpan, _) => actualTimeSpan = timeSpan)
.Returns(Task.FromResult("path"));
var videoImageProvider = GetVideoImageProvider(mediaEncoder.Object);
var input = GetMovie(0, targetStream, new List<MediaStream> { targetStream });
input.RunTimeTicks = 5000;
// not testing return, just verifying what gets requested for time span
await videoImageProvider.GetImage(input, ImageType.Primary, CancellationToken.None);
Assert.Equal(TimeSpan.FromTicks(500), actualTimeSpan);
}
private static VideoImageProvider GetVideoImageProvider(IMediaEncoder? mediaEncoder)
{
// strict to ensure this isn't accidentally used where a prepared mock is intended
mediaEncoder ??= new Mock<IMediaEncoder>(MockBehavior.Strict).Object;
return new VideoImageProvider(mediaEncoder, new NullLogger<VideoImageProvider>());
}
private static Movie GetMovie(int defaultVideoStreamIndex, MediaStream? defaultStream, List<MediaStream> mediaStreams)
{
// Mocking IMediaSourceManager GetMediaStreams instead of mocking Movie works, but has concurrency problems
// between this and EmbeddedImageProviderTests due to BaseItem.MediaSourceManager being static
var movie = new Mock<Movie>
var defaultStreamList = new List<MediaStream>();
if (defaultStream != null)
{
Object =
{
DefaultVideoStreamIndex = defaultVideoStreamIndex
}
};
defaultStreamList.Add(defaultStream);
}
movie.Setup(item => item.GetDefaultVideoStream())
.Returns(defaultStream!);
movie.Setup(item => item.GetMediaStreams())
var mediaSourceManager = new Mock<IMediaSourceManager>(MockBehavior.Strict);
mediaSourceManager.Setup(i => i.GetMediaStreams(It.Is<MediaStreamQuery>(q => q.ItemId == item.Id && q.Index == item.DefaultVideoStreamIndex)))
.Returns(defaultStreamList);
mediaSourceManager.Setup(i => i.GetMediaStreams(It.Is<MediaStreamQuery>(q => q.ItemId == item.Id && q.Type == MediaStreamType.Video)))
.Returns(mediaStreams);
return movie.Object;
return mediaSourceManager.Object;
}
}
}