Enable nullable reference types for MediaBrowser.MediaEncoding.Subtitles

This commit is contained in:
Bond_009 2021-05-20 22:10:19 +02:00
parent c6ca722e57
commit 26d7fc8280
18 changed files with 65 additions and 52 deletions

View file

@ -1,3 +1,4 @@
#nullable disable
#pragma warning disable CS1591
using System;

View file

@ -9,9 +9,9 @@ namespace MediaBrowser.MediaEncoding.BdInfo
{
public class BdInfoDirectoryInfo : IDirectoryInfo
{
private readonly IFileSystem _fileSystem = null;
private readonly IFileSystem _fileSystem;
private readonly FileSystemMetadata _impl = null;
private readonly FileSystemMetadata _impl;
public BdInfoDirectoryInfo(IFileSystem fileSystem, string path)
{
@ -29,7 +29,7 @@ namespace MediaBrowser.MediaEncoding.BdInfo
public string FullName => _impl.FullName;
public IDirectoryInfo Parent
public IDirectoryInfo? Parent
{
get
{

View file

@ -7,7 +7,7 @@ namespace MediaBrowser.MediaEncoding.BdInfo
{
public class BdInfoFileInfo : BDInfo.IO.IFileInfo
{
private FileSystemMetadata _impl = null;
private FileSystemMetadata _impl;
public BdInfoFileInfo(FileSystemMetadata impl)
{

View file

@ -121,11 +121,11 @@ namespace MediaBrowser.MediaEncoding.Encoder
// When changing this, also change the minimum library versions in _ffmpegMinimumLibraryVersions
public static Version MinVersion { get; } = new Version(4, 0);
public static Version MaxVersion { get; } = null;
public static Version? MaxVersion { get; } = null;
public bool ValidateVersion()
{
string output = null;
string output;
try
{
output = GetProcessOutput(_encoderPath, "-version");
@ -133,6 +133,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
catch (Exception ex)
{
_logger.LogError(ex, "Error validating encoder");
return false;
}
if (string.IsNullOrWhiteSpace(output))
@ -207,7 +208,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
/// </summary>
/// <param name="output">The output from "ffmpeg -version".</param>
/// <returns>The FFmpeg version.</returns>
internal Version GetFFmpegVersion(string output)
internal Version? GetFFmpegVersion(string output)
{
// For pre-built binaries the FFmpeg version should be mentioned at the very start of the output
var match = Regex.Match(output, @"^ffmpeg version n?((?:[0-9]+\.?)+)");
@ -275,7 +276,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
private IEnumerable<string> GetHwaccelTypes()
{
string output = null;
string? output = null;
try
{
output = GetProcessOutput(_encoderPath, "-hwaccels");
@ -303,7 +304,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
return false;
}
string output = null;
string output;
try
{
output = GetProcessOutput(_encoderPath, "-h filter=" + filter);
@ -311,6 +312,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
catch (Exception ex)
{
_logger.LogError(ex, "Error detecting the given filter");
return false;
}
if (output.Contains("Filter " + filter, StringComparison.Ordinal))
@ -331,7 +333,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
private IEnumerable<string> GetCodecs(Codec codec)
{
string codecstr = codec == Codec.Encoder ? "encoders" : "decoders";
string output = null;
string output;
try
{
output = GetProcessOutput(_encoderPath, "-" + codecstr);
@ -339,6 +341,7 @@ namespace MediaBrowser.MediaEncoding.Encoder
catch (Exception ex)
{
_logger.LogError(ex, "Error detecting available {Codec}", codecstr);
return Enumerable.Empty<string>();
}
if (string.IsNullOrWhiteSpace(output))

View file

@ -1,3 +1,4 @@
#nullable disable
#pragma warning disable CS1591
using System;

View file

@ -10,6 +10,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>enable</Nullable>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

View file

@ -1,3 +1,5 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Globalization;

View file

@ -1,3 +1,5 @@
#nullable disable
using System.Collections.Generic;
using System.Text.Json.Serialization;

View file

@ -1,3 +1,4 @@
#nullable disable
#pragma warning disable CS1591
using System.Collections.Generic;

View file

@ -1,3 +1,5 @@
#nullable disable
using System.Collections.Generic;
using System.Text.Json.Serialization;

View file

@ -1,3 +1,5 @@
#nullable disable
using System.Collections.Generic;
using System.Text.Json.Serialization;

View file

@ -1,3 +1,4 @@
#nullable disable
#pragma warning disable CS1591
using System;

View file

@ -1,5 +1,3 @@
#nullable enable
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core.SubtitleFormats;

View file

@ -1,4 +1,3 @@
#nullable enable
#pragma warning disable CS1591
namespace MediaBrowser.MediaEncoding.Subtitles

View file

@ -1,5 +1,3 @@
#nullable enable
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core.SubtitleFormats;

View file

@ -1,5 +1,3 @@
#nullable enable
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core.SubtitleFormats;

View file

@ -1,5 +1,3 @@
#nullable enable
using System.Globalization;
using System.IO;
using System.Linq;

View file

@ -3,6 +3,7 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
@ -71,8 +72,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
try
{
var reader = GetReader(inputFormat, true);
var reader = GetReader(inputFormat);
var trackInfo = reader.Parse(stream, cancellationToken);
FilterEvents(trackInfo, startTimeTicks, endTimeTicks, preserveOriginalTimestamps);
@ -139,10 +139,9 @@ namespace MediaBrowser.MediaEncoding.Subtitles
.ConfigureAwait(false);
var inputFormat = subtitle.format;
var writer = TryGetWriter(outputFormat);
// Return the original if we don't have any way of converting it
if (writer == null)
if (!TryGetWriter(outputFormat, out var writer))
{
return subtitle.stream;
}
@ -239,7 +238,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
var currentFormat = (Path.GetExtension(subtitleStream.Path) ?? subtitleStream.Codec)
.TrimStart('.');
if (GetReader(currentFormat, false) == null)
if (TryGetReader(currentFormat, out _))
{
// Convert
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, ".srt");
@ -257,37 +256,41 @@ namespace MediaBrowser.MediaEncoding.Subtitles
return new SubtitleInfo(subtitleStream.Path, mediaSource.Protocol, currentFormat, true);
}
private ISubtitleParser GetReader(string format, bool throwIfMissing)
private bool TryGetReader(string format, [NotNullWhen(true)] out ISubtitleParser? value)
{
if (string.IsNullOrEmpty(format))
{
throw new ArgumentNullException(nameof(format));
}
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
{
return new SrtParser(_logger);
value = new SrtParser(_logger);
return true;
}
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
{
return new SsaParser(_logger);
value = new SsaParser(_logger);
return true;
}
if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
{
return new AssParser(_logger);
value = new AssParser(_logger);
return true;
}
if (throwIfMissing)
{
throw new ArgumentException("Unsupported format: " + format);
}
return null;
value = null;
return false;
}
private ISubtitleWriter TryGetWriter(string format)
private ISubtitleParser GetReader(string format)
{
if (TryGetReader(format, out var reader))
{
return reader;
}
throw new ArgumentException("Unsupported format: " + format);
}
private bool TryGetWriter(string format, [NotNullWhen(true)] out ISubtitleWriter? value)
{
if (string.IsNullOrEmpty(format))
{
@ -296,32 +299,35 @@ namespace MediaBrowser.MediaEncoding.Subtitles
if (string.Equals(format, "json", StringComparison.OrdinalIgnoreCase))
{
return new JsonWriter();
value = new JsonWriter();
return true;
}
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
{
return new SrtWriter();
value = new SrtWriter();
return true;
}
if (string.Equals(format, SubtitleFormat.VTT, StringComparison.OrdinalIgnoreCase))
{
return new VttWriter();
value = new VttWriter();
return true;
}
if (string.Equals(format, SubtitleFormat.TTML, StringComparison.OrdinalIgnoreCase))
{
return new TtmlWriter();
value = new TtmlWriter();
return true;
}
return null;
value = null;
return false;
}
private ISubtitleWriter GetWriter(string format)
{
var writer = TryGetWriter(format);
if (writer != null)
if (TryGetWriter(format, out var writer))
{
return writer;
}
@ -391,7 +397,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
throw new ArgumentNullException(nameof(outputPath));
}
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ({outputPath}) is not valid.", nameof(outputPath)));
var encodingParam = await GetSubtitleFileCharacterSet(inputPath, language, mediaSource.Protocol, cancellationToken).ConfigureAwait(false);
@ -549,7 +555,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
throw new ArgumentNullException(nameof(outputPath));
}
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ({outputPath}) is not valid.", nameof(outputPath)));
var processArgs = string.Format(
CultureInfo.InvariantCulture,
@ -715,7 +721,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
{
using (var stream = await GetStream(path, protocol, cancellationToken).ConfigureAwait(false))
{
var charset = CharsetDetector.DetectFromStream(stream).Detected?.EncodingName;
var charset = CharsetDetector.DetectFromStream(stream).Detected?.EncodingName ?? string.Empty;
// UTF16 is automatically converted to UTF8 by FFmpeg, do not specify a character encoding
if ((path.EndsWith(".ass", StringComparison.Ordinal) || path.EndsWith(".ssa", StringComparison.Ordinal) || path.EndsWith(".srt", StringComparison.Ordinal))
@ -725,7 +731,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
charset = string.Empty;
}
_logger.LogDebug("charset {0} detected for {Path}", charset ?? "null", path);
_logger.LogDebug("charset {0} detected for {Path}", charset, path);
return charset;
}