Initializing the transcoder only when needed. (Fixes #86)

This commit is contained in:
Zoe Roux 2022-04-16 19:22:30 +02:00
parent 48ae67168b
commit 4644fc522a
No known key found for this signature in database
GPG Key ID: 6AA5AE82CCC0D9DD
4 changed files with 73 additions and 26 deletions

View File

@ -0,0 +1,47 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Runtime.Serialization;
namespace Kyoo.Abstractions.Models.Exceptions
{
/// <summary>
/// An exception thrown when a part of the app has a fatal issue.
/// </summary>
[Serializable]
public class HealthException : Exception
{
/// <summary>
/// Create a new <see cref="HealthException"/> with a custom message.
/// </summary>
/// <param name="message">The message to use.</param>
public HealthException(string message)
: base(message)
{ }
/// <summary>
/// The serialization constructor
/// </summary>
/// <param name="info">Serialization infos</param>
/// <param name="context">The serialization context</param>
protected HealthException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
}

View File

@ -24,6 +24,7 @@ using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Exceptions;
using Kyoo.Core.Models.Options;
using Kyoo.Core.Models.Watch;
using Kyoo.Utils;
@ -177,6 +178,11 @@ namespace Kyoo.Core.Controllers
/// </summary>
private readonly ILogger<Transcoder> _logger;
/// <summary>
/// <see langword="true"/> if the C library has been checked, <see langword="false"/> otherwise.
/// </summary>
private bool _initialized;
/// <summary>
/// Create a new <see cref="Transcoder"/>.
/// </summary>
@ -190,14 +196,29 @@ namespace Kyoo.Core.Controllers
_files = files;
_options = options;
_logger = logger;
}
if (TranscoderAPI.Init() != Marshal.SizeOf<FTrack>())
_logger.LogCritical("The transcoder library could not be initialized correctly");
/// <summary>
/// Check if the C library can be used or if there is an issue with it.
/// </summary>
/// <param name="fastStop">Should the healthcheck be abborted if the transcoder was already initialized?</param>
/// <exception cref="HealthException">If the transcoder is corrupted, this exception in thrown.</exception>
public void CheckHealth(bool fastStop = false)
{
if (fastStop && _initialized)
return;
if (TranscoderAPI.Init() == Marshal.SizeOf<FTrack>())
return;
_initialized = true;
_logger.LogCritical("The transcoder library could not be initialized correctly");
throw new HealthException("The transcoder library is corrupted or invalid.");
}
/// <inheritdoc />
public async Task<ICollection<Track>> ExtractInfos(Episode episode, bool reExtract)
{
CheckHealth(true);
string dir = await _files.GetExtraDirectory(episode);
if (dir == null)
throw new ArgumentException("Invalid path.");
@ -230,6 +251,8 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc />
public IActionResult Transmux(Episode episode)
{
CheckHealth(true);
string folder = Path.Combine(_options.Value.TransmuxPath, episode.Slug);
string manifest = Path.GetFullPath(Path.Combine(folder, episode.Slug + ".m3u8"));

View File

@ -18,7 +18,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
@ -28,10 +27,8 @@ using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Abstractions.Models.Exceptions;
using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Abstractions.Models.Utils;
using Kyoo.Core.Models.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using static Kyoo.Abstractions.Models.Utils.Constants;
namespace Kyoo.Core.Api
@ -53,17 +50,6 @@ namespace Kyoo.Core.Api
/// </summary>
private readonly ILibraryManager _libraryManager;
/// <summary>
/// The file manager used to send images and fonts.
/// </summary>
private readonly IFileSystem _files;
/// <summary>
/// The base URL of Kyoo. This will be used to create links for images and
/// <see cref="Page{T}"/>.
/// </summary>
private readonly Uri _baseURL;
/// <summary>
/// Create a new <see cref="ShowApi"/>.
/// </summary>
@ -72,18 +58,12 @@ namespace Kyoo.Core.Api
/// </param>
/// <param name="files">The file manager used to send images and fonts.</param>
/// <param name="thumbs">The thumbnail manager used to retrieve images paths.</param>
/// <param name="options">
/// Options used to retrieve the base URL of Kyoo.
/// </param>
public ShowApi(ILibraryManager libraryManager,
IFileSystem files,
IThumbnailsManager thumbs,
IOptions<BasicOptions> options)
IThumbnailsManager thumbs)
: base(libraryManager.ShowRepository, files, thumbs)
{
_libraryManager = libraryManager;
_files = files;
_baseURL = options.Value.PublicUrl;
}
/// <summary>

View File

@ -16,15 +16,12 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Core.Controllers;
using Kyoo.Core.Models.Options;
using Kyoo.Utils;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;