Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,14 @@ jobs:
--test legacy_encoders --target-dir target/fallback
cargo test --locked --release -p coreav-cli --features ffmpeg-fallback,dav1d,libvpx,libopus,aac --test transcode \
--target-dir target/fallback
- name: Check the MP3 worker encoder-only protocol
env:
COREAV_FFMPEG: ${{ runner.temp }}/coreav-oracle-bin/ffmpeg
run: |
cargo test --locked --release -p coreav-cli --features ffmpeg-fallback,dav1d,libvpx,libopus --test mp3_worker \
--target-dir target/fallback
cargo test --locked --release -p coreav-cli --features ffmpeg-fallback,dav1d,libvpx,libopus,aac --test mp3_worker \
--target-dir target/fallback
- name: Copied video with AAC encoding preserves exact fallback
env:
COREAV_REQUIRE_FFMPEG: '1'
Expand Down
32 changes: 32 additions & 0 deletions crates/coreav-cli/tests/mp3_worker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! Actual MP3 worker starts and output checked by the frozen compatibility tools.
#![cfg(all(unix, feature = "transcode"))]
#![allow(clippy::unwrap_used, clippy::panic)]

#[path = "../../../test-support/oracle.rs"]
mod test_oracle;

#[test]
fn mp3_worker_starts_and_output_pass_the_frozen_checks() {
let oracle = test_oracle::require();
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
let work = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join("mp3-worker-protocol");
if work.exists() {
std::fs::remove_dir_all(&work).unwrap();
}
let result = std::process::Command::new("python3")
.arg(root.join("scripts/mp3_worker_check.py"))
.args(["--coreav", env!("CARGO_BIN_EXE_coreav"), "--work"])
.arg(&work)
.env("COREAV_FFMPEG", oracle.ffmpeg())
.env("COREAV_FFPROBE", oracle.ffprobe())
.output()
.unwrap();
assert!(
result.status.success(),
"{}{}",
String::from_utf8_lossy(&result.stdout),
String::from_utf8_lossy(&result.stderr)
);
print!("{}", String::from_utf8_lossy(&result.stdout));
std::fs::remove_dir_all(work).unwrap();
}
117 changes: 98 additions & 19 deletions crates/coreav-transcode/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! (`coreav_worker::opus`, through the sandbox launcher), on samples
//! resampled to 48 kHz; the AAC encoder is the operator's `ffmpeg` program
//! (`crate::aac`), on samples at the stream's own rate. MP3 uses the
//! operator's `ffmpeg -c:a libmp3lame`, with checked Matroska readback and
//! operator's `ffmpeg -c:a libmp3lame`, with checked MP3 readback and
//! an explicit EOF zero guard removed by the MP3 writer's gapless tag.
//!
//! The audio is encoded before the video, so that a stream CoreAV does not
Expand Down Expand Up @@ -503,10 +503,10 @@ fn encode_with(
Encode::Mp3(..) => "mp3",
},
)?;
// The AAC encoder's Matroska, read back once it has ended.
let raw = match encode {
Encode::Opus(_) => None,
Encode::Aac(..) | Encode::Mp3(..) => Some(Spool::create(output, "mkv")?),
Encode::Aac(..) => Some(Spool::create(output, "mkv")?),
Encode::Mp3(..) => Some(Spool::create(output, "mp3-raw")?),
};
let mut feed = Feed {
encode,
Expand Down Expand Up @@ -563,7 +563,13 @@ fn encode_with(
} else {
0
};
let (sizes, pre_skip, description, duration) = match process.finish().map_err(Error::Encode)? {
let (output, stderr) = if matches!(encode, Encode::Mp3(..)) {
process.finish_with_stderr()
} else {
process.finish().map(|sink| (sink, String::new()))
}
.map_err(Error::Encode)?;
let (sizes, pre_skip, description, duration) = match output {
Output::Opus(Packets {
reader,
mut file,
Expand Down Expand Up @@ -641,10 +647,12 @@ fn encode_with(
)
),
};
let read = checked_readback(
let mut read = checked_readback(
mp3::read_output(&mut written, &expected, &limits.matroska),
deadline,
)?;
read.info.encoder =
mp3::encoder_identity(&stderr).map_err(|e| encoder_error(e.to_string()))?;
(
read.frames
.iter()
Expand Down Expand Up @@ -750,31 +758,43 @@ fn probe_encoder(
deadline: Instant,
codec: &str,
) -> Result<(), Error> {
struct Answer(Vec<u8>);
struct Answer(Vec<u8>, bool);
impl Sink for Answer {
fn write(&mut self, bytes: &[u8]) -> Result<(), coreav_decode::Error> {
let room = PROBE_ANSWER.saturating_sub(self.0.len());
if self.1 && bytes.len() > room {
return Err(coreav_decode::Error::new(
ErrorKind::Limit,
"the MP3 encoder probe exceeded its output limit",
));
}
self.0
.extend_from_slice(bytes.get(..room.min(bytes.len())).unwrap_or_default());
Ok(())
}
}
let command = command(
program,
&[
let args = if codec == "libmp3lame" {
mp3::encode_args(48000, 1, mp3::Mode::Constant(None))
} else {
[
"-hide_banner",
"-loglevel",
"error",
"-h",
&format!("encoder={codec}"),
]
.map(std::ffi::OsString::from),
.map(std::ffi::OsString::from)
.to_vec()
};
let command = command(
program,
&args,
HugePages::Kept,
launcher,
&EncoderLimits::new(AAC_ENCODER_MEMORY),
deadline,
);
let Answer(answer) = Process::spawn(
let Answer(answer, _) = Process::spawn(
if codec == "aac" {
"the AAC encoder"
} else {
Expand All @@ -784,10 +804,28 @@ fn probe_encoder(
command,
program,
deadline,
Answer(Vec::new()),
Answer(Vec::new(), codec == "libmp3lame"),
)
.and_then(Process::finish)
.map_err(Error::Encode)?;
.map_err(|error| {
if codec == "libmp3lame"
&& error.kind == ErrorKind::Backend
&& error
.message
.starts_with("the MP3 encoder failed (exit status: ")
&& error.message.contains("Unknown encoder 'libmp3lame'")
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
Error::Unsupported("the ffmpeg program has no libmp3lame encoder".into())
} else {
Error::Encode(error)
}
})?;
if codec == "libmp3lame" && !answer.is_empty() {
if !answer.starts_with(b"ID3\x04\0\0") {
return Err(encoder_error("invalid MP3 encoder probe output".into()));
}
return Ok(());
}
if !answer.starts_with(format!("Encoder {codec} [").as_bytes()) {
return Err(Error::Unsupported(format!(
"the ffmpeg program {} has no {codec} encoder; install FFmpeg with that encoder",
Expand Down Expand Up @@ -1353,12 +1391,7 @@ mod tests {
}
let mut frame = vec![0xff, 0xfb, 0x54, 0xc4];
frame.resize(192, 0);
let bytes = crate::mp3::tests::fixture(
"A_MPEG/L3",
23_020_833,
&[frame.clone(), frame],
&[None, Some(979_167)],
);
let bytes = crate::mp3::tests::fixture(&[frame.clone(), frame]);
let reads = Cell::new(0);
let deadline = Instant::now() + std::time::Duration::from_millis(20);
let source = TimedSource {
Expand Down Expand Up @@ -1584,6 +1617,52 @@ mod tests {
}
}

#[cfg(unix)]
#[test]
fn mp3_availability_errors_keep_their_classification_and_output_bound() {
for (name, body, unsupported) in [
("mp3-silent", "true", true),
(
"mp3-unavailable",
"printf \"Unknown encoder 'libmp3lame'\\n\" >&2; exit 1",
true,
),
(
"mp3-unavailable-status-eight",
"printf \"Unknown encoder 'libmp3lame'\\n\" >&2; exit 8",
true,
),
(
"mp3-unknown-before-flood",
"printf \"Unknown encoder 'libmp3lame'\\n\" >&2; exec yes",
false,
),
(
"mp3-unknown-before-signal",
"printf \"Unknown encoder 'libmp3lame'\\n\" >&2; kill -TERM $$",
false,
),
("mp3-invalid", "printf bad", false),
("mp3-flood", "exec yes", false),
("mp3-failure", "echo 'other failure' >&2; exit 1", false),
] {
let (dir, script) = ffmpeg_stand_in(name, body);
let result = probe_encoder(
&script,
None,
Instant::now() + Duration::from_secs(5),
"libmp3lame",
);
assert!(result.is_err(), "{name}");
assert_eq!(
matches!(result, Err(Error::Unsupported(_))),
unsupported,
"{name}"
);
std::fs::remove_dir_all(dir).unwrap();
}
}

#[cfg(unix)]
#[test]
fn an_aac_encoder_that_floods_its_output_is_stopped_at_once() {
Expand Down
19 changes: 17 additions & 2 deletions crates/coreav-transcode/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,15 @@ impl<S: Sink> Process<S> {
/// deadline. Returns the sink; a process that exited with an error, was
/// killed or did not read every chunk is an error carrying the end of
/// its standard error.
pub(crate) fn finish(mut self) -> Result<S, Error> {
pub(crate) fn finish(self) -> Result<S, Error> {
self.finish_inner(false).map(|(sink, _)| sink)
}

pub(crate) fn finish_with_stderr(self) -> Result<(S, String), Error> {
self.finish_inner(true)
}

fn finish_inner(mut self, diagnostics: bool) -> Result<(S, String), Error> {
if let Some(e) = self.error.take() {
return Err(e);
}
Expand Down Expand Up @@ -1478,7 +1486,14 @@ impl<S: Sink> Process<S> {
),
)));
}
Ok(sink)
Ok((
sink,
if diagnostics {
self.shared.stderr_text()
} else {
String::new()
},
))
}

/// Ends the encode with `error`: stops the writer, kills the encoder,
Expand Down
Loading
Loading