Merge branch 'main' into worktree-agent-a5b9617c2130e9ba7
Browse files at this commit5 changed files +702 -57
- crates/app/src/main.rs +228 -35
- crates/engine/src/decode.rs +116 -1
- crates/engine/src/export.rs +142 -20
- crates/engine/src/playback.rs +29 -1
- crates/engine/tests/backends.rs +187 -0
crates/app/src/main.rsmodified+228 -35
| @@ -11,11 +11,12 @@ use std::time::{Duration, Instant}; | |||
| 11 | 11 | ||
| 12 | use engine::audio::StreamInfo; | 12 | use engine::audio::StreamInfo; |
| 13 | use engine::color::ColorParams; | 13 | use engine::color::ColorParams; |
| 14 | +use engine::decode::Backend; | ||
| 14 | use engine::eq::{Band, BandKind, EqParams}; | 15 | use engine::eq::{Band, BandKind, EqParams}; |
| 15 | use engine::export::{ExportSettings, Format}; | 16 | use engine::export::{ExportSettings, Format}; |
| 16 | use engine::project::{Edge, Lane, LaneKind, Source, Speed}; | 17 | use engine::project::{Edge, Lane, LaneKind, Source, Speed}; |
| 17 | use engine::scale::FitPolicy; | 18 | use engine::scale::FitPolicy; |
| 18 | -use engine::{Clip, ExportHandle, Frame, PlaybackSession}; | 19 | +use engine::{Clip, Codec, ExportHandle, Frame, PlaybackSession}; |
| 19 | use gpui::{ | 20 | use gpui::{ |
| 20 | AnyElement, App, Application, Bounds, ClickEvent, Context, CursorStyle, Div, FocusHandle, | 21 | AnyElement, App, Application, Bounds, ClickEvent, Context, CursorStyle, Div, FocusHandle, |
| 21 | KeyDownEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, | 22 | KeyDownEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PathBuilder, Pixels, |
| @@ -644,6 +645,19 @@ struct Player { | |||
| 644 | /// `None` is a file with no picture to report (every source that is not an | 645 | /// `None` is a file with no picture to report (every source that is not an |
| 645 | /// image, and one whose header would not read). | 646 | /// image, and one whose header would not read). |
| 646 | sizes: HashMap<PathBuf, Option<(u32, u32)>>, | 647 | sizes: HashMap<PathBuf, Option<(u32, u32)>>, |
| 648 | + /// Which decoder each source will run on, probed once at import and kept: | ||
| 649 | + /// the codec (`None` for a still) and the seat the engine picked for it. | ||
| 650 | + /// What a library row says *before* anything plays; the running answer is | ||
| 651 | + /// the session's own (`PlaybackSession::decode_backend`), which follows a | ||
| 652 | + /// fallback this cannot. Filled like `sizes`: presence means "asked", and | ||
| 653 | + /// `None` is a source with no decoder to name -- a song, or one the probe | ||
| 654 | + /// refused -- which must stay in the map or every repaint would ask again. | ||
| 655 | + decoders: HashMap<PathBuf, Option<(Option<Codec>, Backend)>>, | ||
| 656 | + /// Which encoder an export of the picked settings would open, and what it | ||
| 657 | + /// was asked about: the probe opens a real VA-API encoder (~100 ms), so it | ||
| 658 | + /// runs off the render thread and only while the export card is up. The | ||
| 659 | + /// inner `None` is "asked, not answered yet". | ||
| 660 | + export_seat: Option<(ExportSettings, (u32, u32), Option<&'static str>)>, | ||
| 647 | /// The copied clip. Frame ranges only, so it survives the clip it was taken | 661 | /// The copied clip. Frame ranges only, so it survives the clip it was taken |
| 648 | /// from being deleted -- and it outlives the selection. | 662 | /// from being deleted -- and it outlives the selection. |
| 649 | clipboard: Option<Clip>, | 663 | clipboard: Option<Clip>, |
| @@ -2011,6 +2025,28 @@ impl Player { | |||
| 2011 | }) | 2025 | }) |
| 2012 | .detach(); | 2026 | .detach(); |
| 2013 | } | 2027 | } |
| 2028 | + // Which decoder each file will run on, for the row that says so before | ||
| 2029 | + // a frame of it plays. Off the render thread like the streams above: a | ||
| 2030 | + // stream the plugin takes costs one VA-API init (~90 ms) to answer. | ||
| 2031 | + for path in unseen_paths(session.sources(), &self.decoders) { | ||
| 2032 | + self.decoders.insert(path.clone(), None); | ||
| 2033 | + let probed = cx.background_executor().spawn({ | ||
| 2034 | + let path = path.clone(); | ||
| 2035 | + // A song and a source no decoder here takes are both `None`: | ||
| 2036 | + // the row says nothing about them rather than guessing, and | ||
| 2037 | + // import refused the second at the door anyway. | ||
| 2038 | + async move { engine::decode::probe(&path).ok() } | ||
| 2039 | + }); | ||
| 2040 | + cx.spawn(async move |this, cx| { | ||
| 2041 | + let probed = probed.await; | ||
| 2042 | + this.update(cx, |this, cx| { | ||
| 2043 | + this.decoders.insert(path, probed); | ||
| 2044 | + cx.notify(); | ||
| 2045 | + }) | ||
| 2046 | + .ok(); | ||
| 2047 | + }) | ||
| 2048 | + .detach(); | ||
| 2049 | + } | ||
| 2014 | for key in unseen_sources(session.sources(), &self.waves) { | 2050 | for key in unseen_sources(session.sources(), &self.waves) { |
| 2015 | self.waves.insert(key.clone(), Wave::Loading); | 2051 | self.waves.insert(key.clone(), Wave::Loading); |
| 2016 | let decoded = cx.background_executor().spawn({ | 2052 | let decoded = cx.background_executor().spawn({ |
| @@ -2043,6 +2079,51 @@ impl Player { | |||
| 2043 | } | 2079 | } |
| 2044 | } | 2080 | } |
| 2045 | 2081 | ||
| 2082 | + /// Probes the encoder an export would open, once per (settings, | ||
| 2083 | + /// resolution) and only while the export card is up -- it opens the very | ||
| 2084 | + /// VA-API encoder the export would, which is what makes the card's line a | ||
| 2085 | + /// measurement instead of a promise, and also what makes it too slow for | ||
| 2086 | + /// the render thread. Written before the spawn, like the probes above, so | ||
| 2087 | + /// the repaints during it start no second one. | ||
| 2088 | + fn cache_export_seat(&mut self, cx: &mut Context<Self>) { | ||
| 2089 | + let Some(session) = &self.session else { | ||
| 2090 | + return; | ||
| 2091 | + }; | ||
| 2092 | + let settings = export_settings(self.quality, self.custom_mbps, self.format); | ||
| 2093 | + if !self.export_open || !settings.format.has_video() { | ||
| 2094 | + return; | ||
| 2095 | + } | ||
| 2096 | + let meta = *session.meta(); | ||
| 2097 | + let key = (settings, (meta.width, meta.height)); | ||
| 2098 | + if self | ||
| 2099 | + .export_seat | ||
| 2100 | + .is_some_and(|(asked, size, _)| (asked, size) == key) | ||
| 2101 | + { | ||
| 2102 | + return; | ||
| 2103 | + } | ||
| 2104 | + self.export_seat = Some((key.0, key.1, None)); | ||
| 2105 | + let probed = cx | ||
| 2106 | + .background_executor() | ||
| 2107 | + .spawn(async move { engine::export::planned_video(&meta, &settings) }); | ||
| 2108 | + cx.spawn(async move |this, cx| { | ||
| 2109 | + let probed = probed.await; | ||
| 2110 | + this.update(cx, |this, cx| { | ||
| 2111 | + // Only if the card is still asking the same question: a format | ||
| 2112 | + // changed while the plugin opened has a probe of its own. | ||
| 2113 | + if let Some(seat) = this | ||
| 2114 | + .export_seat | ||
| 2115 | + .as_mut() | ||
| 2116 | + .filter(|(asked, size, _)| (*asked, *size) == key) | ||
| 2117 | + { | ||
| 2118 | + seat.2 = probed; | ||
| 2119 | + } | ||
| 2120 | + cx.notify(); | ||
| 2121 | + }) | ||
| 2122 | + .ok(); | ||
| 2123 | + }) | ||
| 2124 | + .detach(); | ||
| 2125 | + } | ||
| 2126 | + | ||
| 2046 | /// The group id of the clicked clip, which is what marks the other half. | 2127 | /// The group id of the clicked clip, which is what marks the other half. |
| 2047 | fn selected_link(&self) -> Option<u32> { | 2128 | fn selected_link(&self) -> Option<u32> { |
| 2048 | let (lane, idx) = self.selected?; | 2129 | let (lane, idx) = self.selected?; |
| @@ -3152,6 +3233,7 @@ impl Render for Player { | |||
| 3152 | // has been through a repaint by the time its clips are drawn, so this | 3233 | // has been through a repaint by the time its clips are drawn, so this |
| 3153 | // is the one place that has to notice a new one. | 3234 | // is the one place that has to notice a new one. |
| 3154 | self.cache_media(cx); | 3235 | self.cache_media(cx); |
| 3236 | + self.cache_export_seat(cx); | ||
| 3155 | // What the compositor calls this window. Pushed only when it changes: | 3237 | // What the compositor calls this window. Pushed only when it changes: |
| 3156 | // it is a protocol round trip and this runs at vsync. | 3238 | // it is a protocol round trip and this runs at vsync. |
| 3157 | let title = window_title(&self.name); | 3239 | let title = window_title(&self.name); |
| @@ -3642,11 +3724,17 @@ impl Player { | |||
| 3642 | // Its own length, not one derived from what is on the lanes: a row | 3724 | // Its own length, not one derived from what is on the lanes: a row |
| 3643 | // imported and never placed is a row with a length, and it is the | 3725 | // imported and never placed is a row with a length, and it is the |
| 3644 | // length a drag would put down. | 3726 | // length a drag would put down. |
| 3645 | - let rows: Vec<_> = library_rows(sources, &self.streams, self.timeline_audio(), |path| { | 3727 | + let rows: Vec<_> = library_rows( |
| 3646 | - self.session | 3728 | + sources, |
| 3647 | - .as_ref() | 3729 | + &self.streams, |
| 3648 | - .map_or(0, |session| session.file_frames(path)) | 3730 | + &self.decoders, |
| 3649 | - }) | 3731 | + self.timeline_audio(), |
| 3732 | + |path| { | ||
| 3733 | + self.session | ||
| 3734 | + .as_ref() | ||
| 3735 | + .map_or(0, |session| session.file_frames(path)) | ||
| 3736 | + }, | ||
| 3737 | + ) | ||
| 3650 | .into_iter() | 3738 | .into_iter() |
| 3651 | .enumerate() | 3739 | .enumerate() |
| 3652 | .map(|(i, row)| { | 3740 | .map(|(i, row)| { |
| @@ -3883,6 +3971,30 @@ impl Player { | |||
| 3883 | )) | 3971 | )) |
| 3884 | } | 3972 | } |
| 3885 | 3973 | ||
| 3974 | + /// What is decoding the picture right now, for the transport line: the | ||
| 3975 | + /// backend is the running worker's own (it is written where a hardware | ||
| 3976 | + /// session falls back to software, so this follows reality), and the codec | ||
| 3977 | + /// comes from the clip under the playhead. Empty when nothing is playing -- | ||
| 3978 | + /// the question is about what is happening, not about what would. | ||
| 3979 | + fn live_decode(&self, position: f64, playing: bool) -> String { | ||
| 3980 | + let Some(session) = self.session.as_ref().filter(|_| playing) else { | ||
| 3981 | + return String::new(); | ||
| 3982 | + }; | ||
| 3983 | + let backend = session.decode_backend(); | ||
| 3984 | + let codec = session | ||
| 3985 | + .video_clip_at(position) | ||
| 3986 | + .and_then(|(lane, idx)| session.lane_clips(lane).get(idx).map(|clip| clip.source)) | ||
| 3987 | + .and_then(|source| session.sources().get(source)) | ||
| 3988 | + .and_then(|source| self.decoders.get(&source.path).copied().flatten()) | ||
| 3989 | + .and_then(|(codec, _)| codec); | ||
| 3990 | + match backend { | ||
| 3991 | + // Neither is a decode, and saying "SW" of them would be a lie. | ||
| 3992 | + Backend::Gap => "gap · nothing to decode".to_string(), | ||
| 3993 | + Backend::Still => "still · one decode, held".to_string(), | ||
| 3994 | + _ => format!("{} decode", decode_label(codec, backend)), | ||
| 3995 | + } | ||
| 3996 | + } | ||
| 3997 | + | ||
| 3886 | /// Transport, edit and file buttons, timecode, playhead, clips lane. | 3998 | /// Transport, edit and file buttons, timecode, playhead, clips lane. |
| 3887 | fn panel( | 3999 | fn panel( |
| 3888 | &self, | 4000 | &self, |
| @@ -3924,8 +4036,16 @@ impl Player { | |||
| 3924 | let progress = export.progress(); | 4036 | let progress = export.progress(); |
| 3925 | ( | 4037 | ( |
| 3926 | format!( | 4038 | format!( |
| 3927 | - "EXPORTING {}% — {} cancels", | 4039 | + "EXPORTING {}% · {} · {} — {} cancels", |
| 3928 | (progress * 100.) as u32, | 4040 | (progress * 100.) as u32, |
| 4041 | + // The row that was picked; the engine's line below names the | ||
| 4042 | + // seats alone, since the library is what identifies a codec. | ||
| 4043 | + format_label(self.format), | ||
| 4044 | + // What the worker actually opened, so a fallback to the | ||
| 4045 | + // software encoder shows here rather than being invisible. | ||
| 4046 | + export | ||
| 4047 | + .encoders() | ||
| 4048 | + .unwrap_or_else(|| "opening the encoder".to_string()), | ||
| 3929 | key(ActionId::CancelExport) | 4049 | key(ActionId::CancelExport) |
| 3930 | ), | 4050 | ), |
| 3931 | progress, | 4051 | progress, |
| @@ -3935,12 +4055,19 @@ impl Player { | |||
| 3935 | // tooltips. Keys first: at a 640 px window the tail is what a | 4055 | // tooltips. Keys first: at a 640 px window the tail is what a |
| 3936 | // truncation eats, and the two hints at the end are also on the | 4056 | // truncation eats, and the two hints at the end are also on the |
| 3937 | // ruler's and Import's tooltips. | 4057 | // ruler's and Import's tooltips. |
| 4058 | + // While it plays, what is decoding it goes first: it is the | ||
| 4059 | + // answer that changes as the playhead crosses a cut, and the tail | ||
| 4060 | + // of this line is what a narrow window truncates. | ||
| 3938 | ( | 4061 | ( |
| 3939 | - format!( | 4062 | + join_detail( |
| 3940 | - "{} copy · {} paste · {} undo · click the bar to seek · drop a file to import", | 4063 | + &self.live_decode(position, playing), |
| 3941 | - key(ActionId::Copy), | 4064 | + &format!( |
| 3942 | - key(ActionId::Paste), | 4065 | + "{} copy · {} paste · {} undo · click the bar to seek · drop a file to \ |
| 3943 | - key(ActionId::Undo) | 4066 | + import", |
| 4067 | + key(ActionId::Copy), | ||
| 4068 | + key(ActionId::Paste), | ||
| 4069 | + key(ActionId::Undo) | ||
| 4070 | + ), | ||
| 3944 | ), | 4071 | ), |
| 3945 | filled, | 4072 | filled, |
| 3946 | ) | 4073 | ) |
| @@ -4711,7 +4838,11 @@ impl Player { | |||
| 4711 | .px(px(6.)) | 4838 | .px(px(6.)) |
| 4712 | .text_size(px(11.)) | 4839 | .text_size(px(11.)) |
| 4713 | .text_color(rgb(INK_DIM)) | 4840 | .text_color(rgb(INK_DIM)) |
| 4714 | - .child(export_line(self.format, self.session.as_ref())), | 4841 | + .child(export_line( |
| 4842 | + self.format, | ||
| 4843 | + self.session.as_ref(), | ||
| 4844 | + self.export_seat.and_then(|(.., seat)| seat), | ||
| 4845 | + )), | ||
| 4715 | ) | 4846 | ) |
| 4716 | .child( | 4847 | .child( |
| 4717 | div() | 4848 | div() |
| @@ -6576,6 +6707,7 @@ struct Row { | |||
| 6576 | fn library_rows( | 6707 | fn library_rows( |
| 6577 | sources: &[Source], | 6708 | sources: &[Source], |
| 6578 | streams: &HashMap<PathBuf, Vec<StreamInfo>>, | 6709 | streams: &HashMap<PathBuf, Vec<StreamInfo>>, |
| 6710 | + decoders: &HashMap<PathBuf, Option<(Option<Codec>, Backend)>>, | ||
| 6579 | timeline_audio: Option<(u32, u16)>, | 6711 | timeline_audio: Option<(u32, u16)>, |
| 6580 | frames: impl Fn(&Path) -> u32, | 6712 | frames: impl Fn(&Path) -> u32, |
| 6581 | ) -> Vec<Row> { | 6713 | ) -> Vec<Row> { |
| @@ -6591,12 +6723,22 @@ fn library_rows( | |||
| 6591 | path: source.path.clone(), | 6723 | path: source.path.clone(), |
| 6592 | stream: source.audio_stream, | 6724 | stream: source.audio_stream, |
| 6593 | name: row_name(&source.path, source.audio_stream, of_file.len() > 1), | 6725 | name: row_name(&source.path, source.audio_stream, of_file.len() > 1), |
| 6594 | - // Only where there is a choice to describe: a file with one audio | 6726 | + // The decoder first: it is the same for every row of a file and |
| 6595 | - // track is the row it has always been, name and length, and the | 6727 | + // it is what a person opening the panel is asking about. The |
| 6596 | - // length is what would be squeezed out at the panel's least width. | 6728 | + // stream half only where there is a choice to describe -- a file |
| 6597 | - detail: info | 6729 | + // with one audio track is the row it has always been, name and |
| 6598 | - .filter(|_| of_file.len() > 1) | 6730 | + // length, and the length is what would be squeezed out at the |
| 6599 | - .map_or_else(String::new, stream_detail), | 6731 | + // panel's least width. |
| 6732 | + detail: join_detail( | ||
| 6733 | + &decoders | ||
| 6734 | + .get(&source.path) | ||
| 6735 | + .copied() | ||
| 6736 | + .flatten() | ||
| 6737 | + .map_or_else(String::new, |(codec, backend)| decode_label(codec, backend)), | ||
| 6738 | + &info | ||
| 6739 | + .filter(|_| of_file.len() > 1) | ||
| 6740 | + .map_or_else(String::new, stream_detail), | ||
| 6741 | + ), | ||
| 6600 | // A stream already on the timeline is playing: whatever a probe | 6742 | // A stream already on the timeline is playing: whatever a probe |
| 6601 | // would say about it now, it is usable by demonstration. | 6743 | // would say about it now, it is usable by demonstration. |
| 6602 | unusable: None, | 6744 | unusable: None, |
| @@ -6633,9 +6775,20 @@ fn library_rows( | |||
| 6633 | /// why it cannot be used, with the separator only where both halves exist (a | 6775 | /// why it cannot be used, with the separator only where both halves exist (a |
| 6634 | /// single-stream file says nothing about its stream). | 6776 | /// single-stream file says nothing about its stream). |
| 6635 | fn join_detail(detail: &str, tail: &str) -> String { | 6777 | fn join_detail(detail: &str, tail: &str) -> String { |
| 6636 | - match detail.is_empty() { | 6778 | + match (detail.is_empty(), tail.is_empty()) { |
| 6637 | - true => tail.to_string(), | 6779 | + (true, _) => tail.to_string(), |
| 6638 | - false => format!("{detail} · {tail}"), | 6780 | + (false, true) => detail.to_string(), |
| 6781 | + (false, false) => format!("{detail} · {tail}"), | ||
| 6782 | + } | ||
| 6783 | +} | ||
| 6784 | + | ||
| 6785 | +/// How a source's decoder reads: the codec and which seat has it, or the seat | ||
| 6786 | +/// alone for a still, which has no coded stream to name. The one place either | ||
| 6787 | +/// answer is spelled, so a row, a transport line and a card cannot disagree. | ||
| 6788 | +fn decode_label(codec: Option<Codec>, backend: Backend) -> String { | ||
| 6789 | + match codec { | ||
| 6790 | + Some(codec) => format!("{} · {}", codec.name(), backend.label()), | ||
| 6791 | + None => backend.label().to_string(), | ||
| 6639 | } | 6792 | } |
| 6640 | } | 6793 | } |
| 6641 | 6794 | ||
| @@ -6953,14 +7106,32 @@ fn next_resolution(current: (u32, u32), native: (u32, u32)) -> (u32, u32) { | |||
| 6953 | /// [`format_line`] plus the project's resolution, which is what a video export | 7106 | /// [`format_line`] plus the project's resolution, which is what a video export |
| 6954 | /// is written at however many sizes the media on the timeline are. Only for the | 7107 | /// is written at however many sizes the media on the timeline are. Only for the |
| 6955 | /// formats that carry a picture -- a WAV has no resolution to state. | 7108 | /// formats that carry a picture -- a WAV has no resolution to state. |
| 6956 | -fn export_line(format: Format, session: Option<&PlaybackSession>) -> String { | 7109 | +fn export_line( |
| 7110 | + format: Format, | ||
| 7111 | + session: Option<&PlaybackSession>, | ||
| 7112 | + seat: Option<&'static str>, | ||
| 7113 | +) -> String { | ||
| 6957 | let line = format_line(format); | 7114 | let line = format_line(format); |
| 7115 | + // What it will really be encoded by, on the same line: the picture's seat | ||
| 7116 | + // as the probe found it (`…` until it lands, never a guess) and the sound's, | ||
| 7117 | + // which is decided without opening anything. Both are what a running export | ||
| 7118 | + // then names on its progress line, from the same two functions. | ||
| 7119 | + let encoders = match session { | ||
| 7120 | + Some(session) => join_detail( | ||
| 7121 | + seat.unwrap_or(match format.has_video() { | ||
| 7122 | + true => "encoder …", | ||
| 7123 | + false => "", | ||
| 7124 | + }), | ||
| 7125 | + session.planned_audio(format), | ||
| 7126 | + ), | ||
| 7127 | + None => String::new(), | ||
| 7128 | + }; | ||
| 6958 | match session.filter(|_| format.has_video()) { | 7129 | match session.filter(|_| format.has_video()) { |
| 6959 | Some(session) => { | 7130 | Some(session) => { |
| 6960 | let (w, h) = session.resolution(); | 7131 | let (w, h) = session.resolution(); |
| 6961 | - format!("{line} · project {w}x{h}") | 7132 | + format!("{line} · project {w}x{h} · {encoders}") |
| 6962 | } | 7133 | } |
| 6963 | - None => line.to_string(), | 7134 | + None => join_detail(line, &encoders), |
| 6964 | } | 7135 | } |
| 6965 | } | 7136 | } |
| 6966 | 7137 | ||
| @@ -8062,9 +8233,13 @@ mod tests { | |||
| 8062 | ); | 8233 | ); |
| 8063 | // The rows the panel draws: one per source, each its file's own length | 8234 | // The rows the panel draws: one per source, each its file's own length |
| 8064 | // -- source 1 has no clip anywhere and is still 4 s at 30 fps. | 8235 | // -- source 1 has no clip anywhere and is still 4 s at 30 fps. |
| 8065 | - let rows = library_rows(session.sources(), &HashMap::new(), None, |path| { | 8236 | + let rows = library_rows( |
| 8066 | - session.file_frames(path) | 8237 | + session.sources(), |
| 8067 | - }); | 8238 | + &HashMap::new(), |
| 8239 | + &HashMap::new(), | ||
| 8240 | + None, | ||
| 8241 | + |path| session.file_frames(path), | ||
| 8242 | + ); | ||
| 8068 | assert_eq!(rows.len(), 2); | 8243 | assert_eq!(rows.len(), 2); |
| 8069 | assert_eq!(rows[0].frames, 150, "5 s at 30 fps"); | 8244 | assert_eq!(rows[0].frames, 150, "5 s at 30 fps"); |
| 8070 | assert_eq!(rows[1].frames, 120, "4 s at 30 fps, never placed"); | 8245 | assert_eq!(rows[1].frames, 120, "4 s at 30 fps, never placed"); |
| @@ -8103,7 +8278,9 @@ mod tests { | |||
| 8103 | .find(|s| s.index == session.sources()[0].audio_stream) | 8278 | .find(|s| s.index == session.sources()[0].audio_stream) |
| 8104 | .map(|s| (s.sample_rate, s.channels)); | 8279 | .map(|s| (s.sample_rate, s.channels)); |
| 8105 | let frames = session.file_frames(&path); | 8280 | let frames = session.file_frames(&path); |
| 8106 | - let rows = library_rows(session.sources(), &streams, rate, |_| frames); | 8281 | + let rows = library_rows(session.sources(), &streams, &HashMap::new(), rate, |_| { |
| 8282 | + frames | ||
| 8283 | + }); | ||
| 8107 | assert_eq!(rows.len(), 1); | 8284 | assert_eq!(rows.len(), 1); |
| 8108 | assert_eq!(rows[0].name, "test_tone.mp3"); | 8285 | assert_eq!(rows[0].name, "test_tone.mp3"); |
| 8109 | assert_eq!(rows[0].unusable, None, "the row is placeable"); | 8286 | assert_eq!(rows[0].unusable, None, "the row is placeable"); |
| @@ -8170,7 +8347,13 @@ mod tests { | |||
| 8170 | info(3, 0, 0, None, false), | 8347 | info(3, 0, 0, None, false), |
| 8171 | ], | 8348 | ], |
| 8172 | ); | 8349 | ); |
| 8173 | - let rows = library_rows(&sources, &streams, Some((44_100, 2)), |_| 90); | 8350 | + let rows = library_rows( |
| 8351 | + &sources, | ||
| 8352 | + &streams, | ||
| 8353 | + &HashMap::new(), | ||
| 8354 | + Some((44_100, 2)), | ||
| 8355 | + |_| 90, | ||
| 8356 | + ); | ||
| 8174 | assert_eq!( | 8357 | assert_eq!( |
| 8175 | rows.iter().map(|r| r.stream).collect::<Vec<_>>(), | 8358 | rows.iter().map(|r| r.stream).collect::<Vec<_>>(), |
| 8176 | [0, 1, 2, 3], | 8359 | [0, 1, 2, 3], |
| @@ -8208,7 +8391,7 @@ mod tests { | |||
| 8208 | PathBuf::from("/m/plain.mp4"), | 8391 | PathBuf::from("/m/plain.mp4"), |
| 8209 | vec![info(0, 44_100, 2, None, true)], | 8392 | vec![info(0, 44_100, 2, None, true)], |
| 8210 | ); | 8393 | ); |
| 8211 | - let rows = library_rows(&plain, &one, Some((44_100, 2)), |_| 90); | 8394 | + let rows = library_rows(&plain, &one, &HashMap::new(), Some((44_100, 2)), |_| 90); |
| 8212 | assert_eq!(rows.len(), 1); | 8395 | assert_eq!(rows.len(), 1); |
| 8213 | assert_eq!(rows[0].name, "plain.mp4"); | 8396 | assert_eq!(rows[0].name, "plain.mp4"); |
| 8214 | assert_eq!( | 8397 | assert_eq!( |
| @@ -8219,7 +8402,7 @@ mod tests { | |||
| 8219 | let mut silent = HashMap::new(); | 8402 | let mut silent = HashMap::new(); |
| 8220 | silent.insert(PathBuf::from("/m/plain.mp4"), Vec::new()); | 8403 | silent.insert(PathBuf::from("/m/plain.mp4"), Vec::new()); |
| 8221 | for probe in [silent, HashMap::new()] { | 8404 | for probe in [silent, HashMap::new()] { |
| 8222 | - let rows = library_rows(&plain, &probe, None, |_| 90); | 8405 | + let rows = library_rows(&plain, &probe, &HashMap::new(), None, |_| 90); |
| 8223 | assert_eq!(rows.len(), 1); | 8406 | assert_eq!(rows.len(), 1); |
| 8224 | assert_eq!(rows[0].name, "plain.mp4"); | 8407 | assert_eq!(rows[0].name, "plain.mp4"); |
| 8225 | assert_eq!(rows[0].detail, ""); | 8408 | assert_eq!(rows[0].detail, ""); |
| @@ -8237,7 +8420,13 @@ mod tests { | |||
| 8237 | PathBuf::from("/m/other.mp4"), | 8420 | PathBuf::from("/m/other.mp4"), |
| 8238 | vec![info(0, 44_100, 2, None, true)], | 8421 | vec![info(0, 44_100, 2, None, true)], |
| 8239 | ); | 8422 | ); |
| 8240 | - let rows = library_rows(&placed, &streams, Some((44_100, 2)), |_| 90); | 8423 | + let rows = library_rows( |
| 8424 | + &placed, | ||
| 8425 | + &streams, | ||
| 8426 | + &HashMap::new(), | ||
| 8427 | + Some((44_100, 2)), | ||
| 8428 | + |_| 90, | ||
| 8429 | + ); | ||
| 8241 | assert_eq!( | 8430 | assert_eq!( |
| 8242 | rows.iter() | 8431 | rows.iter() |
| 8243 | .map(|r| (file_name(&r.path), r.stream)) | 8432 | .map(|r| (file_name(&r.path), r.stream)) |
| @@ -8640,7 +8829,7 @@ mod tests { | |||
| 8640 | ); | 8829 | ); |
| 8641 | let streams = HashMap::new(); | 8830 | let streams = HashMap::new(); |
| 8642 | let rows = |session: &PlaybackSession| { | 8831 | let rows = |session: &PlaybackSession| { |
| 8643 | - library_rows(session.sources(), &streams, None, |_| 0).len() | 8832 | + library_rows(session.sources(), &streams, &HashMap::new(), None, |_| 0).len() |
| 8644 | }; | 8833 | }; |
| 8645 | assert_eq!(rows(&session), 2, "a row per source"); | 8834 | assert_eq!(rows(&session), 2, "a row per source"); |
| 8646 | 8835 | ||
| @@ -10033,7 +10222,9 @@ mod tests { | |||
| 10033 | // a title and a status line. | 10222 | // a title and a status line. |
| 10034 | let title = 17.; | 10223 | let title = 17.; |
| 10035 | let status = 28.; | 10224 | let status = 28.; |
| 10036 | - let fixed = 17.; | 10225 | + // Two lines of 11 px: the format line carries the encoders an export |
| 10226 | + // would open, which is what takes it past one at this width. | ||
| 10227 | + let fixed = 30.; | ||
| 10037 | let gaps = 4. * 2.; | 10228 | let gaps = 4. * 2.; |
| 10038 | let padding = 24.; | 10229 | let padding = 24.; |
| 10039 | assert!( | 10230 | assert!( |
| @@ -10581,6 +10772,8 @@ fn main() { | |||
| 10581 | waves: HashMap::new(), | 10772 | waves: HashMap::new(), |
| 10582 | streams: HashMap::new(), | 10773 | streams: HashMap::new(), |
| 10583 | sizes: HashMap::new(), | 10774 | sizes: HashMap::new(), |
| 10775 | + decoders: HashMap::new(), | ||
| 10776 | + export_seat: None, | ||
| 10584 | clipboard: None, | 10777 | clipboard: None, |
| 10585 | scrubbing: false, | 10778 | scrubbing: false, |
| 10586 | trim: None, | 10779 | trim: None, |
crates/engine/src/decode.rsmodified+116 -1
| @@ -3,7 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | use std::path::{Path, PathBuf}; | 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::sync::Arc; | 5 | use std::sync::Arc; |
| 6 | -use std::sync::atomic::{AtomicBool, Ordering}; | 6 | +use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; |
| 7 | use std::sync::mpsc::{Receiver, SyncSender, sync_channel}; | 7 | use std::sync::mpsc::{Receiver, SyncSender, sync_channel}; |
| 8 | use std::thread; | 8 | use std::thread; |
| 9 | 9 | ||
| @@ -25,6 +25,106 @@ pub struct Frame { | |||
| 25 | pub bgra: Vec<u8>, | 25 | pub bgra: Vec<u8>, |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | +/// Which decoder a source is really running on. Read-only introspection: it | ||
| 29 | +/// decides nothing, it *reports* what [`DecodeSession::open_worker`] chose -- | ||
| 30 | +/// a hardware session that opened and then failed before its first picture | ||
| 31 | +/// falls back to software and this says so, because it is written where the | ||
| 32 | +/// fallback happens rather than where it was hoped for. | ||
| 33 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] | ||
| 34 | +pub enum Backend { | ||
| 35 | + /// The worker has not reached its decoder yet: the plugin's VA-API init is | ||
| 36 | + /// ~90 ms, and this is what a caller reads in that window. | ||
| 37 | + #[default] | ||
| 38 | + Opening, | ||
| 39 | + /// The VA-API plugin (`libengine_hw.so`). | ||
| 40 | + Hardware, | ||
| 41 | + /// `rusty_h264`, in this process. | ||
| 42 | + Software, | ||
| 43 | + /// A still image: one `image` decode, no stream and no decoder to pick. | ||
| 44 | + Still, | ||
| 45 | + /// A gap: black frames, nothing decoded at all. | ||
| 46 | + Gap, | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +impl Backend { | ||
| 50 | + /// Short enough for a library row and a transport line. | ||
| 51 | + pub fn label(self) -> &'static str { | ||
| 52 | + match self { | ||
| 53 | + Self::Opening => "opening", | ||
| 54 | + Self::Hardware => "HW", | ||
| 55 | + Self::Software => "SW", | ||
| 56 | + Self::Still => "still", | ||
| 57 | + Self::Gap => "gap", | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + fn code(self) -> u8 { | ||
| 62 | + match self { | ||
| 63 | + Self::Opening => 0, | ||
| 64 | + Self::Hardware => 1, | ||
| 65 | + Self::Software => 2, | ||
| 66 | + Self::Still => 3, | ||
| 67 | + Self::Gap => 4, | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + fn from_code(code: u8) -> Self { | ||
| 72 | + match code { | ||
| 73 | + 1 => Self::Hardware, | ||
| 74 | + 2 => Self::Software, | ||
| 75 | + 3 => Self::Still, | ||
| 76 | + 4 => Self::Gap, | ||
| 77 | + _ => Self::Opening, | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +/// Where a worker publishes the decoder it opened, for whoever holds the other | ||
| 83 | +/// end of its channel. One atomic byte, written once at open (and once more if | ||
| 84 | +/// hardware hands the range back to software), read whenever a caller repaints | ||
| 85 | +/// -- never per frame on either side. | ||
| 86 | +#[derive(Clone, Default)] | ||
| 87 | +pub struct BackendCell(Arc<AtomicU8>); | ||
| 88 | + | ||
| 89 | +impl BackendCell { | ||
| 90 | + fn new(backend: Backend) -> Self { | ||
| 91 | + Self(Arc::new(AtomicU8::new(backend.code()))) | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + fn set(&self, backend: Backend) { | ||
| 95 | + self.0.store(backend.code(), Ordering::Relaxed); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + pub fn get(&self) -> Backend { | ||
| 99 | + Backend::from_code(self.0.load(Ordering::Relaxed)) | ||
| 100 | + } | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +/// Which decoder `path` would open, decided exactly as [`DecodeSession::open_worker`] | ||
| 104 | +/// decides it: the same `VE_SW` pin and the same plugin probe against this very | ||
| 105 | +/// file. What lets a front-end name the decoder *before* anything plays. | ||
| 106 | +/// | ||
| 107 | +/// The codec is `None` for a still image, which has no coded stream. A stream | ||
| 108 | +/// no decoder here can take is an `Err` naming the plugin, exactly as opening | ||
| 109 | +/// it would be -- the same refusal, one VA-API init earlier. | ||
| 110 | +/// | ||
| 111 | +/// Costs that one init (~90 ms) for a stream the plugin takes: ask it once per | ||
| 112 | +/// file, off a render thread, and keep the answer. | ||
| 113 | +pub fn probe(path: &Path) -> crate::Result<(Option<Codec>, Backend)> { | ||
| 114 | + if crate::is_image(path) { | ||
| 115 | + return Ok((None, Backend::Still)); | ||
| 116 | + } | ||
| 117 | + let path = path.to_path_buf(); | ||
| 118 | + let (meta, _demuxer) = Demuxer::open(&path)?; | ||
| 119 | + if open_hw(&path, 0).is_some() { | ||
| 120 | + return Ok((Some(meta.codec), Backend::Hardware)); | ||
| 121 | + } | ||
| 122 | + if meta.codec != Codec::H264 { | ||
| 123 | + return Err(meta.codec.needs_plugin().into()); | ||
| 124 | + } | ||
| 125 | + Ok((Some(meta.codec), Backend::Software)) | ||
| 126 | +} | ||
| 127 | + | ||
| 28 | /// A running decode worker: the flag that stops it, and the handle to wait on. | 128 | /// A running decode worker: the flag that stops it, and the handle to wait on. |
| 29 | /// | 129 | /// |
| 30 | /// Dropping one cancels *and joins*. A worker abandoned mid-`vaInitialize` | 130 | /// Dropping one cancels *and joins*. A worker abandoned mid-`vaInitialize` |
| @@ -85,6 +185,9 @@ impl Drop for Worker { | |||
| 85 | pub(crate) struct FrameStream { | 185 | pub(crate) struct FrameStream { |
| 86 | pub(crate) frames: Receiver<Frame>, | 186 | pub(crate) frames: Receiver<Frame>, |
| 87 | pub(crate) worker: Worker, | 187 | pub(crate) worker: Worker, |
| 188 | + /// What this stream's pictures are being decoded by. Dropped last and read | ||
| 189 | + /// by the caller, so it outlives nothing and blocks nothing. | ||
| 190 | + pub(crate) backend: BackendCell, | ||
| 88 | } | 191 | } |
| 89 | 192 | ||
| 90 | pub struct DecodeSession; | 193 | pub struct DecodeSession; |
| @@ -152,6 +255,7 @@ impl DecodeSession { | |||
| 152 | cancel, | 255 | cancel, |
| 153 | handle: None, | 256 | handle: None, |
| 154 | }, | 257 | }, |
| 258 | + backend: BackendCell::new(Backend::Gap), | ||
| 155 | }; | 259 | }; |
| 156 | } | 260 | } |
| 157 | let worker_cancel = Arc::clone(&cancel); | 261 | let worker_cancel = Arc::clone(&cancel); |
| @@ -180,6 +284,7 @@ impl DecodeSession { | |||
| 180 | FrameStream { | 284 | FrameStream { |
| 181 | frames: rx, | 285 | frames: rx, |
| 182 | worker: Worker { cancel, handle }, | 286 | worker: Worker { cancel, handle }, |
| 287 | + backend: BackendCell::new(Backend::Gap), | ||
| 183 | } | 288 | } |
| 184 | } | 289 | } |
| 185 | 290 | ||
| @@ -209,6 +314,7 @@ impl DecodeSession { | |||
| 209 | cancel, | 314 | cancel, |
| 210 | handle: None, | 315 | handle: None, |
| 211 | }, | 316 | }, |
| 317 | + backend: BackendCell::new(Backend::Still), | ||
| 212 | }); | 318 | }); |
| 213 | } | 319 | } |
| 214 | // The one conversion, on this thread: it is a few milliseconds and the | 320 | // The one conversion, on this thread: it is a few milliseconds and the |
| @@ -245,6 +351,7 @@ impl DecodeSession { | |||
| 245 | Ok(FrameStream { | 351 | Ok(FrameStream { |
| 246 | frames: rx, | 352 | frames: rx, |
| 247 | worker: Worker { cancel, handle }, | 353 | worker: Worker { cancel, handle }, |
| 354 | + backend: BackendCell::new(Backend::Still), | ||
| 248 | }) | 355 | }) |
| 249 | } | 356 | } |
| 250 | 357 | ||
| @@ -285,6 +392,9 @@ impl DecodeSession { | |||
| 285 | // decoder run ahead of the display without limit. | 392 | // decoder run ahead of the display without limit. |
| 286 | let (tx, rx) = sync_channel(2); | 393 | let (tx, rx) = sync_channel(2); |
| 287 | let cancel = Arc::new(AtomicBool::new(false)); | 394 | let cancel = Arc::new(AtomicBool::new(false)); |
| 395 | + // Written by the worker below the moment it knows, so a caller reading | ||
| 396 | + // it sees what opened rather than what was hoped for. | ||
| 397 | + let backend = BackendCell::default(); | ||
| 288 | if end_frame <= start_frame { | 398 | if end_frame <= start_frame { |
| 289 | // Nothing to decode: dropping `tx` here closes the channel cleanly, | 399 | // Nothing to decode: dropping `tx` here closes the channel cleanly, |
| 290 | // so the caller sees an immediate end of stream rather than an error. | 400 | // so the caller sees an immediate end of stream rather than an error. |
| @@ -296,10 +406,12 @@ impl DecodeSession { | |||
| 296 | cancel, | 406 | cancel, |
| 297 | handle: None, | 407 | handle: None, |
| 298 | }, | 408 | }, |
| 409 | + backend, | ||
| 299 | }, | 410 | }, |
| 300 | )); | 411 | )); |
| 301 | } | 412 | } |
| 302 | let worker_cancel = Arc::clone(&cancel); | 413 | let worker_cancel = Arc::clone(&cancel); |
| 414 | + let worker_backend = backend.clone(); | ||
| 303 | let handle = thread::Builder::new() | 415 | let handle = thread::Builder::new() |
| 304 | .name("decode".into()) | 416 | .name("decode".into()) |
| 305 | .spawn(move || { | 417 | .spawn(move || { |
| @@ -320,6 +432,7 @@ impl DecodeSession { | |||
| 320 | return; | 432 | return; |
| 321 | } | 433 | } |
| 322 | eprintln!("decode backend: hardware (VA-API plugin)"); | 434 | eprintln!("decode backend: hardware (VA-API plugin)"); |
| 435 | + worker_backend.set(Backend::Hardware); | ||
| 323 | if run_hw(hw, &tx, start_frame, end_frame, &mut render, &worker_cancel) { | 436 | if run_hw(hw, &tx, start_frame, end_frame, &mut render, &worker_cancel) { |
| 324 | return; | 437 | return; |
| 325 | } | 438 | } |
| @@ -334,6 +447,7 @@ impl DecodeSession { | |||
| 334 | return; | 447 | return; |
| 335 | } | 448 | } |
| 336 | eprintln!("decode backend: software (rusty_h264)"); | 449 | eprintln!("decode backend: software (rusty_h264)"); |
| 450 | + worker_backend.set(Backend::Software); | ||
| 337 | run( | 451 | run( |
| 338 | demuxer, | 452 | demuxer, |
| 339 | tx, | 453 | tx, |
| @@ -351,6 +465,7 @@ impl DecodeSession { | |||
| 351 | cancel, | 465 | cancel, |
| 352 | handle: Some(handle), | 466 | handle: Some(handle), |
| 353 | }, | 467 | }, |
| 468 | + backend, | ||
| 354 | }, | 469 | }, |
| 355 | )) | 470 | )) |
| 356 | } | 471 | } |
crates/engine/src/export.rsmodified+142 -20
| @@ -146,6 +146,10 @@ struct Shared { | |||
| 146 | cancel: AtomicBool, | 146 | cancel: AtomicBool, |
| 147 | finished: AtomicBool, | 147 | finished: AtomicBool, |
| 148 | outcome: Mutex<Option<crate::Result<()>>>, | 148 | outcome: Mutex<Option<crate::Result<()>>>, |
| 149 | + /// The encoders this job really opened, published the moment they are | ||
| 150 | + /// chosen and never changed after -- one seat for the whole file, which is | ||
| 151 | + /// what [`Enc`] states. `None` until then. | ||
| 152 | + encoders: Mutex<Option<String>>, | ||
| 149 | } | 153 | } |
| 150 | 154 | ||
| 151 | /// A running export. Poll [`is_finished`](ExportHandle::is_finished) once per | 155 | /// A running export. Poll [`is_finished`](ExportHandle::is_finished) once per |
| @@ -174,6 +178,13 @@ impl ExportHandle { | |||
| 174 | self.shared.finished.load(Ordering::Acquire) | 178 | self.shared.finished.load(Ordering::Acquire) |
| 175 | } | 179 | } |
| 176 | 180 | ||
| 181 | + /// What this export is encoding with -- the video seat and what the sound | ||
| 182 | + /// is doing -- as the worker chose them, hardware fallback included. | ||
| 183 | + /// `None` for the first instants of a job, before the encoder is open. | ||
| 184 | + pub fn encoders(&self) -> Option<String> { | ||
| 185 | + self.shared.encoders.lock().unwrap().clone() | ||
| 186 | + } | ||
| 187 | + | ||
| 177 | /// The outcome, once — taken out of the handle, so a caller that already | 188 | /// The outcome, once — taken out of the handle, so a caller that already |
| 178 | /// reported it sees `None` afterwards. `None` while the export is running. | 189 | /// reported it sees `None` afterwards. `None` while the export is running. |
| 179 | pub fn result(&self) -> Option<crate::Result<()>> { | 190 | pub fn result(&self) -> Option<crate::Result<()>> { |
| @@ -197,6 +208,7 @@ pub fn start( | |||
| 197 | cancel: AtomicBool::new(false), | 208 | cancel: AtomicBool::new(false), |
| 198 | finished: AtomicBool::new(false), | 209 | finished: AtomicBool::new(false), |
| 199 | outcome: Mutex::new(None), | 210 | outcome: Mutex::new(None), |
| 211 | + encoders: Mutex::new(None), | ||
| 200 | }); | 212 | }); |
| 201 | let worker = Arc::clone(&shared); | 213 | let worker = Arc::clone(&shared); |
| 202 | let out = out.to_path_buf(); | 214 | let out = out.to_path_buf(); |
| @@ -261,6 +273,103 @@ fn has_picture(project: &Project) -> bool { | |||
| 261 | .any(|lane| lane.kind == LaneKind::Video && !project.lane(lane).is_empty()) | 273 | .any(|lane| lane.kind == LaneKind::Video && !project.lane(lane).is_empty()) |
| 262 | } | 274 | } |
| 263 | 275 | ||
| 276 | +/// Whether any audio lane holds anything, [`has_picture`]'s pair: what tells a | ||
| 277 | +/// timeline whose export will carry sound from one whose file has none. | ||
| 278 | +fn has_sound(project: &Project) -> bool { | ||
| 279 | + project | ||
| 280 | + .lanes() | ||
| 281 | + .into_iter() | ||
| 282 | + .any(|lane| lane.kind == LaneKind::Audio && !project.lane(lane).is_empty()) | ||
| 283 | +} | ||
| 284 | + | ||
| 285 | +/// Whether any equalizer on the sound is doing something -- the one edit a | ||
| 286 | +/// packet copy cannot carry, so an mp4 whose lane has one is decoded and | ||
| 287 | +/// re-encoded ([`encode_audio`]) instead of copied. Asked of every audio lane: | ||
| 288 | +/// the copy path has exactly one by the time it asks, and [`planned_audio`] is | ||
| 289 | +/// asked before that is settled. | ||
| 290 | +fn equalized(project: &Project) -> bool { | ||
| 291 | + project.audio_lanes().into_iter().any(|lane| { | ||
| 292 | + (0..project.lane(lane).len()) | ||
| 293 | + .any(|idx| project.eq_of(lane, idx).is_some_and(|eq| !eq.is_identity())) | ||
| 294 | + }) | ||
| 295 | +} | ||
| 296 | + | ||
| 297 | +/// The bitrate an export codes at: the caller's number through the same clamp | ||
| 298 | +/// as the computed one, for the reason [`Enc::open`] states. | ||
| 299 | +fn bitrate_of(meta: &VideoMeta, settings: &ExportSettings) -> u64 { | ||
| 300 | + settings | ||
| 301 | + .bitrate | ||
| 302 | + .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)) | ||
| 303 | +} | ||
| 304 | + | ||
| 305 | +/// The hardware seat for these settings, opened exactly as the export opens it | ||
| 306 | +/// -- the same pins, the same AV1 opt-in, the same dimensions a driver may | ||
| 307 | +/// refuse -- or `None` where the software encoder takes the file. The single | ||
| 308 | +/// place that choice is made, which is what lets [`planned_video`] *measure* it | ||
| 309 | +/// instead of promising it. | ||
| 310 | +fn hw_seat(meta: &VideoMeta, settings: &ExportSettings) -> Option<HwEncoder> { | ||
| 311 | + if settings.force_sw || forced("VE_SW_ENC") { | ||
| 312 | + return None; | ||
| 313 | + } | ||
| 314 | + let (fps_num, fps_den) = crate::mux::frame_timing(meta.frame_rate).ok()?; | ||
| 315 | + let bitrate = bitrate_of(meta, settings); | ||
| 316 | + match settings.format { | ||
| 317 | + // Opt-in only, for the reason [`Enc::open_av1`] states in full. | ||
| 318 | + Format::Av1 if !forced("VE_HW_AV1") => None, | ||
| 319 | + Format::Av1 => HwEncoder::open_av1(meta.width, meta.height, fps_num, fps_den, bitrate), | ||
| 320 | + _ => HwEncoder::open(meta.width, meta.height, fps_num, fps_den, bitrate), | ||
| 321 | + } | ||
| 322 | +} | ||
| 323 | + | ||
| 324 | +/// How a front-end names a video seat: which of the two encoders has the file, | ||
| 325 | +/// and which library that is. Not the codec -- a caller shows this beside the | ||
| 326 | +/// format it picked, and `rav1e` names AV1 as `rusty_h264` names H.264. One | ||
| 327 | +/// place, so what a card says before an export and what its progress line says | ||
| 328 | +/// during one cannot drift apart. | ||
| 329 | +fn video_label(format: Format, hw: bool) -> &'static str { | ||
| 330 | + match (format, hw) { | ||
| 331 | + (Format::Av1, true) => "HW encode (VA-API)", | ||
| 332 | + (Format::Av1, false) => "SW encode (rav1e)", | ||
| 333 | + (_, true) => "HW encode (VA-API)", | ||
| 334 | + (_, false) => "SW encode (rusty_h264)", | ||
| 335 | + } | ||
| 336 | +} | ||
| 337 | + | ||
| 338 | +/// What the sound is written by. Every audio encoder here is software -- there | ||
| 339 | +/// is no hardware AAC, PCM or FLAC seat -- so this names *which* one, and | ||
| 340 | +/// whether an mp4 escapes encoding altogether by copying the packets it was | ||
| 341 | +/// given. Empty for AV1, which carries no audio at all: a caller joins this to | ||
| 342 | +/// a line that already says so, and repeating it would cost a line of card. | ||
| 343 | +fn audio_label(project: &Project, format: Format, sound: bool) -> &'static str { | ||
| 344 | + match format { | ||
| 345 | + Format::Av1 => "", | ||
| 346 | + _ if !sound => "no sound to write", | ||
| 347 | + Format::Mp4 if equalized(project) => "AAC · SW encode (rusty_aac)", | ||
| 348 | + Format::Mp4 => "AAC copy", | ||
| 349 | + Format::Wav => "PCM · SW (hound)", | ||
| 350 | + Format::Flac => "FLAC · SW (flacenc)", | ||
| 351 | + } | ||
| 352 | +} | ||
| 353 | + | ||
| 354 | +/// What [`start`] would write this timeline's sound with, before one is | ||
| 355 | +/// started. Pure: no probe and no file opened, so a card may ask it per repaint. | ||
| 356 | +pub fn planned_audio(project: &Project, format: Format) -> &'static str { | ||
| 357 | + audio_label(project, format, has_sound(project)) | ||
| 358 | +} | ||
| 359 | + | ||
| 360 | +/// What [`start`] would encode the picture with, probed the way the export | ||
| 361 | +/// probes it -- the very encoder is opened and closed again -- so this is a | ||
| 362 | +/// measurement and not a promise. `None` for a format that carries no picture. | ||
| 363 | +/// | ||
| 364 | +/// Costs that open (~100 ms here): ask it off a render thread and keep the | ||
| 365 | +/// answer until the format, the resolution or the bitrate changes. | ||
| 366 | +pub fn planned_video(meta: &VideoMeta, settings: &ExportSettings) -> Option<&'static str> { | ||
| 367 | + settings | ||
| 368 | + .format | ||
| 369 | + .has_video() | ||
| 370 | + .then(|| video_label(settings.format, hw_seat(meta, settings).is_some())) | ||
| 371 | +} | ||
| 372 | + | ||
| 264 | fn settle(shared: &Shared, result: crate::Result<()>) { | 373 | fn settle(shared: &Shared, result: crate::Result<()>) { |
| 265 | *shared.outcome.lock().unwrap() = Some(result); | 374 | *shared.outcome.lock().unwrap() = Some(result); |
| 266 | // Published last: a caller that sees the flag is guaranteed the outcome. | 375 | // Published last: a caller that sees the flag is guaranteed the outcome. |
| @@ -367,9 +476,7 @@ fn copy_audio( | |||
| 367 | // only this case is: a timeline nobody has equalized still leaves through | 476 | // only this case is: a timeline nobody has equalized still leaves through |
| 368 | // the copy below, packet for packet, so no passthrough quietly becomes a | 477 | // the copy below, packet for packet, so no passthrough quietly becomes a |
| 369 | // generation of loss. | 478 | // generation of loss. |
| 370 | - let equalized = (0..project.lane(lane).len()) | 479 | + if equalized(project) { |
| 371 | - .any(|idx| project.eq_of(lane, idx).is_some_and(|eq| !eq.is_identity())); | ||
| 372 | - if equalized { | ||
| 373 | return encode_audio(project, meta); | 480 | return encode_audio(project, meta); |
| 374 | } | 481 | } |
| 375 | AudioSession::copy_multi_streams(&project.audio_sources(), segments) | 482 | AudioSession::copy_multi_streams(&project.audio_sources(), segments) |
| @@ -495,6 +602,18 @@ fn run( | |||
| 495 | .collect(); | 602 | .collect(); |
| 496 | 603 | ||
| 497 | let mut encoder = Enc::open(meta, settings)?; | 604 | let mut encoder = Enc::open(meta, settings)?; |
| 605 | + // What this file is really being written by, for a progress line to name. | ||
| 606 | + // Published *after* the seat is open, so a hardware encoder the driver | ||
| 607 | + // refused reads as the software one that took over rather than as the hope | ||
| 608 | + // it replaced -- and the sound says whether it was copied or encoded, which | ||
| 609 | + // `copy_audio` decided a few lines up. | ||
| 610 | + *shared.encoders.lock().unwrap() = Some( | ||
| 611 | + match audio_label(project, settings.format, audio.is_some()) { | ||
| 612 | + // AV1: no sound in the file and none to name. | ||
| 613 | + "" => encoder.label().to_string(), | ||
| 614 | + sound => format!("{} · {sound}", encoder.label()), | ||
| 615 | + }, | ||
| 616 | + ); | ||
| 498 | let mut muxer = None; | 617 | let mut muxer = None; |
| 499 | let mut done = 0u32; | 618 | let mut done = 0u32; |
| 500 | let black = Black::new(meta); | 619 | let black = Black::new(meta); |
| @@ -734,6 +853,9 @@ fn run_audio( | |||
| 734 | else { | 853 | else { |
| 735 | return Err("this timeline has no audio to export".into()); | 854 | return Err("this timeline has no audio to export".into()); |
| 736 | }; | 855 | }; |
| 856 | + // The picture path's line, for a file that is sound alone: there is sound | ||
| 857 | + // by the line above, so this names the encoder writing it. | ||
| 858 | + *shared.encoders.lock().unwrap() = Some(audio_label(project, format, true).to_string()); | ||
| 737 | let channels = usize::from(audio.channels); | 859 | let channels = usize::from(audio.channels); |
| 738 | let frames = (f64::from(project.timeline_frames()) / meta.frame_rate | 860 | let frames = (f64::from(project.timeline_frames()) / meta.frame_rate |
| 739 | * f64::from(audio.sample_rate)) | 861 | * f64::from(audio.sample_rate)) |
| @@ -988,6 +1110,18 @@ enum Enc { | |||
| 988 | } | 1110 | } |
| 989 | 1111 | ||
| 990 | impl Enc { | 1112 | impl Enc { |
| 1113 | + /// Which seat this *is*, said the way [`video_label`] says it -- so the | ||
| 1114 | + /// name a running export shows is the name the card showed, or the honest | ||
| 1115 | + /// difference where the probe and the open disagreed. | ||
| 1116 | + fn label(&self) -> &'static str { | ||
| 1117 | + match self { | ||
| 1118 | + Self::Hw(_) => video_label(Format::Mp4, true), | ||
| 1119 | + Self::Sw { .. } => video_label(Format::Mp4, false), | ||
| 1120 | + Self::Av1Hw(_) => video_label(Format::Av1, true), | ||
| 1121 | + Self::Av1Sw { .. } => video_label(Format::Av1, false), | ||
| 1122 | + } | ||
| 1123 | + } | ||
| 1124 | + | ||
| 991 | fn open(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Self> { | 1125 | fn open(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Self> { |
| 992 | if settings.format == Format::Av1 { | 1126 | if settings.format == Format::Av1 { |
| 993 | return Self::open_av1(meta, settings); | 1127 | return Self::open_av1(meta, settings); |
| @@ -995,18 +1129,13 @@ impl Enc { | |||
| 995 | // A caller's number goes through the same clamp as the computed one: a | 1129 | // A caller's number goes through the same clamp as the computed one: a |
| 996 | // zero bitrate switches the software encoder's lookahead on, which would | 1130 | // zero bitrate switches the software encoder's lookahead on, which would |
| 997 | // break the one-picture-per-call contract `encode` documents below. | 1131 | // break the one-picture-per-call contract `encode` documents below. |
| 998 | - let bitrate = settings | 1132 | + let bitrate = bitrate_of(meta, settings); |
| 999 | - .bitrate | ||
| 1000 | - .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)); | ||
| 1001 | // The plugin wants an exact rational, and the muxer already picks one | 1133 | // The plugin wants an exact rational, and the muxer already picks one |
| 1002 | // that is exact at every rate we can read -- `fps * 1000 / 1000` would | 1134 | // that is exact at every rate we can read -- `fps * 1000 / 1000` would |
| 1003 | // hand 24000/1001 over as a rounded 23.976, which is the same | 1135 | // hand 24000/1001 over as a rounded 23.976, which is the same |
| 1004 | // truncation the container timing had. | 1136 | // truncation the container timing had. |
| 1005 | - let (fps_num, fps_den) = crate::mux::frame_timing(meta.frame_rate)?; | 1137 | + crate::mux::frame_timing(meta.frame_rate)?; |
| 1006 | - if !settings.force_sw | 1138 | + if let Some(hw) = hw_seat(meta, settings) { |
| 1007 | - && !forced("VE_SW_ENC") | ||
| 1008 | - && let Some(hw) = HwEncoder::open(meta.width, meta.height, fps_num, fps_den, bitrate) | ||
| 1009 | - { | ||
| 1010 | eprintln!("export encoder: hardware (VA-API plugin)"); | 1139 | eprintln!("export encoder: hardware (VA-API plugin)"); |
| 1011 | return Ok(Self::Hw(hw)); | 1140 | return Ok(Self::Hw(hw)); |
| 1012 | } | 1141 | } |
| @@ -1047,19 +1176,12 @@ impl Enc { | |||
| 1047 | /// cros-codecs release that fixes it) plus a probe encode of one frame at | 1176 | /// cros-codecs release that fixes it) plus a probe encode of one frame at |
| 1048 | /// open, after which this can prefer hardware the way H.264 does. | 1177 | /// open, after which this can prefer hardware the way H.264 does. |
| 1049 | fn open_av1(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Self> { | 1178 | fn open_av1(meta: &VideoMeta, settings: &ExportSettings) -> crate::Result<Self> { |
| 1050 | - let bitrate = settings | 1179 | + let bitrate = bitrate_of(meta, settings); |
| 1051 | - .bitrate | ||
| 1052 | - .map_or_else(|| bitrate_for(meta), |b| b.clamp(MIN_BITRATE, MAX_BITRATE)); | ||
| 1053 | let (fps_num, fps_den) = crate::mux::frame_timing(meta.frame_rate)?; | 1180 | let (fps_num, fps_den) = crate::mux::frame_timing(meta.frame_rate)?; |
| 1054 | // Two seconds between keyframes, as the H.264 seat does: a seek may only | 1181 | // Two seconds between keyframes, as the H.264 seat does: a seek may only |
| 1055 | // land on one, and this is what a cluster of the Matroska file is. | 1182 | // land on one, and this is what a cluster of the Matroska file is. |
| 1056 | let gop = (meta.frame_rate * 2.0).round().max(1.0) as u64; | 1183 | let gop = (meta.frame_rate * 2.0).round().max(1.0) as u64; |
| 1057 | - if forced("VE_HW_AV1") | 1184 | + if let Some(hw) = hw_seat(meta, settings) { |
| 1058 | - && !settings.force_sw | ||
| 1059 | - && !forced("VE_SW_ENC") | ||
| 1060 | - && let Some(hw) = | ||
| 1061 | - HwEncoder::open_av1(meta.width, meta.height, fps_num, fps_den, bitrate) | ||
| 1062 | - { | ||
| 1063 | eprintln!("export encoder: hardware AV1 (VA-API plugin)"); | 1185 | eprintln!("export encoder: hardware AV1 (VA-API plugin)"); |
| 1064 | return Ok(Self::Av1Hw(hw)); | 1186 | return Ok(Self::Av1Hw(hw)); |
| 1065 | } | 1187 | } |
crates/engine/src/playback.rsmodified+29 -1
| @@ -22,7 +22,7 @@ use crate::ao::AoSession; | |||
| 22 | use crate::audio::{AudioChunk, AudioSession}; | 22 | use crate::audio::{AudioChunk, AudioSession}; |
| 23 | use crate::clock::{ClockSource, PlaybackClock}; | 23 | use crate::clock::{ClockSource, PlaybackClock}; |
| 24 | use crate::color::ColorParams; | 24 | use crate::color::ColorParams; |
| 25 | -use crate::decode::{DecodeSession, Frame, Worker}; | 25 | +use crate::decode::{Backend, BackendCell, DecodeSession, Frame, Worker}; |
| 26 | use crate::demux::{Codec, Demuxer, VideoMeta}; | 26 | use crate::demux::{Codec, Demuxer, VideoMeta}; |
| 27 | use crate::eq::EqParams; | 27 | use crate::eq::EqParams; |
| 28 | use crate::project::{Clip, Edge, Lane, LaneKind, Project, Rate, Source, Span, Speed}; | 28 | use crate::project::{Clip, Edge, Lane, LaneKind, Project, Rate, Source, Span, Speed}; |
| @@ -161,6 +161,11 @@ pub struct PlaybackSession { | |||
| 161 | frames: Receiver<Frame>, | 161 | frames: Receiver<Frame>, |
| 162 | /// The current decode worker. | 162 | /// The current decode worker. |
| 163 | worker: Worker, | 163 | worker: Worker, |
| 164 | + /// What that worker opened -- hardware, software, a still, a gap -- written | ||
| 165 | + /// by the worker itself and read by [`decode_backend`](Self::decode_backend). | ||
| 166 | + /// Replaced with the stream at every seek, so it always describes the | ||
| 167 | + /// pictures currently arriving and never the clip before them. | ||
| 168 | + backend: BackendCell, | ||
| 164 | /// Workers cancelled by a seek or a clip change, waiting to be reaped. | 169 | /// Workers cancelled by a seek or a clip change, waiting to be reaped. |
| 165 | /// Joining one inline costs whatever VA-API init it is still inside (98 ms | 170 | /// Joining one inline costs whatever VA-API init it is still inside (98 ms |
| 166 | /// measured worst case), which is a visible hitch on the caller's thread; | 171 | /// measured worst case), which is a visible hitch on the caller's thread; |
| @@ -274,6 +279,7 @@ impl PlaybackSession { | |||
| 274 | native: (meta.width, meta.height), | 279 | native: (meta.width, meta.height), |
| 275 | frames: stream.frames, | 280 | frames: stream.frames, |
| 276 | worker: stream.worker, | 281 | worker: stream.worker, |
| 282 | + backend: stream.backend, | ||
| 277 | retired: Vec::new(), | 283 | retired: Vec::new(), |
| 278 | clock: PlaybackClock::new(source), | 284 | clock: PlaybackClock::new(source), |
| 279 | audio, | 285 | audio, |
| @@ -371,6 +377,7 @@ impl PlaybackSession { | |||
| 371 | native: (width, height), | 377 | native: (width, height), |
| 372 | frames: stream.frames, | 378 | frames: stream.frames, |
| 373 | worker: stream.worker, | 379 | worker: stream.worker, |
| 380 | + backend: stream.backend, | ||
| 374 | retired: Vec::new(), | 381 | retired: Vec::new(), |
| 375 | // The song keeps the clock, as a video's own sound does -- and wall | 382 | // The song keeps the clock, as a video's own sound does -- and wall |
| 376 | // time keeps it on a machine with no device, where the picture | 383 | // time keeps it on a machine with no device, where the picture |
| @@ -446,6 +453,7 @@ impl PlaybackSession { | |||
| 446 | native: (meta.width, meta.height), | 453 | native: (meta.width, meta.height), |
| 447 | frames: stream.frames, | 454 | frames: stream.frames, |
| 448 | worker: stream.worker, | 455 | worker: stream.worker, |
| 456 | + backend: stream.backend, | ||
| 449 | retired: Vec::new(), | 457 | retired: Vec::new(), |
| 450 | clock: PlaybackClock::new(ClockSource::Wall), | 458 | clock: PlaybackClock::new(ClockSource::Wall), |
| 451 | audio: None, | 459 | audio: None, |
| @@ -681,6 +689,7 @@ impl PlaybackSession { | |||
| 681 | native, | 689 | native, |
| 682 | frames: stream.frames, | 690 | frames: stream.frames, |
| 683 | worker: stream.worker, | 691 | worker: stream.worker, |
| 692 | + backend: stream.backend, | ||
| 684 | retired: Vec::new(), | 693 | retired: Vec::new(), |
| 685 | clock: PlaybackClock::new(match audio { | 694 | clock: PlaybackClock::new(match audio { |
| 686 | Some(_) => ClockSource::Audio, | 695 | Some(_) => ClockSource::Audio, |
| @@ -708,6 +717,24 @@ impl PlaybackSession { | |||
| 708 | &self.meta | 717 | &self.meta |
| 709 | } | 718 | } |
| 710 | 719 | ||
| 720 | + /// What the pictures now arriving are being decoded by: the clip under the | ||
| 721 | + /// playhead, since one video worker feeds the composite at a time. An | ||
| 722 | + /// atomic load -- a front-end may ask it every repaint -- and it is what | ||
| 723 | + /// *opened*, so a clip whose hardware session fell back to software reads | ||
| 724 | + /// [`Backend::Software`] from the frame that fallback happened. | ||
| 725 | + pub fn decode_backend(&self) -> Backend { | ||
| 726 | + self.backend.get() | ||
| 727 | + } | ||
| 728 | + | ||
| 729 | + /// What an export of this timeline would encode the *sound* with, before | ||
| 730 | + /// one is started: the copy an mp4 normally makes, or the software encoder | ||
| 731 | + /// the format (or an equalized lane) forces. Pure -- no probe, no file | ||
| 732 | + /// opened -- so a card may ask it per repaint; the picture's half costs a | ||
| 733 | + /// plugin open and lives in [`crate::export::planned_video`]. | ||
| 734 | + pub fn planned_audio(&self, format: crate::export::Format) -> &'static str { | ||
| 735 | + crate::export::planned_audio(&self.project, format) | ||
| 736 | + } | ||
| 737 | + | ||
| 711 | /// Why this session plays silent although the file has sound -- an audio | 738 | /// Why this session plays silent although the file has sound -- an audio |
| 712 | /// track in a codec we cannot decode, or one the decoder refused. `None` | 739 | /// track in a codec we cannot decode, or one the decoder refused. `None` |
| 713 | /// when there is audio, and for a file that never had any. A front-end | 740 | /// when there is audio, and for a file that never had any. A front-end |
| @@ -932,6 +959,7 @@ impl PlaybackSession { | |||
| 932 | // then can it return and be reaped by a later sweep. Nothing here | 959 | // then can it return and be reaped by a later sweep. Nothing here |
| 933 | // joins, so this ordering costs the seek nothing. | 960 | // joins, so this ordering costs the seek nothing. |
| 934 | self.frames = stream.frames; | 961 | self.frames = stream.frames; |
| 962 | + self.backend = stream.backend; | ||
| 935 | self.retire(stream.worker); | 963 | self.retire(stream.worker); |
| 936 | } | 964 | } |
| 937 | self.span = span; | 965 | self.span = span; |
crates/engine/tests/backends.rsadded+187 -0
| @@ -0,0 +1,187 @@ | |||
| 1 | +//! Which decoder and which encoder a job really runs on -- the introspection a | ||
| 2 | +//! front-end shows before an operation and while it runs. | ||
| 3 | +//! | ||
| 4 | +//! The point of every assertion here is *truthfulness*, not a particular seat: | ||
| 5 | +//! this box may or may not have a working plugin, and a test that demanded | ||
| 6 | +//! hardware would only be measuring the machine. What must hold is that the | ||
| 7 | +//! answer given before an operation is the answer the operation then gives, and | ||
| 8 | +//! that neither of them ever names a decoder that could not have run (software | ||
| 9 | +//! HEVC, for one, does not exist here). | ||
| 10 | + | ||
| 11 | +use std::path::PathBuf; | ||
| 12 | +use std::time::{Duration, Instant}; | ||
| 13 | + | ||
| 14 | +use engine::Codec; | ||
| 15 | +use engine::decode::{Backend, probe}; | ||
| 16 | +use engine::export::{ExportSettings, Format}; | ||
| 17 | +use engine::{ExportHandle, PlaybackSession}; | ||
| 18 | + | ||
| 19 | +fn asset(name: &str) -> PathBuf { | ||
| 20 | + PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
| 21 | + .join("../../assets") | ||
| 22 | + .join(name) | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +fn out_path(name: &str, ext: &str) -> PathBuf { | ||
| 26 | + let path = std::env::temp_dir().join(format!("ve_backend_{name}_{}.{ext}", std::process::id())); | ||
| 27 | + let _ = std::fs::remove_file(&path); | ||
| 28 | + let _ = std::fs::remove_file(format!("{}.part", path.display())); | ||
| 29 | + path | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +/// A still has no coded stream and no decoder to pick, and it must say so | ||
| 33 | +/// rather than borrowing the picture path's answer. | ||
| 34 | +#[test] | ||
| 35 | +fn a_still_probes_as_a_still() { | ||
| 36 | + let (codec, backend) = probe(&asset("test_still.png")).expect("probe the still"); | ||
| 37 | + assert_eq!(codec, None, "an image has no coded stream to name"); | ||
| 38 | + assert_eq!(backend, Backend::Still); | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +/// H.264 has both seats, so the probe may legitimately answer either -- but it | ||
| 42 | +/// must answer one of them, and it must name the codec. | ||
| 43 | +#[test] | ||
| 44 | +fn an_h264_source_probes_as_one_of_the_two_seats() { | ||
| 45 | + let (codec, backend) = probe(&asset("test_baseline.mp4")).expect("probe the baseline fixture"); | ||
| 46 | + assert_eq!(codec, Some(Codec::H264)); | ||
| 47 | + assert!( | ||
| 48 | + matches!(backend, Backend::Hardware | Backend::Software), | ||
| 49 | + "a probe that returned must have picked a seat, got {backend:?}" | ||
| 50 | + ); | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +/// HEVC has no software decoder here at all. A box with the plugin answers | ||
| 54 | +/// hardware; a box without it is refused by name -- and *never* told that | ||
| 55 | +/// `rusty_h264` will take it, which is the one answer that would be a lie. | ||
| 56 | +#[test] | ||
| 57 | +fn hevc_probes_as_hardware_or_as_a_refusal() { | ||
| 58 | + match probe(&asset("test_hevc.mkv")) { | ||
| 59 | + Ok((codec, backend)) => { | ||
| 60 | + assert_eq!(codec, Some(Codec::Hevc)); | ||
| 61 | + assert_eq!(backend, Backend::Hardware, "there is no software HEVC here"); | ||
| 62 | + } | ||
| 63 | + Err(e) => assert!( | ||
| 64 | + e.to_string().contains("plugin"), | ||
| 65 | + "a refusal names the plugin, got {e}" | ||
| 66 | + ), | ||
| 67 | + } | ||
| 68 | +} | ||
| 69 | + | ||
| 70 | +/// The running session reports the decoder its *worker* opened, and it agrees | ||
| 71 | +/// with what the probe said before anything played. This is the whole point of | ||
| 72 | +/// the cell: the worker writes it where the fallback happens, so a hardware | ||
| 73 | +/// session that dropped to software could only be seen here. | ||
| 74 | +#[test] | ||
| 75 | +fn a_playing_session_reports_the_seat_the_probe_named() { | ||
| 76 | + let path = asset("test_baseline.mp4"); | ||
| 77 | + let (_codec, probed) = probe(&path).expect("probe the baseline fixture"); | ||
| 78 | + let session = PlaybackSession::open(&path).expect("open the fixture"); | ||
| 79 | + let started = Instant::now(); | ||
| 80 | + loop { | ||
| 81 | + let live = session.decode_backend(); | ||
| 82 | + if live != Backend::Opening { | ||
| 83 | + assert_eq!(live, probed, "the running seat is not the probed one"); | ||
| 84 | + break; | ||
| 85 | + } | ||
| 86 | + assert!( | ||
| 87 | + started.elapsed() < Duration::from_secs(20), | ||
| 88 | + "the worker never published a decode backend" | ||
| 89 | + ); | ||
| 90 | + std::thread::sleep(Duration::from_millis(10)); | ||
| 91 | + } | ||
| 92 | +} | ||
| 93 | + | ||
| 94 | +/// What the export card says before the button and what the progress line says | ||
| 95 | +/// during the job are the same two answers, from the same two functions: the | ||
| 96 | +/// picture's seat is probed by opening the very encoder the export opens, and | ||
| 97 | +/// the sound's is decided outright. A drift between them is a front-end lying | ||
| 98 | +/// about the machine, which is exactly what this whole surface is for. | ||
| 99 | +#[test] | ||
| 100 | +fn the_planned_encoders_are_the_ones_the_job_opens() { | ||
| 101 | + let settings = ExportSettings { | ||
| 102 | + format: Format::Mp4, | ||
| 103 | + ..ExportSettings::default() | ||
| 104 | + }; | ||
| 105 | + let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | ||
| 106 | + let planned = format!( | ||
| 107 | + "{} · {}", | ||
| 108 | + engine::export::planned_video(session.meta(), &settings).expect("an mp4 carries picture"), | ||
| 109 | + session.planned_audio(Format::Mp4) | ||
| 110 | + ); | ||
| 111 | + let out = out_path("planned", "mp4"); | ||
| 112 | + let handle = session.export_to_with(&out, &settings); | ||
| 113 | + let started = Instant::now(); | ||
| 114 | + let opened = loop { | ||
| 115 | + if let Some(line) = handle.encoders() { | ||
| 116 | + break line; | ||
| 117 | + } | ||
| 118 | + assert!( | ||
| 119 | + !handle.is_finished(), | ||
| 120 | + "the export settled before it opened an encoder: {:?}", | ||
| 121 | + handle.result().map(|r| r.err()) | ||
| 122 | + ); | ||
| 123 | + assert!( | ||
| 124 | + started.elapsed() < Duration::from_secs(60), | ||
| 125 | + "no encoder was published" | ||
| 126 | + ); | ||
| 127 | + std::thread::sleep(Duration::from_millis(10)); | ||
| 128 | + }; | ||
| 129 | + // Nothing here wants the file, only the seat it was going to be written | ||
| 130 | + // with: cancelling deletes the `.part` the worker had started. | ||
| 131 | + handle.cancel(); | ||
| 132 | + wind_down(&handle); | ||
| 133 | + assert_eq!(opened, planned); | ||
| 134 | +} | ||
| 135 | + | ||
| 136 | +/// A WAV job has no picture at all and still names what writes it, so an audio | ||
| 137 | +/// export is not a progress line with nothing to say. | ||
| 138 | +#[test] | ||
| 139 | +fn an_audio_only_job_names_its_encoder() { | ||
| 140 | + let settings = ExportSettings { | ||
| 141 | + format: Format::Wav, | ||
| 142 | + ..ExportSettings::default() | ||
| 143 | + }; | ||
| 144 | + assert_eq!( | ||
| 145 | + engine::export::planned_video( | ||
| 146 | + &engine::demux::Demuxer::open(&asset("test_av.mp4")) | ||
| 147 | + .expect("open the fixture") | ||
| 148 | + .0, | ||
| 149 | + &settings | ||
| 150 | + ), | ||
| 151 | + None, | ||
| 152 | + "a WAV has no video seat to name" | ||
| 153 | + ); | ||
| 154 | + let session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | ||
| 155 | + let planned = session.planned_audio(Format::Wav); | ||
| 156 | + let out = out_path("wav", "wav"); | ||
| 157 | + let handle = session.export_to_with(&out, &settings); | ||
| 158 | + let started = Instant::now(); | ||
| 159 | + let opened = loop { | ||
| 160 | + if let Some(line) = handle.encoders() { | ||
| 161 | + break line; | ||
| 162 | + } | ||
| 163 | + assert!( | ||
| 164 | + started.elapsed() < Duration::from_secs(60), | ||
| 165 | + "no encoder was published" | ||
| 166 | + ); | ||
| 167 | + std::thread::sleep(Duration::from_millis(10)); | ||
| 168 | + }; | ||
| 169 | + handle.cancel(); | ||
| 170 | + wind_down(&handle); | ||
| 171 | + let _ = std::fs::remove_file(&out); | ||
| 172 | + assert_eq!(opened, planned); | ||
| 173 | +} | ||
| 174 | + | ||
| 175 | +/// Waits for a cancelled job to settle, so its `.part` is gone before the test | ||
| 176 | +/// ends -- and so no worker outlives the process holding a VA-API session. | ||
| 177 | +fn wind_down(handle: &ExportHandle) { | ||
| 178 | + let started = Instant::now(); | ||
| 179 | + while !handle.is_finished() { | ||
| 180 | + assert!( | ||
| 181 | + started.elapsed() < Duration::from_secs(60), | ||
| 182 | + "a cancelled export did not settle" | ||
| 183 | + ); | ||
| 184 | + std::thread::sleep(Duration::from_millis(10)); | ||
| 185 | + } | ||
| 186 | + let _ = handle.result(); | ||
| 187 | +} | ||