Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

fix(engine): clip.audioChannels no longer resets channels on a channels-less call (#1845)

Michaele593ea6fa3a5
diff --git a/crates/engine/src/clip_ops.rs b/crates/engine/src/clip_ops.rs
index 8a85b9eee..f7d97b29b 100644
--- a/crates/engine/src/clip_ops.rs
+++ b/crates/engine/src/clip_ops.rs
@@ -1234,7 +1234,15 @@ fn parse_format(f: &str) -> Option<AudioChannels> {
fn audio_channels(s: &mut Session, p: &Value) -> Result<Value> {
let items: Vec<ItemId> =
items_p(s, p).into_iter().filter(|i| s.project.item(*i).and_then(|it| it.as_media()).is_some_and(|m| m.info.has_audio())).collect();
- if items.is_empty() || p.get("channels").is_some() {
+ // `channels` is the only thing the timeline-clip branch below reads, so it alone decides
+ // which branch runs: no project item selected used to fall into it too (on the theory that
+ // there was nothing else to do), but a `channels`-less call with no item selected (e.g. a
+ // stray `format` with an empty project selection, #1845) then reset the selected clips'
+ // `source_channels` to `[]` via `unwrap_or_default()` instead of being rejected.
+ if items.is_empty() && p.get("channels").is_none() {
+ return Err(bad("clip.audioChannels", "select a clip with audio, or a project item to set its format"));
+ }
+ if p.get("channels").is_some() {
// timeline audio clips: which source channels each plays
let clips = match p.get("clips").and_then(Value::as_array) {
Some(a) if a.iter().all(Value::is_u64) => a.iter().filter_map(|v| v.as_u64().map(ClipId)).collect(),
diff --git a/crates/engine/src/clip_ops_tests.rs b/crates/engine/src/clip_ops_tests.rs
index 262b1ec80..12d195929 100644
--- a/crates/engine/src/clip_ops_tests.rs
+++ b/crates/engine/src/clip_ops_tests.rs
@@ -379,6 +379,24 @@ fn modify_audio_channels_and_breakout_to_mono() {
assert!(s.execute("clip.breakoutToMono", json!({})).is_err(), "no audio");
}
+/// #1845: with no project item selected, a `channels`-less call (a stray `format` included)
+/// used to fall into the timeline-clip branch anyway and reset `source_channels` to `[]` via
+/// `unwrap_or_default()`, instead of being rejected. The map set by the previous `channels` call
+/// must survive a later `format`-only call under the same (empty) project selection.
+#[test]
+fn audio_channels_rejects_a_channel_less_call_with_no_item_selected() {
+ let mut s = demo();
+ s.state.project_selection.clear();
+ let a1 = s.active_sequence().unwrap().audio_tracks[0].items[0].id;
+ s.state.selection = vec![a1];
+ s.execute("clip.audioChannels", json!({"channels": [1]})).unwrap();
+ assert_eq!(clip(&s, a1.0).source_channels, vec![1]);
+ assert!(s.execute("clip.audioChannels", json!({"format": null})).is_err(), "nothing to do: no item selected and no `channels`");
+ assert_eq!(clip(&s, a1.0).source_channels, vec![1], "the existing map must survive a rejected call");
+ assert!(s.execute("clip.audioChannels", json!({})).is_err(), "same with no params at all");
+ assert_eq!(clip(&s, a1.0).source_channels, vec![1]);
+}
+
#[test]
fn modify_timecode_and_reset() {
let mut s = demo();