From 98eda1c5485195da455c3a7d4d9c6b8fd677226c Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 18 Mar 2022 15:48:32 -0500 Subject: add taglib --- src/slskd/slskd.csproj | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 70c689b8..251412e9 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -76,6 +76,7 @@ + -- cgit v1.2.3 From 0f12d85c5babd68d8b3702279cfcb7255978a9a9 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 18 Mar 2022 15:48:43 -0500 Subject: move Share --- src/slskd/Shares/Domain/Share.cs | 75 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/slskd/Shares/Domain/Share.cs (limited to 'src') diff --git a/src/slskd/Shares/Domain/Share.cs b/src/slskd/Shares/Domain/Share.cs new file mode 100644 index 00000000..890c8565 --- /dev/null +++ b/src/slskd/Shares/Domain/Share.cs @@ -0,0 +1,75 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Shares +{ + using System.Linq; + using System.Text.RegularExpressions; + + /// + /// A file share. + /// + public sealed class Share + { + /// + /// Initializes a new instance of the class. + /// + /// + public Share(string share) + { + Raw = share; + IsExcluded = share.StartsWith('-') || share.StartsWith('!'); + + if (IsExcluded) + { + share = share[1..]; + } + + // test to see whether an alias has been specified + var matches = Regex.Matches(share, @"^\[(.*)\](.*)$"); + + if (matches.Any()) + { + // split the alias from the path, and validate the alias + var groups = matches[0].Groups; + Alias = groups[1].Value; + LocalPath = groups[2].Value; + } + else + { + Alias = share.Split(new[] { '/', '\\' }).Last(); + LocalPath = share; + } + + var parent = System.IO.Directory.GetParent(LocalPath).FullName.TrimEnd('/', '\\'); + + Mask = Compute.MaskHash(parent); + + var maskedPath = LocalPath.ReplaceFirst(parent, Mask); + + var aliasedSegment = LocalPath[(parent.Length + 1)..]; + RemotePath = maskedPath.ReplaceFirst(aliasedSegment, Alias); + } + + public string Alias { get; init; } + public bool IsExcluded { get; init; } + public string LocalPath { get; init; } + public string Mask { get; init; } + public string Raw { get; init; } + public string RemotePath { get; init; } + } +} -- cgit v1.2.3 From cac0d9ea1826e226e04eab5bed22f8571c8bec7a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 18 Mar 2022 15:49:00 -0500 Subject: add logic to create File with metadata --- src/slskd/Shares/Extensions.cs | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/slskd/Shares/Extensions.cs (limited to 'src') diff --git a/src/slskd/Shares/Extensions.cs b/src/slskd/Shares/Extensions.cs new file mode 100644 index 00000000..aa1936b5 --- /dev/null +++ b/src/slskd/Shares/Extensions.cs @@ -0,0 +1,66 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +using System.IO; + +namespace slskd.Shares +{ + using System.Collections.Generic; + using System.Linq; + using Soulseek; + + public static class Extensions + { + private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" }; + private static readonly string[] AudioExtensions = { "aa", "aax", "aac", "aiff", "ape", "dsf", "flac", "m4a", "m4b", "m4p", "mp3", "mpc", "mpp", "ogg", "oga", "wav", "wma", "wv", "webm" }; + private static readonly HashSet SupportedExtensions = AudioExtensions.Concat(VideoExtensions).ToHashSet(); + + public static File ToSoulseekFile(this string filename, Share share) + { + var code = 1; + var maskedFilename = filename.ReplaceFirst(share.LocalPath, share.RemotePath); + var size = new FileInfo(filename).Length; + var extension = Path.GetExtension(filename)[1..].ToLowerInvariant(); + List attributeList = default; + + if (SupportedExtensions.Contains(extension)) + { + attributeList = new List(); + + try + { + var file = TagLib.File.Create(filename); + + attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); + attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); + + if (file.Properties.BitsPerSample > 0) + { + attributeList.Add(new FileAttribute(FileAttributeType.SampleRate, file.Properties.AudioSampleRate)); + attributeList.Add(new FileAttribute(FileAttributeType.BitDepth, file.Properties.BitsPerSample)); + } + } + catch + { + // unsupported or corrupt file, noop + } + } + + return new File(code, maskedFilename, size, extension, attributeList); + } + } +} -- cgit v1.2.3 From ca97a7f48b1a1782a4fb38d9ee86c41a04fd58cc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 18 Mar 2022 15:49:10 -0500 Subject: gather metadata when processing shares --- src/slskd/Shares/SharedFileCache.cs | 48 +------------------------------------ 1 file changed, 1 insertion(+), 47 deletions(-) (limited to 'src') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index ad2468da..f13eaad4 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -249,7 +249,7 @@ namespace slskd.Shares try { var newFiles = System.IO.Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly) - .Select(f => new File(1, f.ReplaceFirst(share.LocalPath, share.RemotePath), new FileInfo(f).Length, Path.GetExtension(f))) + .Select(f => f.ToSoulseekFile(share)) .ToDictionary(f => f.Filename, f => f); // merge the new dictionary with the rest this will overwrite any duplicate keys, but keys are the fully @@ -462,51 +462,5 @@ namespace slskd.Shares MaskedFiles = new Dictionary(); State.SetValue(state => state with { Directories = 0, Files = 0 }); } - - private sealed class Share - { - public Share(string share) - { - Raw = share; - IsExcluded = share.StartsWith('-') || share.StartsWith('!'); - - if (IsExcluded) - { - share = share[1..]; - } - - // test to see whether an alias has been specified - var matches = Regex.Matches(share, @"^\[(.*)\](.*)$"); - - if (matches.Any()) - { - // split the alias from the path, and validate the alias - var groups = matches[0].Groups; - Alias = groups[1].Value; - LocalPath = groups[2].Value; - } - else - { - Alias = share.Split(new[] { '/', '\\' }).Last(); - LocalPath = share; - } - - var parent = System.IO.Directory.GetParent(LocalPath).FullName.TrimEnd('/', '\\'); - - Mask = Compute.MaskHash(parent); - - var maskedPath = LocalPath.ReplaceFirst(parent, Mask); - - var aliasedSegment = LocalPath[(parent.Length + 1)..]; - RemotePath = maskedPath.ReplaceFirst(aliasedSegment, Alias); - } - - public string Alias { get; init; } - public bool IsExcluded { get; init; } - public string LocalPath { get; init; } - public string Mask { get; init; } - public string Raw { get; init; } - public string RemotePath { get; init; } - } } } \ No newline at end of file -- cgit v1.2.3 From 1deff8981e2f8dd8edb077501a6fbc106ff17988 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Mar 2022 20:57:34 -0500 Subject: dispose TagLib files --- src/slskd/Shares/Extensions.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Shares/Extensions.cs b/src/slskd/Shares/Extensions.cs index aa1936b5..01e7e000 100644 --- a/src/slskd/Shares/Extensions.cs +++ b/src/slskd/Shares/Extensions.cs @@ -40,10 +40,11 @@ namespace slskd.Shares if (SupportedExtensions.Contains(extension)) { attributeList = new List(); + TagLib.File file = default; try { - var file = TagLib.File.Create(filename); + file = TagLib.File.Create(filename, TagLib.ReadStyle.Average); attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); @@ -58,6 +59,10 @@ namespace slskd.Shares { // unsupported or corrupt file, noop } + finally + { + file?.Dispose(); + } } return new File(code, maskedFilename, size, extension, attributeList); -- cgit v1.2.3 From ef73185e540d66a650164909993e84c6e3396aa5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Mar 2022 21:06:15 -0500 Subject: move extension method to SoulseekFileFactory --- src/slskd/Shares/Extensions.cs | 71 ------------------------------ src/slskd/Shares/SharedFileCache.cs | 10 +++-- src/slskd/Shares/SoulseekFileFactory.cs | 76 +++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 74 deletions(-) delete mode 100644 src/slskd/Shares/Extensions.cs create mode 100644 src/slskd/Shares/SoulseekFileFactory.cs (limited to 'src') diff --git a/src/slskd/Shares/Extensions.cs b/src/slskd/Shares/Extensions.cs deleted file mode 100644 index 01e7e000..00000000 --- a/src/slskd/Shares/Extensions.cs +++ /dev/null @@ -1,71 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program 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 Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -using System.IO; - -namespace slskd.Shares -{ - using System.Collections.Generic; - using System.Linq; - using Soulseek; - - public static class Extensions - { - private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" }; - private static readonly string[] AudioExtensions = { "aa", "aax", "aac", "aiff", "ape", "dsf", "flac", "m4a", "m4b", "m4p", "mp3", "mpc", "mpp", "ogg", "oga", "wav", "wma", "wv", "webm" }; - private static readonly HashSet SupportedExtensions = AudioExtensions.Concat(VideoExtensions).ToHashSet(); - - public static File ToSoulseekFile(this string filename, Share share) - { - var code = 1; - var maskedFilename = filename.ReplaceFirst(share.LocalPath, share.RemotePath); - var size = new FileInfo(filename).Length; - var extension = Path.GetExtension(filename)[1..].ToLowerInvariant(); - List attributeList = default; - - if (SupportedExtensions.Contains(extension)) - { - attributeList = new List(); - TagLib.File file = default; - - try - { - file = TagLib.File.Create(filename, TagLib.ReadStyle.Average); - - attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); - attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); - - if (file.Properties.BitsPerSample > 0) - { - attributeList.Add(new FileAttribute(FileAttributeType.SampleRate, file.Properties.AudioSampleRate)); - attributeList.Add(new FileAttribute(FileAttributeType.BitDepth, file.Properties.BitsPerSample)); - } - } - catch - { - // unsupported or corrupt file, noop - } - finally - { - file?.Dispose(); - } - } - - return new File(code, maskedFilename, size, extension, attributeList); - } - } -} diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index f13eaad4..d68cbc3b 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -86,10 +86,13 @@ namespace slskd.Shares /// Initializes a new instance of the class. /// /// + /// public SharedFileCache( - IOptionsMonitor optionsMonitor) + IOptionsMonitor optionsMonitor, + ISoulseekFileFactory soulseekFileFactory = null) { OptionsMonitor = optionsMonitor; + SoulseekFileFactory = soulseekFileFactory ?? new SoulseekFileFactory(); } /// @@ -105,6 +108,7 @@ namespace slskd.Shares private List Shares { get; set; } private SqliteConnection SQLite { get; set; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); + private ISoulseekFileFactory SoulseekFileFactory { get; } /// /// Returns the contents of the cache. @@ -249,8 +253,8 @@ namespace slskd.Shares try { var newFiles = System.IO.Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly) - .Select(f => f.ToSoulseekFile(share)) - .ToDictionary(f => f.Filename, f => f); + .Select(filename => SoulseekFileFactory.Create(filename, share.LocalPath, share.RemotePath)) + .ToDictionary(file => file.Filename, file => file); // merge the new dictionary with the rest this will overwrite any duplicate keys, but keys are the fully // qualified name the only time this *should* cause problems is if one of the shares is a subdirectory of another. diff --git a/src/slskd/Shares/SoulseekFileFactory.cs b/src/slskd/Shares/SoulseekFileFactory.cs new file mode 100644 index 00000000..b7223c56 --- /dev/null +++ b/src/slskd/Shares/SoulseekFileFactory.cs @@ -0,0 +1,76 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +using System.IO; + +namespace slskd.Shares +{ + using System.Collections.Generic; + using System.Linq; + using Soulseek; + + public interface ISoulseekFileFactory + { + File Create(string filename, string localPath, string remotePath); + } + + public class SoulseekFileFactory : ISoulseekFileFactory + { + private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" }; + private static readonly string[] AudioExtensions = { "aa", "aax", "aac", "aiff", "ape", "dsf", "flac", "m4a", "m4b", "m4p", "mp3", "mpc", "mpp", "ogg", "oga", "wav", "wma", "wv", "webm" }; + private static readonly HashSet SupportedExtensions = AudioExtensions.Concat(VideoExtensions).ToHashSet(); + + public File Create(string filename, string localPath, string remotePath) + { + var code = 1; + var maskedFilename = filename.ReplaceFirst(localPath, remotePath); + var size = new FileInfo(filename).Length; + var extension = Path.GetExtension(filename)[1..].ToLowerInvariant(); + List attributeList = default; + + if (SupportedExtensions.Contains(extension)) + { + attributeList = new List(); + TagLib.File file = default; + + try + { + file = TagLib.File.Create(filename, TagLib.ReadStyle.Average); + + attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); + attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); + + if (file.Properties.BitsPerSample > 0) + { + attributeList.Add(new FileAttribute(FileAttributeType.SampleRate, file.Properties.AudioSampleRate)); + attributeList.Add(new FileAttribute(FileAttributeType.BitDepth, file.Properties.BitsPerSample)); + } + } + catch + { + // unsupported or corrupt file, noop + } + finally + { + file?.Dispose(); + } + } + + return new File(code, maskedFilename, size, extension, attributeList); + } + } +} -- cgit v1.2.3 From dfeed19ea66c19dfa05ea913d0c3c3d8d1b26169 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Mar 2022 21:20:57 -0500 Subject: use the official TagLib nuget --- src/slskd/slskd.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 251412e9..e2c34310 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -76,7 +76,7 @@ - + -- cgit v1.2.3 From 3b82defbf5d27411c84fd06a2b69ab2d85fd3aaa Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 20 Mar 2022 21:21:06 -0500 Subject: read images lazily --- src/slskd/Shares/SoulseekFileFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Shares/SoulseekFileFactory.cs b/src/slskd/Shares/SoulseekFileFactory.cs index b7223c56..0e345c05 100644 --- a/src/slskd/Shares/SoulseekFileFactory.cs +++ b/src/slskd/Shares/SoulseekFileFactory.cs @@ -49,7 +49,7 @@ namespace slskd.Shares try { - file = TagLib.File.Create(filename, TagLib.ReadStyle.Average); + file = TagLib.File.Create(filename, TagLib.ReadStyle.Average | TagLib.ReadStyle.PictureLazy); attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); -- cgit v1.2.3 From fbd7db198afece0a6182c109a1e6a60a08fed89d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 21 Mar 2022 10:27:39 -0500 Subject: docs and logs --- src/slskd/Shares/SharedFileCache.cs | 2 +- src/slskd/Shares/SoulseekFileFactory.cs | 35 ++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index d68cbc3b..02068e2a 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -253,7 +253,7 @@ namespace slskd.Shares try { var newFiles = System.IO.Directory.GetFiles(directory, "*", SearchOption.TopDirectoryOnly) - .Select(filename => SoulseekFileFactory.Create(filename, share.LocalPath, share.RemotePath)) + .Select(filename => SoulseekFileFactory.Create(filename, maskedFilename: filename.ReplaceFirst(share.LocalPath, share.RemotePath))) .ToDictionary(file => file.Filename, file => file); // merge the new dictionary with the rest this will overwrite any duplicate keys, but keys are the fully diff --git a/src/slskd/Shares/SoulseekFileFactory.cs b/src/slskd/Shares/SoulseekFileFactory.cs index 0e345c05..7ff2d9d7 100644 --- a/src/slskd/Shares/SoulseekFileFactory.cs +++ b/src/slskd/Shares/SoulseekFileFactory.cs @@ -19,25 +19,46 @@ using System.IO; namespace slskd.Shares { + using System; using System.Collections.Generic; using System.Linq; + using Serilog; using Soulseek; + /// + /// Creates instances of . + /// public interface ISoulseekFileFactory { - File Create(string filename, string localPath, string remotePath); + /// + /// Creates an instance of from the given path. + /// + /// The fully qualified path to the file. + /// The masked filename. + /// The created instance. + File Create(string filename, string maskedFilename); } + /// + /// Creates instances of . + /// public class SoulseekFileFactory : ISoulseekFileFactory { - private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" }; private static readonly string[] AudioExtensions = { "aa", "aax", "aac", "aiff", "ape", "dsf", "flac", "m4a", "m4b", "m4p", "mp3", "mpc", "mpp", "ogg", "oga", "wav", "wma", "wv", "webm" }; + private static readonly string[] VideoExtensions = { "mkv", "ogv", "avi", "wmv", "asf", "mp4", "m4p", "m4v", "mpg", "mpe", "mpv", "mpg", "m2v" }; private static readonly HashSet SupportedExtensions = AudioExtensions.Concat(VideoExtensions).ToHashSet(); - public File Create(string filename, string localPath, string remotePath) + private ILogger Log { get; } = Serilog.Log.ForContext(); + + /// + /// Creates an instance of from the given path. + /// + /// The fully qualified path to the file. + /// The masked filename. + /// The created instance. + public File Create(string filename, string maskedFilename) { var code = 1; - var maskedFilename = filename.ReplaceFirst(localPath, remotePath); var size = new FileInfo(filename).Length; var extension = Path.GetExtension(filename)[1..].ToLowerInvariant(); List attributeList = default; @@ -60,9 +81,9 @@ namespace slskd.Shares attributeList.Add(new FileAttribute(FileAttributeType.BitDepth, file.Properties.BitsPerSample)); } } - catch + catch (Exception ex) { - // unsupported or corrupt file, noop + Log.Warning("Failed to read metadata from file '{Filename}'; the file is an unsupported format or may be corrupt ({ExceptionType})", filename, ex.GetType().Name); } finally { @@ -73,4 +94,4 @@ namespace slskd.Shares return new File(code, maskedFilename, size, extension, attributeList); } } -} +} \ No newline at end of file -- cgit v1.2.3 From 4f459248d7d2ee624490cd41bc085a66f4d76787 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 22 Mar 2022 10:40:51 -0500 Subject: identify VBR files, reorder attributes to ensure client compatibility --- src/slskd/Shares/SoulseekFileFactory.cs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/slskd/Shares/SoulseekFileFactory.cs b/src/slskd/Shares/SoulseekFileFactory.cs index 7ff2d9d7..ccc3e6b0 100644 --- a/src/slskd/Shares/SoulseekFileFactory.cs +++ b/src/slskd/Shares/SoulseekFileFactory.cs @@ -72,11 +72,23 @@ namespace slskd.Shares { file = TagLib.File.Create(filename, TagLib.ReadStyle.Average | TagLib.ReadStyle.PictureLazy); - attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); - attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); + // try to mimick the behavior of existing clients by providing attributes selectively and in a specific order, + // depending on whether files use a lossless or lossy codec. lossless files should have BitsPerSample, while lossy + // will not. this may not be the best way to determine this. + bool isLossless = file.Properties.BitsPerSample > 0; - if (file.Properties.BitsPerSample > 0) + if (!isLossless) { + // per Nicotine+ docs, Soulseek NS, Nicotine+, Museek+, SoulSeeX all send bit rate, length, then VBR + attributeList.Add(new FileAttribute(FileAttributeType.BitRate, file.Properties.AudioBitrate)); + attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); + attributeList.Add(new FileAttribute(FileAttributeType.VariableBitRate, IsVBR(file) ? 1 : 0)); + } + else + { + // SoulseekQt 2015-6-12 and later provides the length, sample rate and bit depth for lossless files + // bitrate can be deduced from this information + attributeList.Add(new FileAttribute(FileAttributeType.Length, (int)file.Properties.Duration.TotalSeconds)); attributeList.Add(new FileAttribute(FileAttributeType.SampleRate, file.Properties.AudioSampleRate)); attributeList.Add(new FileAttribute(FileAttributeType.BitDepth, file.Properties.BitsPerSample)); } @@ -93,5 +105,16 @@ namespace slskd.Shares return new File(code, maskedFilename, size, extension, attributeList); } + + private bool IsVBR(TagLib.File file) + { + static bool HasVBRIHeader(TagLib.Mpeg.AudioHeader header) => header.VBRIHeader.Present && header.VBRIHeader.TotalSize > 0; + static bool HasXingHeader(TagLib.Mpeg.AudioHeader header) => header.XingHeader.Present && header.XingHeader.TotalSize > 0; + + return file.Properties.Codecs.Any(c => + (c is TagLib.Mpeg.AudioHeader h && (HasVBRIHeader(h) || HasXingHeader(h))) + || c is TagLib.Aac.AudioHeader + || c is TagLib.Ogg.Codec); + } } } \ No newline at end of file -- cgit v1.2.3