From 401f03e2f0149b0f9d68df5d62fc3d5cccd3dc9e Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Fri, 10 Oct 2025 16:34:43 -0500 Subject: [PATCH] fix: add validation that stream is seekable --- .../Media/Audio/AudioExtractor.cs | 23 +++++++++++-------- .../Media/Audio/IAudioExtractor.cs | 1 + 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/StreamShorts.Library/Media/Audio/AudioExtractor.cs b/src/StreamShorts.Library/Media/Audio/AudioExtractor.cs index e852638..ca21e86 100644 --- a/src/StreamShorts.Library/Media/Audio/AudioExtractor.cs +++ b/src/StreamShorts.Library/Media/Audio/AudioExtractor.cs @@ -32,14 +32,9 @@ public sealed class AudioExtractor : IAudioExtractor throw new ArgumentNullException(nameof(video), "Video stream cannot be null"); } - if (video.CanRead is false) + if (IsVideoStreamUsable(video) is false) { - throw new ArgumentException("Video stream must be readable", nameof(video)); - } - - if (video.CanSeek is false) - { - throw new ArgumentException("Video stream must be seekable", nameof(video)); + throw new ArgumentException("Stream must be non-null, readable, and seekable", nameof(video)); } if (audio is null) @@ -47,9 +42,9 @@ public sealed class AudioExtractor : IAudioExtractor throw new ArgumentNullException(nameof(audio), "Audio stream cannot be null"); } - if (audio.CanWrite is false) + if (IsAudioStreamUsable(audio) is false) { - throw new ArgumentException("Audio stream must be writable", nameof(audio)); + throw new ArgumentException("Stream must be non-null, writable, and seekable", nameof(audio)); } var originalPosition = video.Position; @@ -75,4 +70,14 @@ public sealed class AudioExtractor : IAudioExtractor video.Position = originalPosition; } } + + private static bool IsVideoStreamUsable(Stream stream) + { + return stream.CanRead && stream.CanSeek; + } + + private static bool IsAudioStreamUsable(Stream stream) + { + return stream.CanWrite && stream.CanSeek; + } } \ No newline at end of file diff --git a/src/StreamShorts.Library/Media/Audio/IAudioExtractor.cs b/src/StreamShorts.Library/Media/Audio/IAudioExtractor.cs index 61f4954..6730c18 100644 --- a/src/StreamShorts.Library/Media/Audio/IAudioExtractor.cs +++ b/src/StreamShorts.Library/Media/Audio/IAudioExtractor.cs @@ -16,6 +16,7 @@ public interface IAudioExtractor /// Thrown when the video stream is not seekable. /// Thrown when the audio stream is null. /// Thrown when the audio stream is not writable. + /// Thrown when the audio stream is not seekable. /// Thrown when the audio extraction fails. /// The method will preserve the passed video stream's data and position. Task ExtractMp3FromMp4Async(Stream video, Stream audio);