stream-download-rs
stream-download is a library for streaming content from a remote location to a local cache and using it as a read
and seek
-able source.
The requested content is downloaded in the background and read or seek operations are allowed before the download is finished.
Seek operations may cause the stream to be restarted from the requested position if the download is still in progress.
This is useful for media applications that need to stream large files that may take a long time to download.
This library makes heavy use of the adapter pattern to allow for pluggable transports and storage implementations.
Installation
Features
http
- adds an HTTP-based implementation of theSourceStream
trait (enabled by default).reqwest
- enables streaming content over http using reqwest (enabled by default).reqwest-native-tls
- enables reqwest'snative-tls
feature. Also enables thereqwest
feature.reqwest-rustls
- enables reqwest'srustls
feature. Also enables thereqwest
feature.open-dal
- adds aSourceStream
implementation that uses Apache OpenDAL as the backend.temp-storage
- adds a temporary file-based storage backend (enabled by default).
One of reqwest-native-tls
or reqwest-rustls
is required if you wish to use https streams.
Usage
use Error;
use Read;
use io;
use Result;
use TempStorageProvider;
use ;
async
Examples
See examples.
Transports
Transports implement the SourceStream
trait. Two types of transports are provided out of the box - http
for typical HTTP-based sources and open_dal
which is more complex, but supports a large variety of services.
Only http
is enabled by default.
You can provide a custom transport by implementing SourceStream
yourself.
Streams with Unknown Length
Resources such as standalone songs or videos have a finite length that we use to support certain seeking functionality. Infinite streams or those that otherwise don't have a known length are still supported, but attempting to seek from the end of the stream will return an error. This may cause issues with certain audio or video libraries that attempt to perform such seek operations. If it's necessary to explicitly check for an infinite stream, you can check the stream's content length ahead of time.
use Error;
use Read;
use io;
use Result;
use HttpStream;
use Client;
use SourceStream;
use TempStorageProvider;
use ;
async
Icecast/Shoutcast Streams
If you're using this library to handle Icecast streams or one if its derivatives, check out the icy-metadata crate. There are examples for how to use it with stream-download in the repo.
Storage
The storage module provides ways to customize how the stream is cached locally. Pre-configured implementations are available for memory and temporary file-based storage. Typically you'll want to use temporary file-based storage to prevent using too much memory, but memory-based storage may be preferable if you know the stream size is small or you need to run your application on a read-only filesystem.
use Error;
use Read;
use Result;
use MemoryStorageProvider;
use ;
async
Bounded Storage
When using infinite streams which don't need to support seeking, it usually isn't desirable to let the underlying cache grow indefinitely if the stream may be running for a while. For these cases, you may want to use bounded storage. Bounded storage uses a circular buffer which will overwrite the oldest contents once it fills up.
use Error;
use Read;
use NonZeroUsize;
use Result;
use BoundedStorageProvider;
use MemoryStorageProvider;
use ;
async
Adaptive Storage
When you need to support both finite and infinite streams, you may want to use adaptive storage. This is a convenience wrapper that will use bounded storage when the stream has no content length and unbounded storage when the stream does return a content length.
use Error;
use Read;
use NonZeroUsize;
use Result;
use AdaptiveStorageProvider;
use TempStorageProvider;
use ;
async
Authentication and other customization
It's possible to customize your HTTP requests if you need to perform authentication or change other settings.
See client_options for customizing the HTTP client builder.
See custom_client for dynamically modifying each HTTP request.
Supported Rust Versions
The MSRV is currently 1.75.0
.