Pack video into .beam and stream frames + audio from the blob
Video was the only media type always kept external (VideoClip.file_path),
so a project with video wasn't self-contained. Now video packs into the
SQLite container under the same large-media policy as audio (pack < 2 GB
unless the user chose Reference), and both the frames and the embedded audio
track decode by streaming directly from the blob — no temp files.
- New crate ffmpeg-blob-io: an AVIOContext-over-Read+Seek shim (BlobInput)
that lets ffmpeg demux from an arbitrary byte source. Isolates all the
unsafe FFI + ffmpeg ABI coupling (version-pinned =8.0.0/=8.0.1). Manual
Drop teardown order; AVSEEK_SIZE restores the read position (FFmpeg assumes
a size query doesn't move it — required for MP4 moov-at-end).
- Schema/save/load: VideoClip.media_id; save_beam packs/references video as
MediaKind::Video (keyed by clip id); load resolves packed vs referenced and
reports missing sources. A packed clip points its linked video-audio pool
entry's media_id at the video row so the audio streams from the same blob.
- Frames: video.rs VideoSource{Path,Packed} threaded through new/seek/scan/
probe/thumbnails (a fresh BlobReader per open); editor builds the source
from current_file_path (now set before register_loaded_videos).
- Audio: VideoAudioReader::open_source via BlobInput; the disk_reader
StreamSource block on packed video-audio is removed; the engine's existing
factory activation routes it unchanged.
Tests: ffmpeg-blob-io AVIO unit tests (WAV via Cursor, seek, open/drop loop);
core packed_video_stream (blob->AVIO->Input) and beam_archive video round-trip;
daw-backend open_source test (compiles; links/runs only off-container).
Runtime-verified: a packed video plays frames + audio after the source file
is removed.
This commit is contained in:
@@ -571,8 +571,37 @@ impl CompressedReader {
|
||||
///
|
||||
/// Public (vs. the private `CompressedReader`) only so integration tests can
|
||||
/// exercise it directly; treat it as crate-internal.
|
||||
/// Adapts a host `MediaByteSource` (Read+Seek+Send+Sync) to the `ffmpeg-blob-io`
|
||||
/// `BlobSource` (Read+Seek+Send) so a packed video can be demuxed from a blob.
|
||||
struct ByteSourceAdapter(Box<dyn MediaByteSource>);
|
||||
impl std::io::Read for ByteSourceAdapter {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
self.0.read(buf)
|
||||
}
|
||||
}
|
||||
impl std::io::Seek for ByteSourceAdapter {
|
||||
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
|
||||
self.0.seek(pos)
|
||||
}
|
||||
}
|
||||
|
||||
/// A demuxer input for video-audio: a video file path, or a container blob streamed
|
||||
/// via the AVIO shim. Both expose the same ffmpeg `Input`.
|
||||
enum AudioInput {
|
||||
Path(ffmpeg_next::format::context::Input),
|
||||
Blob(ffmpeg_blob_io::BlobInput),
|
||||
}
|
||||
impl AudioInput {
|
||||
fn get(&mut self) -> &mut ffmpeg_next::format::context::Input {
|
||||
match self {
|
||||
AudioInput::Path(i) => i,
|
||||
AudioInput::Blob(b) => b.input_mut(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VideoAudioReader {
|
||||
input: ffmpeg_next::format::context::Input,
|
||||
input: AudioInput,
|
||||
decoder: ffmpeg_next::decoder::Audio,
|
||||
/// Built lazily from the first decoded frame's format/layout → interleaved f32.
|
||||
resampler: Option<ffmpeg_next::software::resampling::Context>,
|
||||
@@ -605,15 +634,30 @@ impl VideoAudioReader {
|
||||
self.total_frames
|
||||
}
|
||||
|
||||
/// Open the audio track of a video **file**.
|
||||
pub fn open(path: &Path) -> Result<Self, String> {
|
||||
ffmpeg_next::init().map_err(|e| e.to_string())?;
|
||||
let input = ffmpeg_next::format::input(&path)
|
||||
.map_err(|e| format!("Failed to open media: {}", e))?;
|
||||
Self::from_input(AudioInput::Path(input))
|
||||
}
|
||||
|
||||
/// Open the audio track of a video streamed from a **byte source** (a packed
|
||||
/// `.beam` video blob), via the `ffmpeg-blob-io` AVIO shim. `ext` is a container
|
||||
/// hint (e.g. `"mp4"`).
|
||||
pub fn open_source(src: Box<dyn MediaByteSource>, ext: Option<&str>) -> Result<Self, String> {
|
||||
ffmpeg_next::init().map_err(|e| e.to_string())?;
|
||||
let blob = ffmpeg_blob_io::BlobInput::open(Box::new(ByteSourceAdapter(src)), ext)
|
||||
.map_err(|e| format!("Failed to open packed video audio: {}", e))?;
|
||||
Self::from_input(AudioInput::Blob(blob))
|
||||
}
|
||||
|
||||
fn from_input(mut input: AudioInput) -> Result<Self, String> {
|
||||
// Pull stream scalars + build the decoder inside a scope so the stream
|
||||
// borrow of `input` ends before we use `input` again.
|
||||
let (stream_index, time_base, stream_duration, decoder) = {
|
||||
let stream = input
|
||||
.get()
|
||||
.streams()
|
||||
.best(ffmpeg_next::media::Type::Audio)
|
||||
.ok_or_else(|| "No audio stream found".to_string())?;
|
||||
@@ -631,8 +675,8 @@ impl VideoAudioReader {
|
||||
|
||||
let duration_secs = if stream_duration > 0 {
|
||||
stream_duration as f64 * time_base
|
||||
} else if input.duration() > 0 {
|
||||
input.duration() as f64 / f64::from(ffmpeg_next::ffi::AV_TIME_BASE)
|
||||
} else if input.get().duration() > 0 {
|
||||
input.get().duration() as f64 / f64::from(ffmpeg_next::ffi::AV_TIME_BASE)
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
@@ -659,6 +703,7 @@ impl VideoAudioReader {
|
||||
// Seek to at-or-before the target (max_ts = ts_av) so we can decode
|
||||
// forward to it exactly. ffmpeg-next's `seek` wants a bounded range.
|
||||
self.input
|
||||
.get()
|
||||
.seek(ts_av, 0..(ts_av + 1))
|
||||
.map_err(|e| format!("Seek failed: {}", e))?;
|
||||
self.decoder.flush();
|
||||
@@ -684,7 +729,7 @@ impl VideoAudioReader {
|
||||
}
|
||||
|
||||
// Read one packet (owned), releasing the `input` borrow before decoding.
|
||||
let packet = self.input.packets().next().map(|(_, p)| p);
|
||||
let packet = self.input.get().packets().next().map(|(_, p)| p);
|
||||
match packet {
|
||||
Some(packet) => {
|
||||
if packet.stream() == self.stream_index {
|
||||
@@ -827,8 +872,8 @@ impl StreamSource {
|
||||
(SourceKind::VideoAudio, StreamOpen::Path(p)) => {
|
||||
Ok(StreamSource::Video(VideoAudioReader::open(&p)?))
|
||||
}
|
||||
(SourceKind::VideoAudio, StreamOpen::Source { .. }) => {
|
||||
Err("VideoAudio cannot be opened from a packed byte source".to_string())
|
||||
(SourceKind::VideoAudio, StreamOpen::Source { src, ext }) => {
|
||||
Ok(StreamSource::Video(VideoAudioReader::open_source(src, ext.as_deref())?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2432,6 +2432,7 @@ impl Engine {
|
||||
channels,
|
||||
sample_rate,
|
||||
total_frames,
|
||||
None, // imported from an external video file; packing happens on save
|
||||
);
|
||||
let pool_index = self.audio_pool.add_file(audio_file);
|
||||
|
||||
|
||||
@@ -244,12 +244,15 @@ impl AudioFile {
|
||||
|
||||
/// Create a placeholder AudioFile for a video's audio track. `path` is the
|
||||
/// source video file; the audio is streamed on demand by the disk reader's
|
||||
/// FFmpeg-backed `VideoAudioReader`.
|
||||
/// FFmpeg-backed `VideoAudioReader`. `packed_media_id` is `Some` when the video
|
||||
/// bytes are packed in the `.beam` container (audio streams from the same blob
|
||||
/// via the host factory); `None` when the audio comes from an external video file.
|
||||
pub fn from_video_audio(
|
||||
path: PathBuf,
|
||||
channels: u32,
|
||||
sample_rate: u32,
|
||||
total_frames: u64,
|
||||
packed_media_id: Option<String>,
|
||||
) -> Self {
|
||||
Self {
|
||||
path,
|
||||
@@ -263,7 +266,7 @@ impl AudioFile {
|
||||
frames: total_frames,
|
||||
original_format: None,
|
||||
original_bytes: None,
|
||||
packed_media_id: None,
|
||||
packed_media_id,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1168,7 +1171,27 @@ impl AudioClipPool {
|
||||
let entry_start = std::time::Instant::now();
|
||||
eprintln!("📊 [LOAD_SERIALIZED] Processing entry {}/{}: '{}'", i + 1, entries.len(), entry.name);
|
||||
|
||||
let success = if entry.is_video_audio {
|
||||
let success = if entry.is_video_audio && entry.media_id.is_some() {
|
||||
// Packed video: the audio track lives in the same container blob as
|
||||
// the video. Build a streaming VideoAudio entry from the saved
|
||||
// metadata (no probe needed); playback opens the blob via the host
|
||||
// factory at clip-activation time → VideoAudioReader::open_source.
|
||||
let media_id = entry.media_id.clone().unwrap();
|
||||
let total_frames = (entry.duration * entry.sample_rate as f64).ceil() as u64;
|
||||
let file = AudioFile::from_video_audio(
|
||||
PathBuf::new(),
|
||||
entry.channels,
|
||||
entry.sample_rate,
|
||||
total_frames,
|
||||
Some(media_id),
|
||||
);
|
||||
if entry.pool_index < self.files.len() {
|
||||
self.files[entry.pool_index] = file;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else if entry.is_video_audio {
|
||||
// Re-probe the video's audio track via FFmpeg → a streaming
|
||||
// VideoAudio entry (keeps full 5.1/7.1; no decode-to-RAM).
|
||||
match entry.relative_path.as_ref() {
|
||||
@@ -1186,6 +1209,7 @@ impl AudioClipPool {
|
||||
reader.channels(),
|
||||
reader.sample_rate(),
|
||||
reader.total_frames(),
|
||||
None,
|
||||
);
|
||||
if entry.pool_index < self.files.len() {
|
||||
self.files[entry.pool_index] = file;
|
||||
|
||||
Reference in New Issue
Block a user