mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-03 00:41:55 +01:00
a few more async optimizations
This commit is contained in:
parent
1c5f728ec2
commit
fbf8cc833c
@@ -18,10 +18,7 @@ namespace MediaBrowser.Controller.FFMpeg
|
||||
// Use try catch to avoid having to use File.Exists
|
||||
try
|
||||
{
|
||||
using (FileStream stream = File.OpenRead(outputCachePath))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
||||
}
|
||||
return GetCachedResult(outputCachePath);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
@@ -29,7 +26,12 @@ namespace MediaBrowser.Controller.FFMpeg
|
||||
|
||||
await Run(item.Path, outputCachePath).ConfigureAwait(false);
|
||||
|
||||
using (FileStream stream = File.OpenRead(outputCachePath))
|
||||
return GetCachedResult(item.Path);
|
||||
}
|
||||
|
||||
public static FFProbeResult GetCachedResult(string path)
|
||||
{
|
||||
using (FileStream stream = File.OpenRead(path))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
||||
}
|
||||
@@ -40,10 +42,7 @@ namespace MediaBrowser.Controller.FFMpeg
|
||||
// Use try catch to avoid having to use File.Exists
|
||||
try
|
||||
{
|
||||
using (FileStream stream = File.OpenRead(outputCachePath))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
||||
}
|
||||
return GetCachedResult(outputCachePath);
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
@@ -51,10 +50,7 @@ namespace MediaBrowser.Controller.FFMpeg
|
||||
|
||||
await Run(item.Path, outputCachePath).ConfigureAwait(false);
|
||||
|
||||
using (FileStream stream = File.OpenRead(outputCachePath))
|
||||
{
|
||||
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
||||
}
|
||||
return GetCachedResult(item.Path);
|
||||
}
|
||||
|
||||
private async static Task Run(string input, string output)
|
||||
|
||||
@@ -86,7 +86,7 @@ namespace MediaBrowser.Controller.IO
|
||||
await ProcessPathChanges(paths).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ProcessPathChanges(IEnumerable<string> paths)
|
||||
private Task ProcessPathChanges(IEnumerable<string> paths)
|
||||
{
|
||||
List<BaseItem> itemsToRefresh = new List<BaseItem>();
|
||||
|
||||
@@ -105,11 +105,11 @@ namespace MediaBrowser.Controller.IO
|
||||
return folder != null && folder.IsRoot;
|
||||
}))
|
||||
{
|
||||
await Kernel.Instance.ReloadRoot().ConfigureAwait(false);
|
||||
return Kernel.Instance.ReloadRoot();
|
||||
}
|
||||
else
|
||||
{
|
||||
await Task.WhenAll(itemsToRefresh.Select(i => Kernel.Instance.ReloadItem(i))).ConfigureAwait(false);
|
||||
return Task.WhenAll(itemsToRefresh.Select(i => Kernel.Instance.ReloadItem(i)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,21 +68,17 @@ namespace MediaBrowser.Controller
|
||||
|
||||
public async override Task Init(IProgress<TaskProgress> progress)
|
||||
{
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
await base.Init(progress).ConfigureAwait(false);
|
||||
ExtractFFMpeg();
|
||||
|
||||
await base.Init(progress).ConfigureAwait(false);
|
||||
|
||||
progress.Report(new TaskProgress() { Description = "Loading Users", PercentComplete = 15 });
|
||||
ReloadUsers();
|
||||
progress.Report(new TaskProgress() { Description = "Loading Users", PercentComplete = 15 });
|
||||
ReloadUsers();
|
||||
|
||||
progress.Report(new TaskProgress() { Description = "Extracting FFMpeg", PercentComplete = 20 });
|
||||
await ExtractFFMpeg().ConfigureAwait(false);
|
||||
progress.Report(new TaskProgress() { Description = "Loading Media Library", PercentComplete = 25 });
|
||||
await ReloadRoot().ConfigureAwait(false);
|
||||
|
||||
progress.Report(new TaskProgress() { Description = "Loading Media Library", PercentComplete = 25 });
|
||||
await ReloadRoot().ConfigureAwait(false);
|
||||
|
||||
progress.Report(new TaskProgress() { Description = "Loading Complete", PercentComplete = 100 });
|
||||
}).ConfigureAwait(false);
|
||||
progress.Report(new TaskProgress() { Description = "Loading Complete", PercentComplete = 100 });
|
||||
}
|
||||
|
||||
protected override void OnComposablePartsLoaded()
|
||||
@@ -245,22 +241,21 @@ namespace MediaBrowser.Controller
|
||||
continue;
|
||||
}
|
||||
|
||||
await provider.Fetch(item, args).ConfigureAwait(false);
|
||||
await provider.FetchAsync(item, args).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void ExtractFFMpeg()
|
||||
{
|
||||
ExtractFFMpeg(ApplicationPaths.FFMpegPath);
|
||||
ExtractFFMpeg(ApplicationPaths.FFProbePath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run these during Init.
|
||||
/// Can't run do this on-demand because there will be multiple workers accessing them at once and we'd have to lock them
|
||||
/// </summary>
|
||||
private async Task ExtractFFMpeg()
|
||||
{
|
||||
// FFMpeg.exe
|
||||
await ExtractFFMpeg(ApplicationPaths.FFMpegPath).ConfigureAwait(false);
|
||||
await ExtractFFMpeg(ApplicationPaths.FFProbePath).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task ExtractFFMpeg(string exe)
|
||||
private async void ExtractFFMpeg(string exe)
|
||||
{
|
||||
if (File.Exists(exe))
|
||||
{
|
||||
|
||||
@@ -116,14 +116,17 @@ namespace MediaBrowser.Controller.Library
|
||||
|
||||
if (item != null)
|
||||
{
|
||||
await Kernel.Instance.ExecuteMetadataProviders(item, args);
|
||||
await Kernel.Instance.ExecuteMetadataProviders(item, args).ConfigureAwait(false);
|
||||
|
||||
var folder = item as Folder;
|
||||
|
||||
if (folder != null)
|
||||
if (item.IsFolder)
|
||||
{
|
||||
// If it's a folder look for child entities
|
||||
await AttachChildren(folder, fileSystemChildren).ConfigureAwait(false);
|
||||
(item as Folder).Children = (await Task.WhenAll<BaseItem>(GetChildren(item as Folder, fileSystemChildren)).ConfigureAwait(false))
|
||||
.Where(i => i != null).OrderBy(f =>
|
||||
{
|
||||
return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,27 +136,18 @@ namespace MediaBrowser.Controller.Library
|
||||
/// <summary>
|
||||
/// Finds child BaseItems for a given Folder
|
||||
/// </summary>
|
||||
private async Task AttachChildren(Folder folder, LazyFileInfo[] fileSystemChildren)
|
||||
private Task<BaseItem>[] GetChildren(Folder folder, LazyFileInfo[] fileSystemChildren)
|
||||
{
|
||||
int count = fileSystemChildren.Length;
|
||||
Task<BaseItem>[] tasks = new Task<BaseItem>[fileSystemChildren.Length];
|
||||
|
||||
Task<BaseItem>[] tasks = new Task<BaseItem>[count];
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
for (int i = 0; i < fileSystemChildren.Length; i++)
|
||||
{
|
||||
var child = fileSystemChildren[i];
|
||||
|
||||
tasks[i] = GetItem(child.Path, folder, child.FileInfo);
|
||||
}
|
||||
|
||||
BaseItem[] baseItemChildren = await Task<BaseItem>.WhenAll(tasks).ConfigureAwait(false);
|
||||
|
||||
// Sort them
|
||||
folder.Children = baseItemChildren.Where(i => i != null).OrderBy(f =>
|
||||
{
|
||||
return string.IsNullOrEmpty(f.SortName) ? f.Name : f.SortName;
|
||||
|
||||
});
|
||||
return tasks;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -216,41 +210,41 @@ namespace MediaBrowser.Controller.Library
|
||||
/// <summary>
|
||||
/// Gets a Person
|
||||
/// </summary>
|
||||
public async Task<Person> GetPerson(string name)
|
||||
public Task<Person> GetPerson(string name)
|
||||
{
|
||||
string path = Path.Combine(Kernel.Instance.ApplicationPaths.PeoplePath, name);
|
||||
|
||||
return await GetImagesByNameItem<Person>(path, name).ConfigureAwait(false);
|
||||
return GetImagesByNameItem<Person>(path, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a Studio
|
||||
/// </summary>
|
||||
public async Task<Studio> GetStudio(string name)
|
||||
public Task<Studio> GetStudio(string name)
|
||||
{
|
||||
string path = Path.Combine(Kernel.Instance.ApplicationPaths.StudioPath, name);
|
||||
|
||||
return await GetImagesByNameItem<Studio>(path, name).ConfigureAwait(false);
|
||||
return GetImagesByNameItem<Studio>(path, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a Genre
|
||||
/// </summary>
|
||||
public async Task<Genre> GetGenre(string name)
|
||||
public Task<Genre> GetGenre(string name)
|
||||
{
|
||||
string path = Path.Combine(Kernel.Instance.ApplicationPaths.GenrePath, name);
|
||||
|
||||
return await GetImagesByNameItem<Genre>(path, name).ConfigureAwait(false);
|
||||
return GetImagesByNameItem<Genre>(path, name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a Year
|
||||
/// </summary>
|
||||
public async Task<Year> GetYear(int value)
|
||||
public Task<Year> GetYear(int value)
|
||||
{
|
||||
string path = Path.Combine(Kernel.Instance.ApplicationPaths.YearPath, value.ToString());
|
||||
|
||||
return await GetImagesByNameItem<Year>(path, value.ToString()).ConfigureAwait(false);
|
||||
return GetImagesByNameItem<Year>(path, value.ToString());
|
||||
}
|
||||
|
||||
private ConcurrentDictionary<string, object> ImagesByNameItemCache = new ConcurrentDictionary<string, object>();
|
||||
@@ -258,7 +252,7 @@ namespace MediaBrowser.Controller.Library
|
||||
/// <summary>
|
||||
/// Generically retrieves an IBN item
|
||||
/// </summary>
|
||||
private async Task<T> GetImagesByNameItem<T>(string path, string name)
|
||||
private Task<T> GetImagesByNameItem<T>(string path, string name)
|
||||
where T : BaseEntity, new()
|
||||
{
|
||||
string key = path.ToLower();
|
||||
@@ -266,12 +260,10 @@ namespace MediaBrowser.Controller.Library
|
||||
// Look for it in the cache, if it's not there, create it
|
||||
if (!ImagesByNameItemCache.ContainsKey(key))
|
||||
{
|
||||
T obj = await CreateImagesByNameItem<T>(path, name).ConfigureAwait(false);
|
||||
ImagesByNameItemCache[key] = obj;
|
||||
return obj;
|
||||
ImagesByNameItemCache[key] = CreateImagesByNameItem<T>(path, name);
|
||||
}
|
||||
|
||||
return ImagesByNameItemCache[key] as T;
|
||||
return ImagesByNameItemCache[key] as Task<T>;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -23,16 +23,22 @@ namespace MediaBrowser.Controller.Providers
|
||||
get { return MetadataProviderPriority.First; }
|
||||
}
|
||||
|
||||
public async override Task Fetch(BaseEntity item, ItemResolveEventArgs args)
|
||||
public async override Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
||||
{
|
||||
Audio audio = item as Audio;
|
||||
|
||||
Fetch(audio, await FFProbe.Run(audio, GetFFProbeOutputPath(item)).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
private string GetFFProbeOutputPath(BaseEntity item)
|
||||
{
|
||||
string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeAudioCacheDirectory, item.Id.ToString().Substring(0, 1));
|
||||
|
||||
string outputPath = Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
|
||||
|
||||
FFProbeResult data = await FFProbe.Run(audio, outputPath).ConfigureAwait(false);
|
||||
return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
|
||||
}
|
||||
|
||||
private void Fetch(Audio audio, FFProbeResult data)
|
||||
{
|
||||
MediaStream stream = data.streams.First(s => s.codec_type.Equals("audio", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
string bitrate = null;
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace MediaBrowser.Controller.Providers
|
||||
}
|
||||
}
|
||||
|
||||
public abstract Task Fetch(BaseEntity item, ItemResolveEventArgs args);
|
||||
public abstract Task FetchAsync(BaseEntity item, ItemResolveEventArgs args);
|
||||
|
||||
public abstract MetadataProviderPriority Priority { get; }
|
||||
}
|
||||
|
||||
@@ -19,14 +19,16 @@ namespace MediaBrowser.Controller.Providers
|
||||
get { return MetadataProviderPriority.First; }
|
||||
}
|
||||
|
||||
public async override Task Fetch(BaseEntity item, ItemResolveEventArgs args)
|
||||
public override Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
||||
{
|
||||
var metadataFile = args.GetFileSystemEntryByName("folder.xml");
|
||||
|
||||
if (metadataFile.HasValue)
|
||||
{
|
||||
await Task.Run(() => { new FolderXmlParser().Fetch(item as Folder, metadataFile.Value.Path); }).ConfigureAwait(false);
|
||||
return Task.Run(() => { new FolderXmlParser().Fetch(item as Folder, metadataFile.Value.Path); });
|
||||
}
|
||||
|
||||
return Task.FromResult<object>(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,24 +21,23 @@ namespace MediaBrowser.Controller.Providers
|
||||
get { return MetadataProviderPriority.First; }
|
||||
}
|
||||
|
||||
public override Task Fetch(BaseEntity item, ItemResolveEventArgs args)
|
||||
public override Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
if (args.IsDirectory)
|
||||
{
|
||||
if (args.IsDirectory)
|
||||
{
|
||||
var baseItem = item as BaseItem;
|
||||
var baseItem = item as BaseItem;
|
||||
|
||||
if (baseItem != null)
|
||||
{
|
||||
PopulateImages(baseItem, args);
|
||||
}
|
||||
else
|
||||
{
|
||||
PopulateImages(item, args);
|
||||
}
|
||||
if (baseItem != null)
|
||||
{
|
||||
return Task.Run(() => { PopulateImages(baseItem, args); });
|
||||
}
|
||||
});
|
||||
else
|
||||
{
|
||||
return Task.Run(() => { PopulateImages(item, args); });
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult<object>(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -49,7 +48,7 @@ namespace MediaBrowser.Controller.Providers
|
||||
for (int i = 0; i < args.FileSystemChildren.Length; i++)
|
||||
{
|
||||
var file = args.FileSystemChildren[i];
|
||||
|
||||
|
||||
string filePath = file.Path;
|
||||
|
||||
string ext = Path.GetExtension(filePath);
|
||||
|
||||
@@ -20,10 +20,8 @@ namespace MediaBrowser.Controller.Providers
|
||||
get { return MetadataProviderPriority.First; }
|
||||
}
|
||||
|
||||
public async override Task Fetch(BaseEntity item, ItemResolveEventArgs args)
|
||||
public async override Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
||||
{
|
||||
BaseItem baseItem = item as BaseItem;
|
||||
|
||||
var trailerPath = args.GetFileSystemEntryByName("trailers", true);
|
||||
|
||||
if (trailerPath.HasValue)
|
||||
@@ -36,9 +34,7 @@ namespace MediaBrowser.Controller.Providers
|
||||
{
|
||||
string file = allFiles[i];
|
||||
|
||||
BaseItem child = await Kernel.Instance.ItemController.GetItem(file).ConfigureAwait(false);
|
||||
|
||||
Video video = child as Video;
|
||||
Video video = await Kernel.Instance.ItemController.GetItem(file).ConfigureAwait(false) as Video;
|
||||
|
||||
if (video != null)
|
||||
{
|
||||
@@ -46,7 +42,7 @@ namespace MediaBrowser.Controller.Providers
|
||||
}
|
||||
}
|
||||
|
||||
baseItem.LocalTrailers = localTrailers;
|
||||
(item as BaseItem).LocalTrailers = localTrailers;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace MediaBrowser.Controller.Providers
|
||||
get { return MetadataProviderPriority.Second; }
|
||||
}
|
||||
|
||||
public override async Task Fetch(BaseEntity item, ItemResolveEventArgs args)
|
||||
public override async Task FetchAsync(BaseEntity item, ItemResolveEventArgs args)
|
||||
{
|
||||
Video video = item as Video;
|
||||
|
||||
@@ -39,11 +39,14 @@ namespace MediaBrowser.Controller.Providers
|
||||
return;
|
||||
}
|
||||
|
||||
Fetch(video, await FFProbe.Run(video, GetFFProbeOutputPath(video)).ConfigureAwait(false));
|
||||
}
|
||||
|
||||
private string GetFFProbeOutputPath(Video item)
|
||||
{
|
||||
string outputDirectory = Path.Combine(Kernel.Instance.ApplicationPaths.FFProbeVideoCacheDirectory, item.Id.ToString().Substring(0, 1));
|
||||
|
||||
string outputPath = Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
|
||||
|
||||
FFProbeResult data = await FFProbe.Run(video, outputPath).ConfigureAwait(false);
|
||||
return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".js");
|
||||
}
|
||||
|
||||
private void Fetch(Video video, FFProbeResult data)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml;
|
||||
using MediaBrowser.Model.Entities;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user