Skip to content

Commit

Permalink
Added a base generator project
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Dec 12, 2021
1 parent 1e96152 commit 595224d
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 88 deletions.
102 changes: 102 additions & 0 deletions CastIt.GoogleCast.Generator/BaseMediaRequestGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using CastIt.Domain.Enums;
using CastIt.FFmpeg;
using CastIt.GoogleCast.Models.Media;
using CastIt.GoogleCast.Models.Play;
using CastIt.GoogleCast.Shared.Enums;
using CastIt.Server.Shared;
using CastIt.Shared.FilePaths;
using CastIt.Shared.Models;
using Microsoft.Extensions.Logging;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;

namespace CastIt.GoogleCast.Generator
{
public abstract class BaseMediaRequestGenerator
{
public const int SubTitleDefaultTrackId = 1;

protected readonly ILogger Logger;
protected readonly IFFmpegService FFmpeg;
protected readonly IBaseServerService Server;
protected readonly IFileService FileService;

protected BaseMediaRequestGenerator(
ILogger logger,
IFFmpegService fFmpeg,
IBaseServerService server,
IFileService fileService)
{
Logger = logger;
FFmpeg = fFmpeg;
Server = server;
FileService = fileService;
}

protected void SetStreams(ServerFileItem file, bool fileOptionsChanged)
{
Logger.LogInformation($"{nameof(SetStreams)}: Setting the video and audio streams...");
if (fileOptionsChanged)
return;
Logger.LogInformation($"{nameof(SetStreams)}: Setting the video and audio streams...");
file
.CleanAllStreams()
.SetVideoStreams()
.SetAudioStreams();
}

protected async Task SetSubtitlesIfAny(
ServerFileItem file,
ServerAppSettings settings,
PlayMediaRequest request,
CancellationToken cancellationToken = default)
{
Logger.LogInformation($"{nameof(SetSubtitlesIfAny)}: Setting subtitles if needed...");
bool useSubTitleStream = request.SubtitleStreamIndex >= 0;
string selectedSubtitlePath = file.CurrentFileSubTitles.Find(f => f.IsSelected)?.Path;
if (!useSubTitleStream && string.IsNullOrEmpty(selectedSubtitlePath))
{
Logger.LogInformation($"{nameof(SetSubtitlesIfAny)}: No subtitles were specified");
return;
}

Logger.LogInformation($"{nameof(SetSubtitlesIfAny)}: Subtitles were specified, generating a compatible one...");
string subtitleLocation = useSubTitleStream ? request.MediaInfo.ContentId : selectedSubtitlePath;
string subTitleFilePath = FileService.GetSubTitleFilePath();
await FFmpeg.GenerateSubTitles(
subtitleLocation,
subTitleFilePath,
request.SeekSeconds,
useSubTitleStream ? request.SubtitleStreamIndex : 0,
settings.SubtitleDelayInSeconds,
cancellationToken);

var subtitle = new Track
{
TrackId = SubTitleDefaultTrackId,
SubType = TextTrackType.Subtitles,
Type = TrackType.Text,
Name = "English",
Language = "en-US",
TrackContentId = Server.GetSubTitleUrl(),
};
request.MediaInfo.Tracks.Add(subtitle);
request.MediaInfo.TextTrackStyle = new TextTrackStyle
{
ForegroundColor = settings.CurrentSubtitleFgColor == SubtitleFgColorType.White
? Color.WhiteSmoke
: Color.Yellow,
BackgroundColor = Color.Transparent,
EdgeColor = Color.Black,
FontScale = (int)settings.CurrentSubtitleFontScale / 100,
WindowType = TextTrackWindowType.Normal,
EdgeType = TextTrackEdgeType.Raised,
FontStyle = settings.CurrentSubtitleFontStyle,
FontGenericFamily = settings.CurrentSubtitleFontFamily
};
request.ActiveTrackIds.Add(SubTitleDefaultTrackId);
Logger.LogInformation($"{nameof(SetSubtitlesIfAny)}: Subtitles were generated");
}
}
}
14 changes: 14 additions & 0 deletions CastIt.GoogleCast.Generator/CastIt.GoogleCast.Generator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\CastIt.FFmpeg\CastIt.FFmpeg.csproj" />
<ProjectReference Include="..\CastIt.GoogleCast\CastIt.GoogleCast.csproj" />
<ProjectReference Include="..\CastIt.Server.Shared\CastIt.Server.Shared.csproj" />
<ProjectReference Include="..\CastIt.Shared\CastIt.Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<ProjectReference Include="..\CastIt.FFmpeg\CastIt.FFmpeg.csproj" />
<ProjectReference Include="..\CastIt.GoogleCast.Generator\CastIt.GoogleCast.Generator.csproj" />
<ProjectReference Include="..\CastIt.GoogleCast\CastIt.GoogleCast.csproj" />
<ProjectReference Include="..\CastIt.Server.Shared\CastIt.Server.Shared.csproj" />
<ProjectReference Include="..\CastIt.Shared\CastIt.Shared.csproj" />
Expand Down
57 changes: 25 additions & 32 deletions CastIt.GoogleCast.LocalFile/LocalFileMediaRequestGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using CastIt.Domain.Exceptions;
using CastIt.Domain.Extensions;
using CastIt.FFmpeg;
using CastIt.GoogleCast.Generator;
using CastIt.GoogleCast.Interfaces;
using CastIt.GoogleCast.Models.Media;
using CastIt.GoogleCast.Models.Play;
using CastIt.GoogleCast.Shared.Enums;
using CastIt.Server.Shared;
using CastIt.Shared.Extensions;
using CastIt.Shared.FilePaths;
using CastIt.Shared.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
Expand All @@ -18,23 +20,18 @@

