From d8fc77101dcf80a3643a00b3446dca1e390ce997 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Thu, 10 Aug 2023 15:33:00 +1000 Subject: Give codecs complete control of their input files --- src/codecs/include/codec.hpp | 46 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'src/codecs/include/codec.hpp') diff --git a/src/codecs/include/codec.hpp b/src/codecs/include/codec.hpp index 32ebef69..ece3d4fe 100644 --- a/src/codecs/include/codec.hpp +++ b/src/codecs/include/codec.hpp @@ -23,6 +23,34 @@ namespace codecs { +/* + * Interface for an abstract source of file-like data. + */ +class IStream { + public: + IStream(StreamType t) : t_(t) {} + virtual ~IStream() {} + + auto type() -> StreamType { return t_; } + + virtual auto Read(cpp::span dest) -> ssize_t = 0; + + virtual auto CanSeek() -> bool = 0; + + enum class SeekFrom { + kStartOfStream, + kEndOfStream, + kCurrentPosition, + }; + + virtual auto SeekTo(int64_t destination, SeekFrom from) -> void = 0; + + virtual auto CurrentPosition() -> int64_t = 0; + + protected: + StreamType t_; +}; + /* * Common interface to be implemented by all audio decoders. */ @@ -63,32 +91,30 @@ class ICodec { struct OutputFormat { uint8_t num_channels; uint32_t sample_rate_hz; - std::optional duration_seconds; - std::optional bits_per_second; + + bool operator==(const OutputFormat&) const = default; }; /* * Decodes metadata or headers from the given input stream, and returns the * format for the samples that will be decoded from it. */ - virtual auto BeginStream(cpp::span input) - -> Result = 0; + virtual auto OpenStream(std::shared_ptr input) + -> cpp::result = 0; struct OutputInfo { std::size_t samples_written; - bool is_finished_writing; + bool is_stream_finished; }; /* * Writes PCM samples to the given output buffer. */ - virtual auto ContinueStream(cpp::span input, - cpp::span output) - -> Result = 0; + virtual auto DecodeTo(cpp::span destination) + -> cpp::result = 0; - virtual auto SeekStream(cpp::span input, - std::size_t target_sample) -> Result = 0; + virtual auto SeekTo(size_t target_sample) -> cpp::result = 0; }; auto CreateCodecForType(StreamType type) -> std::optional; -- cgit v1.2.3