1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Frame ingestion: sources, sampling, the frame governor, retention and the stream pipeline
//! (spec 3.7 §7.9, §7.13).
//!
//! A [`FrameSource`] turns a camera, a file, a screen, a browser or the web home's camera
//! into a [`FrameStream`] of decoded frames at the rate and size a [`Sampling`] asks for. A
//! source never buffers without bound: when the consumer falls behind, the source drops the
//! oldest frames it holds and keeps the newest.
//!
//! | Source | Feature | Ingest | Decode |
//! |--------|---------|--------|--------|
//! | `RtspSource` | `rtsp` | RTSP through `retina`: H.264, H.265, MJPEG | MJPEG in process; H.264 and H.265 through `ffmpeg` |
//! | `FileSource` | `ffmpeg` | MP4 and MOV files | a user-installed `ffmpeg` binary |
//! | `DeviceSource` | `device` | a camera on this machine: AVFoundation (macOS), v4l2 (Linux), DirectShow (Windows) | a user-installed `ffmpeg` binary |
//! | `ScreenSource` | `screen` | a `ComputerActuator` screenshot or a computer-use-mcp observation, at an interval; with `computer-use`, `RuntimeObserver` and `screen_source` over any `ComputerUseRuntime` | PNG, JPEG |
//! | `BrowserSource` | `browser` | the `browser_screenshot` tool, at an interval | PNG |
//! | `SurfaceCameraSource` | `surface-camera` | frames the web home pushes through a `CameraFeed` | JPEG |
//!
//! [`StreamPipeline`] runs one source through the fixed stages of §7.13:
//!
//! ```text
//! source → sampler → frame governor → privacy hook → detector → tracker → observation sink
//! ```
//!
//! - **Sampling.** [`Sampler`] keeps frames at most `fps` apart by presentation time and
//! downscales them to `max_long_edge`.
//! - **The frame governor.** [`FrameGovernor`] holds a stream to its [`FrameBudget`]: a local
//! rate, escalations per minute and frame quotas per period. Under pressure it steps down
//! in a fixed order — lower the local rate, stop escalating, pause the source — writes a
//! `vision.governor.step` audit record at each step, and resumes when the next period
//! starts.
//! - **Privacy.** Every frame passes a [`PrivacyHook`] before any other stage sees it. The
//! only way to a [`RedactedFrame`] is [`FramePrivacy::apply`], and [`FrameKeeper`] stores
//! nothing else, so a frame is blurred before any artifact write (V-I9, S18).
//! - **Retention.** Frames stay ephemeral unless they become a wake payload, evidence on a
//! card, or a sample a person kept; [`FrameKeeper`] stores those for 30 days by default
//! (Q13), purges them after, and writes `vision.evidence.kept` and `.purged` records.
//! - **Detection and tracking.** The detector runs through [`VisionRuntime`], so each frame
//! is metered — in `UsageUnit::Frames`, at zero cost on the device — and its regions come
//! back in source pixels; the
//! [`Tracker`] gives them anonymous track identifiers that reset with the source session.
//! - **Observations.** Each processed frame reaches an [`ObservationSink`]: watch
//! conditions, wakes and the live camera surface consume the stream there.
//! - **Watches and wakes.** [`StreamWatch`] is the watch stage: it holds watch conditions
//! against every observation, keeps the frame of each event as its wake payload, and
//! delivers the event to a [`WatchSink`](crate::WatchSink) such as the kernel's
//! `VisionWakeSource`. [`FrameSources`] holds an operator's named sources and streams each
//! only while a watch is registered on it; the `vision_watch` tool starts and stops those
//! watches (spec 3.7 §7.8, §7.10).
//!
//! With the `conformance` feature, [`check_frame_source`] checks any source against the
//! [`FrameSource`] contract: refused bad rates, rising indices and times, the sampling rate,
//! the size cap, and ephemeral frames.
//!
//! The kill switch pauses a stream at its next frame (V-I17): while the run's governance
//! control is frozen, frames are dropped unprocessed, and the pipeline records
//! `vision.source.paused` and, after the thaw, `vision.source.resumed`.
//!
//! [`VisionRuntime`]: crate::VisionRuntime
//! [`Tracker`]: crate::Tracker
//! [`Sampling`]: crate::Sampling
use Pin;
use async_trait;
use Stream;
use crateResult;
use crate;
use crateSampling;
pub use BrowserSource;
pub use ;
pub use observation_image;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use Sampler;
pub use ;
pub use ;
pub use StreamWatch;
/// How many decoded frames a source holds for a consumer that has fallen behind, before it
/// drops the oldest.
pub const SOURCE_QUEUE_FRAMES: usize = 4;
/// Decoded frames from a [`FrameSource`], in presentation order. The stream ends when the
/// source does; dropping it stops the source and any process it spawned.
pub type FrameStream = ;
/// A source of decoded frames: a camera, a file, a screen, a browser or the web home's
/// camera (spec 3.7 §7.13).
///
/// # Example
///
/// ```
/// use adk_vision::stream::{FrameSource, FrameStream};
/// use adk_vision::{DecodedImage, Frame, FramePixels, FrameSourceId, Sampling};
/// use futures::StreamExt;
/// use std::sync::Arc;
/// use std::time::Duration;
///
/// /// Ten grey frames, 100 ms apart, sampled at the rate asked for.
/// struct Grey(FrameSourceId);
///
/// #[async_trait::async_trait]
/// impl FrameSource for Grey {
/// fn id(&self) -> &FrameSourceId {
/// &self.0
/// }
///
/// async fn frames(&self, sampling: Sampling) -> adk_vision::Result<FrameStream> {
/// let mut sampler = adk_vision::stream::Sampler::new(sampling)?;
/// let source = self.0.clone();
/// let frames = (0..10u64).filter_map(move |index| {
/// let pts = Duration::from_millis(index * 100);
/// sampler.admit(pts).then(|| {
/// Ok(Frame {
/// source: source.clone(),
/// index,
/// pts,
/// captured_at: chrono::Utc::now(),
/// pixels: FramePixels::Ephemeral(Arc::new(DecodedImage::filled(4, 4, [128; 4]))),
/// })
/// })
/// });
/// Ok(futures::stream::iter(frames.collect::<Vec<_>>()).boxed())
/// }
/// }
///
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// let source = Grey(FrameSourceId("grey".into()));
/// let frames: Vec<_> = source
/// .frames(Sampling { fps: 5.0, max_long_edge: None })
/// .await
/// .unwrap()
/// .collect()
/// .await;
/// // 10 frames a second sampled at 5 a second.
/// assert_eq!(frames.len(), 5);
/// # });
/// ```