namespace CastIt.GoogleCast.LocalFile
{
public class LocalFileMediaRequestGenerator : IPlayMediaRequestGenerator
public class LocalFileMediaRequestGenerator : BaseMediaRequestGenerator, IPlayMediaRequestGenerator
{
private readonly ILogger<LocalFileMediaRequestGenerator> _logger;
private readonly IBaseServerService _server;
private readonly IFFmpegService _ffmpeg;

public string Identifier
=> nameof(LocalFileMediaRequestGenerator);

public LocalFileMediaRequestGenerator(
ILogger<LocalFileMediaRequestGenerator> logger,
IBaseServerService server,
IFFmpegService ffmpeg)
IFFmpegService ffmpeg,
IFileService fileService)
: base(logger, ffmpeg, server, fileService)
{
_logger = logger;
_server = server;
_ffmpeg = ffmpeg;
}

public Task<bool> CanHandleRequest(
Expand All @@ -53,38 +50,30 @@ public async Task<PlayMediaRequest> BuildRequest(
bool fileOptionsChanged,
CancellationToken cancellationToken = default)
{
_logger.LogInformation($"{nameof(BuildRequest)}: Building request...");
Logger.LogInformation($"{nameof(BuildRequest)}: Building request...");

var fileInfo = await _ffmpeg.GetFileInfo(file.Path, cancellationToken);
var fileInfo = await FFmpeg.GetFileInfo(file.Path, cancellationToken);
if (fileInfo == null)
{
var msg = $"Couldn't retrieve the file info for file = {file.Filename}";
_logger.LogWarning($"{nameof(BuildRequest)}: {msg}");
Logger.LogWarning($"{nameof(BuildRequest)}: {msg}");
throw new ErrorLoadingFileException(msg);
}

file.UpdateFileInfo(fileInfo);
//TODO: MAYBE MOVE THIS TO A BASE CLASS ?
if (!fileOptionsChanged)
{
_logger.LogInformation($"{nameof(BuildRequest)}: Setting the video and audio streams...");
file
.CleanAllStreams()
.SetVideoStreams()
.SetAudioStreams();
}
SetStreams(file, fileOptionsChanged);

bool videoNeedsTranscode = file.Type.IsLocalVideo() && _ffmpeg.VideoNeedsTranscode(
bool videoNeedsTranscode = file.Type.IsLocalVideo() && FFmpeg.VideoNeedsTranscode(
file.CurrentFileVideoStreamIndex, settings.ForceVideoTranscode,
settings.VideoScale, file.FileInfo);
bool audioNeedsTranscode = _ffmpeg.AudioNeedsTranscode(
bool audioNeedsTranscode = FFmpeg.AudioNeedsTranscode(
file.CurrentFileAudioStreamIndex, settings.ForceAudioTranscode,
file.FileInfo, file.Type.IsLocalMusic());
HwAccelDeviceType hwAccelToUse = file.Type.IsLocalVideo()
? _ffmpeg.GetHwAccelToUse(file.CurrentFileVideoStreamIndex, file.FileInfo, settings.EnableHardwareAcceleration)
? FFmpeg.GetHwAccelToUse(file.CurrentFileVideoStreamIndex, file.FileInfo, settings.EnableHardwareAcceleration)
: HwAccelDeviceType.None;
string thumbnail = _ffmpeg.GetThumbnail(file.Path);
string thumbnailUrl = _server.GetChromeCastPreviewUrl(thumbnail);
string thumbnail = FFmpeg.GetThumbnail(file.Path);
string thumbnailUrl = Server.GetChromeCastPreviewUrl(thumbnail);
var options = new LocalFileGeneratePlayMediaRequest
{
FileInfo = file.FileInfo,
Expand All @@ -100,12 +89,16 @@ public async Task<PlayMediaRequest> BuildRequest(
VideoScale = settings.VideoScale,
Seconds = seekSeconds,
};
return BuildRequest(options);
var request = BuildRequest(options);
await SetSubtitlesIfAny(file, settings, request, cancellationToken);

Logger.LogInformation($"{nameof(BuildRequest)}: Request was built");
return request;
}

private PlayMediaRequest BuildRequest(LocalFileGeneratePlayMediaRequest options)
{
_logger.LogInformation($"{nameof(BuildRequest)}: File is a local one, generating metadata...");
Logger.LogInformation($"{nameof(BuildRequest)}: File is a local one, generating metadata...");
string title = Path.GetFileName(options.Mrl);
var media = new MediaInformation
{
Expand All @@ -114,7 +107,7 @@ private PlayMediaRequest BuildRequest(LocalFileGeneratePlayMediaRequest options)
Title = title,
},
//You have to set the content type before hand, with that, the album art of a music file will be shown
ContentType = _server.GetOutputMimeType(options.Mrl),
ContentType = Server.GetOutputMimeType(options.Mrl),
Duration = options.FileInfo.Format.Duration
};

Expand All @@ -132,7 +125,7 @@ private PlayMediaRequest BuildRequest(LocalFileGeneratePlayMediaRequest options)
};
}

_logger.LogInformation($"{nameof(BuildRequest)}: Retrieving img url to use...");
Logger.LogInformation($"{nameof(BuildRequest)}: Retrieving img url to use...");
if (!string.IsNullOrEmpty(options.ThumbnailUrl))
{
media.Metadata.Images.Add(new GoogleCast.Models.Image
Expand All @@ -158,7 +151,7 @@ private PlayMediaRequest BuildRequest(LocalFileGeneratePlayMediaRequest options)
VideoWidthAndHeight = options.FileInfo.Videos
.Find(f => f.Index == options.VideoStreamIndex)?
.WidthAndHeightText,
ContentType = _server.GetOutputMimeType(options.Mrl)
ContentType = Server.GetOutputMimeType(options.Mrl)
};

var request = new PlayMediaRequest
Expand All @@ -174,7 +167,7 @@ private PlayMediaRequest BuildRequest(LocalFileGeneratePlayMediaRequest options)
OriginalMrl = options.Mrl
};

_logger.LogInformation($"{nameof(BuildRequest)}: Metadata was successfully created");
Logger.LogInformation($"{nameof(BuildRequest)}: Metadata was successfully created");
return request;
}
}
Expand Down
1 change: 1 addition & 0 deletions CastIt.GoogleCast.Youtube/CastIt.GoogleCast.Youtube.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<ProjectReference Include="..\CastIt.FFmpeg\CastIt.FFmpeg.csproj" />
<ProjectReference Include="..\CastIt.GoogleCast.Generator\CastIt.GoogleCast.Generator.csproj" />
<ProjectReference Include="..\CastIt.GoogleCast\CastIt.GoogleCast.csproj" />
<ProjectReference Include="..\CastIt.Server.Shared\CastIt.Server.Shared.csproj" />
<ProjectReference Include="..\CastIt.Youtube\CastIt.Youtube.csproj" />
Expand Down
Loading

0 comments on commit 595224d

Please sign in to comment.