Make transcoding logic similar for episodes and movies

This commit is contained in:
Zoe Roux 2024-02-18 17:31:25 +01:00
parent 0a0939fa3d
commit ff5ecb474f
4 changed files with 151 additions and 173 deletions

View File

@ -1,52 +0,0 @@
// 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.Threading.Tasks;
using AspNetCore.Proxy;
using AspNetCore.Proxy.Options;
using Kyoo.Abstractions.Models.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Kyoo.Core.Controllers;
public class Transcoder : Controller
{
public Task Proxy(string route, string path)
{
HttpProxyOptions proxyOptions = HttpProxyOptionsBuilder
.Instance.WithBeforeSend(
(ctx, req) =>
{
req.Headers.Add("X-Path", path);
return Task.CompletedTask;
}
)
.WithHandleFailure(
async (context, exception) =>
{
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
await context.Response.WriteAsJsonAsync(
new RequestError("Service unavailable")
);
}
)
.Build();
return this.HttpProxyAsync($"http://transcoder:7666/{route}", proxyOptions);
}
}

View File

@ -0,0 +1,126 @@
// 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.Threading.Tasks;
using AspNetCore.Proxy;
using AspNetCore.Proxy.Options;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Abstractions.Models.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Kyoo.Core.Api;
public abstract class TranscoderApi<T>(IRepository<T> repository, IThumbnailsManager thumbs)
: CrudThumbsApi<T>(repository, thumbs)
where T : class, IResource, IThumbnails, IQuery
{
private Task _Proxy(string route, string path)
{
HttpProxyOptions proxyOptions = HttpProxyOptionsBuilder
.Instance.WithBeforeSend(
(ctx, req) =>
{
req.Headers.Add("X-Path", path);
return Task.CompletedTask;
}
)
.WithHandleFailure(
async (context, exception) =>
{
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
await context.Response.WriteAsJsonAsync(
new RequestError("Service unavailable")
);
}
)
.Build();
return this.HttpProxyAsync($"http://transcoder:7666/{route}", proxyOptions);
}
protected abstract Task<string> GetPath(Identifier identifier);
/// <summary>
/// Direct stream
/// </summary>
/// <remarks>
/// Retrieve the raw video stream, in the same container as the one on the server. No transcoding or
/// transmuxing is done.
/// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Episode"/>.</param>
/// <returns>The video file of this episode.</returns>
/// <response code="404">No episode with the given ID or slug could be found.</response>
[HttpGet("{identifier:id}/direct")]
[PartialPermission(Kind.Play)]
[ProducesResponseType(StatusCodes.Status206PartialContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetDirectStream(Identifier identifier)
{
await _Proxy("/direct", await GetPath(identifier));
}
/// <summary>
/// Get master playlist
/// </summary>
/// <remarks>
/// Get a master playlist containing all possible video qualities and audios available for this resource.
/// Note that the direct stream is missing (since the direct is not an hls stream) and
/// subtitles/fonts are not included to support more codecs than just webvtt.
/// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Episode"/>.</param>
/// <returns>The master playlist of this episode.</returns>
/// <response code="404">No episode with the given ID or slug could be found.</response>
[HttpGet("{identifier:id}/master.m3u8")]
[PartialPermission(Kind.Play)]
[ProducesResponseType(StatusCodes.Status206PartialContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetMaster(Identifier identifier)
{
await _Proxy("/master.m3u8", await GetPath(identifier));
}
[HttpGet("{identifier:id}/{quality}/index.m3u8")]
[PartialPermission(Kind.Play)]
public async Task GetVideoIndex(Identifier identifier, string quality)
{
await _Proxy($"/{quality}/index.m3u8", await GetPath(identifier));
}
[HttpGet("{identifier:id}/audio/{audio}/index.m3u8")]
[PartialPermission(Kind.Play)]
public async Task GetAudioIndex(Identifier identifier, string audio)
{
await _Proxy($"/audio/{audio}/index.m3u8", await GetPath(identifier));
}
[HttpGet("{identifier:id}/audio/{audio}/{segment}")]
[PartialPermission(Kind.Play)]
public async Task GetAudioSegment(Identifier identifier, string audio, string segment)
{
await _Proxy($"/audio/{audio}/{segment}", await GetPath(identifier));
}
[HttpGet("{identifier:id}/info")]
[PartialPermission(Kind.Play)]
public async Task GetInfo(Identifier identifier)
{
await _Proxy("/info", await GetPath(identifier));
}
}

View File

@ -24,7 +24,6 @@ using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Abstractions.Models.Permissions; using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Abstractions.Models.Utils; using Kyoo.Abstractions.Models.Utils;
using Kyoo.Authentication; using Kyoo.Authentication;
using Kyoo.Core.Controllers;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using static Kyoo.Abstractions.Models.Utils.Constants; using static Kyoo.Abstractions.Models.Utils.Constants;
@ -39,11 +38,8 @@ namespace Kyoo.Core.Api
[ApiController] [ApiController]
[PartialPermission(nameof(Episode))] [PartialPermission(nameof(Episode))]
[ApiDefinition("Episodes", Group = ResourcesGroup)] [ApiDefinition("Episodes", Group = ResourcesGroup)]
public class EpisodeApi( public class EpisodeApi(ILibraryManager libraryManager, IThumbnailsManager thumbnails)
ILibraryManager libraryManager, : TranscoderApi<Episode>(libraryManager.Episodes, thumbnails)
IThumbnailsManager thumbnails,
Transcoder transcoder
) : CrudThumbsApi<Episode>(libraryManager.Episodes, thumbnails)
{ {
/// <summary> /// <summary>
/// Get episode's show /// Get episode's show
@ -191,95 +187,12 @@ namespace Kyoo.Core.Api
await libraryManager.WatchStatus.DeleteEpisodeStatus(id, User.GetIdOrThrow()); await libraryManager.WatchStatus.DeleteEpisodeStatus(id, User.GetIdOrThrow());
} }
/// <summary> protected override async Task<string> GetPath(Identifier identifier)
/// Direct stream
/// </summary>
/// <remarks>
/// Retrieve the raw video stream, in the same container as the one on the server. No transcoding or
/// transmuxing is done.
/// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Episode"/>.</param>
/// <returns>The video file of this episode.</returns>
/// <response code="404">No episode with the given ID or slug could be found.</response>
[HttpGet("{identifier:id}/direct")]
[PartialPermission(Kind.Play)]
[ProducesResponseType(StatusCodes.Status206PartialContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetDirectStream(Identifier identifier)
{ {
string path = await identifier.Match( return await identifier.Match(
async id => (await libraryManager.Episodes.Get(id)).Path, async id => (await Repository.Get(id)).Path,
async slug => (await libraryManager.Episodes.Get(slug)).Path async slug => (await Repository.Get(slug)).Path
); );
await transcoder.Proxy("/direct", path);
}
/// <summary>
/// Get master playlist
/// </summary>
/// <remarks>
/// Get a master playlist containing all possible video qualities and audios available for this resource.
/// Note that the direct stream is missing (since the direct is not an hls stream) and
/// subtitles/fonts are not included to support more codecs than just webvtt.
/// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Episode"/>.</param>
/// <returns>The master playlist of this episode.</returns>
/// <response code="404">No episode with the given ID or slug could be found.</response>
[HttpGet("{identifier:id}/master.m3u8")]
[PartialPermission(Kind.Play)]
[ProducesResponseType(StatusCodes.Status206PartialContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task GetMaster(Identifier identifier)
{
string path = await identifier.Match(
async id => (await libraryManager.Episodes.Get(id)).Path,
async slug => (await libraryManager.Episodes.Get(slug)).Path
);
await transcoder.Proxy("/master.m3u8", path);
}
[HttpGet("{identifier:id}/{quality}/index.m3u8")]
[PartialPermission(Kind.Play)]
public async Task GetVideoIndex(Identifier identifier, string quality)
{
string path = await identifier.Match(
async id => (await libraryManager.Episodes.Get(id)).Path,
async slug => (await libraryManager.Episodes.Get(slug)).Path
);
await transcoder.Proxy($"/{quality}/index.m3u8", path);
}
[HttpGet("{identifier:id}/audio/{audio}/index.m3u8")]
[PartialPermission(Kind.Play)]
public async Task GetAudioIndex(Identifier identifier, string audio)
{
string path = await identifier.Match(
async id => (await libraryManager.Episodes.Get(id)).Path,
async slug => (await libraryManager.Episodes.Get(slug)).Path
);
await transcoder.Proxy($"/audio/{audio}/index.m3u8", path);
}
[HttpGet("{identifier:id}/audio/{audio}/{segment}")]
[PartialPermission(Kind.Play)]
public async Task GetAudioSegment(Identifier identifier, string audio, string segment)
{
string path = await identifier.Match(
async id => (await libraryManager.Episodes.Get(id)).Path,
async slug => (await libraryManager.Episodes.Get(slug)).Path
);
await transcoder.Proxy($"/audio/{audio}/{segment}", path);
}
[HttpGet("{identifier:id}/info")]
[PartialPermission(Kind.Play)]
public async Task GetInfo(Identifier identifier)
{
string path = await identifier.Match(
async id => (await libraryManager.Episodes.Get(id)).Path,
async slug => (await libraryManager.Episodes.Get(slug)).Path
);
await transcoder.Proxy("/info", path);
} }
} }
} }

View File

@ -40,26 +40,9 @@ namespace Kyoo.Core.Api
[ApiController] [ApiController]
[PartialPermission(nameof(Show))] [PartialPermission(nameof(Show))]
[ApiDefinition("Shows", Group = ResourcesGroup)] [ApiDefinition("Shows", Group = ResourcesGroup)]
public class MovieApi : CrudThumbsApi<Movie> public class MovieApi(ILibraryManager libraryManager, IThumbnailsManager thumbs)
: TranscoderApi<Movie>(libraryManager.Movies, thumbs)
{ {
/// <summary>
/// The library manager used to modify or retrieve information in the data store.
/// </summary>
private readonly ILibraryManager _libraryManager;
/// <summary>
/// Create a new <see cref="ShowApi"/>.
/// </summary>
/// <param name="libraryManager">
/// The library manager used to modify or retrieve information about the data store.
/// </param>
/// <param name="thumbs">The thumbnail manager used to retrieve images paths.</param>
public MovieApi(ILibraryManager libraryManager, IThumbnailsManager thumbs)
: base(libraryManager.Movies, thumbs)
{
_libraryManager = libraryManager;
}
// /// <summary> // /// <summary>
// /// Get staff // /// Get staff
// /// </summary> // /// </summary>
@ -113,7 +96,7 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Studio> fields [FromQuery] Include<Studio> fields
) )
{ {
return await _libraryManager.Studios.Get( return await libraryManager.Studios.Get(
identifier.IsContainedIn<Studio, Movie>(x => x.Movies!), identifier.IsContainedIn<Studio, Movie>(x => x.Movies!),
fields fields
); );
@ -147,7 +130,7 @@ namespace Kyoo.Core.Api
[FromQuery] Include<Collection> fields [FromQuery] Include<Collection> fields
) )
{ {
ICollection<Collection> resources = await _libraryManager.Collections.GetAll( ICollection<Collection> resources = await libraryManager.Collections.GetAll(
Filter.And(filter, identifier.IsContainedIn<Collection, Movie>(x => x.Movies)), Filter.And(filter, identifier.IsContainedIn<Collection, Movie>(x => x.Movies)),
sortBy, sortBy,
fields, fields,
@ -156,7 +139,7 @@ namespace Kyoo.Core.Api
if ( if (
!resources.Any() !resources.Any()
&& await _libraryManager.Movies.GetOrDefault(identifier.IsSame<Movie>()) == null && await libraryManager.Movies.GetOrDefault(identifier.IsSame<Movie>()) == null
) )
return NotFound(); return NotFound();
return Page(resources, pagination.Limit); return Page(resources, pagination.Limit);
@ -181,9 +164,9 @@ namespace Kyoo.Core.Api
{ {
Guid id = await identifier.Match( Guid id = await identifier.Match(
id => Task.FromResult(id), id => Task.FromResult(id),
async slug => (await _libraryManager.Movies.Get(slug)).Id async slug => (await libraryManager.Movies.Get(slug)).Id
); );
return await _libraryManager.WatchStatus.GetMovieStatus(id, User.GetIdOrThrow()); return await libraryManager.WatchStatus.GetMovieStatus(id, User.GetIdOrThrow());
} }
/// <summary> /// <summary>
@ -216,9 +199,9 @@ namespace Kyoo.Core.Api
{ {
Guid id = await identifier.Match( Guid id = await identifier.Match(
id => Task.FromResult(id), id => Task.FromResult(id),
async slug => (await _libraryManager.Movies.Get(slug)).Id async slug => (await libraryManager.Movies.Get(slug)).Id
); );
return await _libraryManager.WatchStatus.SetMovieStatus( return await libraryManager.WatchStatus.SetMovieStatus(
id, id,
User.GetIdOrThrow(), User.GetIdOrThrow(),
status, status,
@ -245,9 +228,17 @@ namespace Kyoo.Core.Api
{ {
Guid id = await identifier.Match( Guid id = await identifier.Match(
id => Task.FromResult(id), id => Task.FromResult(id),
async slug => (await _libraryManager.Movies.Get(slug)).Id async slug => (await libraryManager.Movies.Get(slug)).Id
);
await libraryManager.WatchStatus.DeleteMovieStatus(id, User.GetIdOrThrow());
}
protected override Task<string> GetPath(Identifier identifier)
{
return identifier.Match(
async id => (await Repository.Get(id)).Path,
async slug => (await Repository.Get(slug)).Path
); );
await _libraryManager.WatchStatus.DeleteMovieStatus(id, User.GetIdOrThrow());
} }
} }
} }