Skip to content

Commit

Permalink
[CastIt.Server] Improvements to the thumbnail generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Dec 19, 2021
1 parent af2c08b commit e2336e3
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 61 deletions.
35 changes: 35 additions & 0 deletions CastIt.Domain/Models/ThumbnailRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace CastIt.Domain.Models
{
public class ThumbnailRange
{
public Range<long> Range { get; }
public byte[] Image { get; private set; }
public bool HasImage
=> Image != null;
public bool IsBeingGenerated { get; private set; }

public ThumbnailRange(long minimum, long maximum, int index)
{
Range = new Range<long>(minimum, maximum, index);
}

public ThumbnailRange Generating()
{
IsBeingGenerated = true;
return this;
}

public ThumbnailRange Generated()
{
IsBeingGenerated = false;
return this;
}

public ThumbnailRange SetImage(byte[] image)
{
Image = image;
return this;
}
}

}
15 changes: 12 additions & 3 deletions CastIt.FFmpeg/FFmpegService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ public async Task<byte[]> GetThumbnailTile(string mrl, long tentativeSecond, int
HwAccelDeviceType.None
};
CheckFfmpegExePaths();
_logger.LogInformation(
$"{nameof(GetThumbnailTile)}: Trying to generate thumbnail for " +
$"second = {tentativeSecond} and mrl = {mrl} ...");
foreach (var hwAccel in hwAccels)
{
var builder = new FFmpegArgsBuilder();
Expand Down Expand Up @@ -313,7 +316,9 @@ public async Task<byte[]> GetThumbnailTile(string mrl, long tentativeSecond, int
throw new ArgumentOutOfRangeException($"The provided hw accel = {hwAccel} is not supported");
}
var cmd = builder.GetArgs();
_logger.LogInformation($"{nameof(GetThumbnailTile)}: Trying to generate thumbnail tile with hwAccel = {hwAccel} and cmd = {cmd} ...");
_logger.LogInformation(
$"{nameof(GetThumbnailTile)}: Trying to generate thumbnail tile with " +
$"cmd = {cmd} ...");

using var process = new Process
{
Expand All @@ -338,11 +343,15 @@ public async Task<byte[]> GetThumbnailTile(string mrl, long tentativeSecond, int

if (process.ExitCode == 0)
{
_logger.LogInformation($"{nameof(GetThumbnailTile)}: Thumbnail tiles were generated successfully with hwaccel = {hwAccel}");
_logger.LogInformation(
$"{nameof(GetThumbnailTile)}: Thumbnail tiles were generated successfully " +
$"with cmd = {cmd}");
return memStream.ToArray();
}

_logger.LogWarning($"{nameof(GetThumbnailTile)}: Couldn't generate thumbnail tile with hwaccel = {hwAccel}");
_logger.LogWarning(
$"{nameof(GetThumbnailTile)}: Couldn't generate thumbnail tile " +
$"with hwaccel = {hwAccel}");
}
}
catch (Exception e)
Expand Down
7 changes: 7 additions & 0 deletions CastIt.Server/Interfaces/IImageProviderService.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using CastIt.Shared.Models;
using System.Threading.Tasks;

namespace CastIt.Server.Interfaces
{
public interface IImageProviderService
{
byte[] NoImageBytes { get; }

byte[] TransparentImageBytes { get; }

Task Init();

string GetPlayListImageUrl(ServerPlayList playList, ServerFileItem currentPlayedFile);

string GetPlayListImageUrl(ServerPlayList playList);
Expand Down
78 changes: 78 additions & 0 deletions CastIt.Server/Services/ImageProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
using CastIt.Shared.Models;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace CastIt.Server.Services
{
Expand All @@ -16,6 +19,75 @@ public class ImageProviderService : IImageProviderService
private readonly string _imagesBasePath;
private readonly string _noImgFoundPath;

public byte[] NoImageBytes { get; private set; }
public byte[] TransparentImageBytes { get; } = new List<byte>
{
0x89,
0x50,
0x4E,
0x47,
0x0D,
0x0A,
0x1A,
0x0A,
0x00,
0x00,
0x00,
0x0D,
0x49,
0x48,
0x44,
0x52,
0x00,
0x00,
0x00,
0x01,
0x00,
0x00,
0x00,
0x01,
0x08,
0x06,
0x00,
0x00,
0x00,
0x1F,
0x15,
0xC4,
0x89,
0x00,
0x00,
0x00,
0x0A,
0x49,
0x44,
0x41,
0x54,
0x78,
0x9C,
0x63,
0x00,
0x01,
0x00,
0x00,
0x05,
0x00,
0x01,
0x0D,
0x0A,
0x2D,
0xB4,
0x00,
0x00,
0x00,
0x00,
0x49,
0x45,
0x4E,
0x44,
0xAE
}.ToArray();

public ImageProviderService(
IFileService fileService,
IServerService serverService,
Expand All @@ -27,6 +99,12 @@ public ImageProviderService(
_noImgFoundPath = $"{_imagesBasePath}/{NoImage}";
}

public async Task Init()
{
string path = GetNoImagePath();
NoImageBytes = await File.ReadAllBytesAsync(path);
}

public string GetPlayListImageUrl(ServerPlayList playList, ServerFileItem currentPlayedFile)
{
if (playList == null)
Expand Down
Loading

0 comments on commit e2336e3

Please sign in to comment.