Skip to content

Commit eef4cfb

Browse files
feat(huddle): add opt-in camera video sharing
Signed-off-by: Daniel Prokhorov <[email protected]>
1 parent 3fc1db4 commit eef4cfb

37 files changed

Lines changed: 3230 additions & 118 deletions

‎crates/buzz-core/src/huddle_screen.rs‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ pub const MAX_CONTENT_BYTES: usize = 244 * 1024;
2121
pub const MAX_WIDTH: u32 = 1920;
2222
/// Maximum encoded height.
2323
pub const MAX_HEIGHT: u32 = 1080;
24+
/// Maximum webcam width, independent of the larger screen-share limit.
25+
pub const MAX_CAMERA_WIDTH: u32 = 1280;
26+
/// Maximum webcam height.
27+
pub const MAX_CAMERA_HEIGHT: u32 = 720;
2428
/// Target capture and playback cadence, independent of relay event rate.
2529
pub const VIDEO_FPS: u32 = 30;
2630
/// Maximum access units per transport event.
@@ -78,6 +82,11 @@ pub enum ScreenMessage {
7882
},
7983
}
8084

85+
/// Consent identity shared by screen and camera video tracks.
86+
pub type VideoShare = ScreenShare;
87+
/// The bounded H.264 wire format shared by independently scoped video tracks.
88+
pub type HuddleVideoMessage = ScreenMessage;
89+
8190
/// Inspect an access unit without decoding pixels.
8291
///
8392
/// SPS dimensions and codec bytes come from the actual compressed stream,
@@ -169,16 +178,33 @@ pub fn validate_access_unit(
169178
///
170179
/// The caller separately verifies signatures and channel/community access.
171180
pub fn validate_screen_event(event: &Event, now: u64) -> Result<Uuid, String> {
172-
if u32::from(event.kind.as_u16()) != crate::kind::KIND_HUDDLE_SCREEN {
173-
return Err("invalid: not a huddle screen event".into());
181+
validate_video_event(event, now, crate::kind::KIND_HUDDLE_SCREEN)
182+
}
183+
184+
/// Validate a camera track without accepting screen events in its place.
185+
pub fn validate_camera_event(event: &Event, now: u64) -> Result<Uuid, String> {
186+
validate_video_event(event, now, crate::kind::KIND_HUDDLE_CAMERA)
187+
}
188+
189+
fn validate_video_event(event: &Event, now: u64, kind: u32) -> Result<Uuid, String> {
190+
let camera = kind == crate::kind::KIND_HUDDLE_CAMERA;
191+
let media = if camera { "camera" } else { "screen" };
192+
let max_width = if camera { MAX_CAMERA_WIDTH } else { MAX_WIDTH };
193+
let max_height = if camera {
194+
MAX_CAMERA_HEIGHT
195+
} else {
196+
MAX_HEIGHT
197+
};
198+
if u32::from(event.kind.as_u16()) != kind {
199+
return Err(format!("invalid: not a huddle {media} event"));
174200
}
175201
let channels: Vec<_> = event
176202
.tags
177203
.iter()
178204
.filter(|tag| tag.as_slice().first().map(String::as_str) == Some("h"))
179205
.collect();
180206
if channels.len() != 1 || channels[0].as_slice().len() != 2 {
181-
return Err("invalid: screen events require exactly one h tag".into());
207+
return Err(format!("invalid: {media} events require exactly one h tag"));
182208
}
183209
let channel = Uuid::parse_str(&channels[0].as_slice()[1])
184210
.map_err(|_| "invalid: screen channel must be a UUID")?;
@@ -217,8 +243,8 @@ pub fn validate_screen_event(event: &Event, now: u64) -> Result<Uuid, String> {
217243
|| sequence > 9_007_199_254_740_991
218244
|| width == 0
219245
|| height == 0
220-
|| width > MAX_WIDTH
221-
|| height > MAX_HEIGHT
246+
|| width > max_width
247+
|| height > max_height
222248
|| frames.is_empty()
223249
|| frames.len() > MAX_BATCH_FRAMES
224250
|| codec.len() != 11
@@ -359,4 +385,23 @@ mod tests {
359385
}
360386
assert!(validate_screen_event(&event(&message, &[&channel]), 100).is_err());
361387
}
388+
389+
#[test]
390+
fn camera_events_are_separate_from_screen_events_and_remain_ephemeral() {
391+
let channel = Uuid::new_v4().to_string();
392+
let message = video();
393+
let camera = EventBuilder::new(
394+
Kind::Custom(crate::kind::KIND_HUDDLE_CAMERA as u16),
395+
serde_json::to_string(&message).unwrap(),
396+
)
397+
.tags([Tag::parse(["h", channel.as_str()]).unwrap()])
398+
.custom_created_at(Timestamp::from(100))
399+
.sign_with_keys(&Keys::generate())
400+
.unwrap();
401+
assert!(validate_camera_event(&camera, 100).is_ok());
402+
assert!(validate_screen_event(&camera, 100).is_err());
403+
assert!(validate_camera_event(&event(&message, &[&channel]), 100).is_err());
404+
assert!(crate::kind::is_ephemeral(crate::kind::KIND_HUDDLE_CAMERA));
405+
assert!(validate_camera_event(&camera, 106).is_err());
406+
}
362407
}

‎crates/buzz-core/src/kind.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,9 @@ pub const KIND_HUDDLE_REACTION: u32 = 24810;
507507
/// Ephemeral: bounded H.264 video batches and screen-share stop notifications, scoped
508508
/// to the active private huddle backing channel by exactly one `h` tag.
509509
pub const KIND_HUDDLE_SCREEN: u32 = 24811;
510+
511+
/// Ephemeral H.264 webcam video and camera stop notifications in a huddle.
512+
pub const KIND_HUDDLE_CAMERA: u32 = 24812;
510513
// Stream messaging
511514
/// NIP-29 group chat message kind. V1 used kind:10001 (replaceable range — wrong), then 40001.
512515
///
@@ -741,6 +744,7 @@ pub const ALL_KINDS: &[u32] = &[
741744
KIND_AGENT_PRESENCE,
742745
KIND_HUDDLE_REACTION,
743746
KIND_HUDDLE_SCREEN,
747+
KIND_HUDDLE_CAMERA,
744748
KIND_BLOSSOM_AUTH,
745749
KIND_PAIRING,
746750
KIND_AGENT_OBSERVER_FRAME,

‎crates/buzz-relay/src/handlers/event.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,12 +924,19 @@ async fn handle_ephemeral_event(
924924
let event_id = event.id.to_hex();
925925
let verify_result = tokio::task::spawn_blocking(move || {
926926
verify_event(&event_clone).map_err(|e| format!("invalid: {e}"))?;
927-
if event_kind_u32(&event_clone) == buzz_core::kind::KIND_HUDDLE_SCREEN {
927+
let kind = event_kind_u32(&event_clone);
928+
if kind == buzz_core::kind::KIND_HUDDLE_SCREEN {
928929
buzz_core::huddle_screen::validate_screen_event(
929930
&event_clone,
930931
nostr::Timestamp::now().as_secs(),
931932
)
932933
.map(Some)
934+
} else if kind == buzz_core::kind::KIND_HUDDLE_CAMERA {
935+
buzz_core::huddle_screen::validate_camera_event(
936+
&event_clone,
937+
nostr::Timestamp::now().as_secs(),
938+
)
939+
.map(Some)
933940
} else {
934941
Ok(None)
935942
}
@@ -1183,7 +1190,10 @@ async fn publish_channel_ephemeral(
11831190
.local_event_ids
11841191
.invalidate(&(tenant.community(), event.id.to_bytes()));
11851192
warn!(event_id = %event.id.to_hex(), "Ephemeral publish failed: {e}");
1186-
if event_kind_u32(&event) == buzz_core::kind::KIND_HUDDLE_SCREEN {
1193+
if matches!(
1194+
event_kind_u32(&event),
1195+
buzz_core::kind::KIND_HUDDLE_SCREEN | buzz_core::kind::KIND_HUDDLE_CAMERA
1196+
) {
11871197
return Err(IngestError::Internal(
11881198
"error: screen delivery unavailable".into(),
11891199
));

‎crates/buzz-relay/src/handlers/huddle_screen_tests.rs‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ fn connection(keys: &Keys) -> Arc<ConnectionState> {
3434
}
3535

3636
fn stop_event(keys: &Keys, channels: &[String]) -> Event {
37+
stop_video_event(keys, channels, buzz_core::kind::KIND_HUDDLE_SCREEN)
38+
}
39+
40+
fn stop_video_event(keys: &Keys, channels: &[String], kind: u32) -> Event {
3741
EventBuilder::new(
38-
Kind::Custom(buzz_core::kind::KIND_HUDDLE_SCREEN as u16),
42+
Kind::Custom(kind as u16),
3943
serde_json::json!({
4044
"type": "stopped",
4145
"share": {
@@ -55,6 +59,20 @@ fn stop_event(keys: &Keys, channels: &[String]) -> Event {
5559
.unwrap()
5660
}
5761

62+
#[tokio::test]
63+
async fn camera_handler_rejects_missing_or_ambiguous_channel_scope() {
64+
let state = crate::state::tests::test_state().await;
65+
state.db.pool().close().await;
66+
let keys = Keys::generate();
67+
let channel = Uuid::new_v4().to_string();
68+
for channels in [vec![], vec![channel.clone(), channel.clone()]] {
69+
let event = stop_video_event(&keys, &channels, buzz_core::kind::KIND_HUDDLE_CAMERA);
70+
let result = deliver(event, &keys, state.clone()).await;
71+
assert!(matches!(result, Err(IngestError::Rejected(ref reason))
72+
if reason == "invalid: camera events require exactly one h tag"));
73+
}
74+
}
75+
5876
async fn deliver(event: Event, keys: &Keys, state: Arc<AppState>) -> Result<(), IngestError> {
5977
let conn = connection(keys);
6078
super::handle_ephemeral_event(

‎crates/buzz-relay/src/rejection/quota_tests.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,20 @@ async fn ephemeral_events_leave_human_and_agent_message_budgets_untouched() {
123123
let state = state_with_limits(2, 3, 100).await;
124124
let recorder = DebuggingRecorder::new();
125125
let _guard = metrics::set_default_local_recorder(&recorder);
126-
// Both range boundaries, typing, presence, and observer frames. Adjacent
126+
// Both range boundaries, typing, presence, and screen/camera frames. Adjacent
127127
// non-ephemeral kinds below must still consume the message allowance.
128-
let ephemeral_kinds = [20000, 20001, 20002, 24200, 24811, 29999];
128+
let ephemeral_kinds = [20000, 20001, 20002, 24200, 24811, 24812, 29999];
129129
for (agent, limit) in [(false, 2), (true, 3)] {
130130
for stored_kind in [0, 1, 9, 19999, 30000, 40002] {
131131
let (conn, mut rx) = connection(agent);
132132
for kind in ephemeral_kinds {
133133
assert!(enforce_ws_admission(&event(kind), &conn, &state).await);
134134
}
135135
assert_eq!(counter(&state, &conn, LimitType::Messages).await, None);
136-
assert_eq!(counter(&state, &conn, LimitType::WsEvents).await, Some(6));
136+
assert_eq!(
137+
counter(&state, &conn, LimitType::WsEvents).await,
138+
Some(ephemeral_kinds.len() as u64)
139+
);
137140
for expected in 1..=limit {
138141
assert!(enforce_ws_admission(&event(stored_kind), &conn, &state).await);
139142
assert_eq!(

‎crates/buzz-test-client/tests/e2e_huddle_screen.rs‎

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use anyhow::{bail, Context, Result};
99
use buzz_core::{
1010
huddle_screen::{ScreenMessage, ScreenShare},
1111
kind::{
12-
KIND_HUDDLE_SCREEN, KIND_NIP29_CREATE_GROUP, KIND_NIP29_EDIT_METADATA, KIND_NIP29_PUT_USER,
12+
KIND_HUDDLE_CAMERA, KIND_HUDDLE_SCREEN, KIND_NIP29_CREATE_GROUP, KIND_NIP29_EDIT_METADATA,
13+
KIND_NIP29_PUT_USER,
1314
},
1415
};
1516
use buzz_test_client::{BuzzTestClient, RelayMessage};
@@ -32,8 +33,12 @@ struct Room {
3233
}
3334

3435
fn filter(channel: &str) -> Filter {
36+
video_filter(channel, KIND_HUDDLE_SCREEN)
37+
}
38+
39+
fn video_filter(channel: &str, kind: u32) -> Filter {
3540
Filter::new()
36-
.kind(Kind::Custom(KIND_HUDDLE_SCREEN as u16))
41+
.kind(Kind::Custom(kind as u16))
3742
.custom_tags(SingleLetterTag::lowercase(Alphabet::H), [channel])
3843
.limit(0)
3944
}
@@ -64,9 +69,18 @@ fn video_packets() -> Result<Vec<ScreenMessage>> {
6469
}
6570

6671
fn screen_event(keys: &Keys, message: &ScreenMessage, channels: &[&str]) -> Result<Event> {
72+
video_event(keys, message, channels, KIND_HUDDLE_SCREEN)
73+
}
74+
75+
fn video_event(
76+
keys: &Keys,
77+
message: &ScreenMessage,
78+
channels: &[&str],
79+
kind: u32,
80+
) -> Result<Event> {
6781
sign(
6882
keys,
69-
KIND_HUDDLE_SCREEN,
83+
kind,
7084
serde_json::to_string(message)?,
7185
channels
7286
.iter()
@@ -75,6 +89,106 @@ fn screen_event(keys: &Keys, message: &ScreenMessage, channels: &[&str]) -> Resu
7589
)
7690
}
7791

92+
#[tokio::test]
93+
#[ignore = "Requires Postgres, Redis, and a local relay built from this branch"]
94+
async fn camera_and_screen_share_independent_private_live_tracks_without_persistence() -> Result<()>
95+
{
96+
let mut room = room().await?;
97+
let mut viewer = BuzzTestClient::connect(&room.url, &room.viewer).await?;
98+
viewer
99+
.subscribe(
100+
"cameras",
101+
vec![video_filter(&room.channel, KIND_HUDDLE_CAMERA)],
102+
)
103+
.await?;
104+
assert!(viewer
105+
.collect_until_eose("cameras", DEADLINE)
106+
.await?
107+
.is_empty());
108+
viewer
109+
.subscribe("screens", vec![filter(&room.channel)])
110+
.await?;
111+
assert!(viewer
112+
.collect_until_eose("screens", DEADLINE)
113+
.await?
114+
.is_empty());
115+
let packets = video_packets()?;
116+
let camera = video_event(
117+
&room.owner,
118+
&packets[0],
119+
&[&room.channel],
120+
KIND_HUDDLE_CAMERA,
121+
)?;
122+
let screen = screen_event(&room.owner, &packets[0], &[&room.channel])?;
123+
for (event, subscription) in [(camera, "cameras"), (screen, "screens")] {
124+
let id = event.id;
125+
accepted(&mut room.publisher, event).await?;
126+
assert!(matches!(viewer.recv_event(DEADLINE).await?,
127+
RelayMessage::Event { subscription_id, event }
128+
if subscription_id == subscription && event.id == id));
129+
}
130+
let ScreenMessage::Video { share, .. } = &packets[0] else {
131+
bail!("video fixture")
132+
};
133+
accepted(
134+
&mut room.publisher,
135+
video_event(
136+
&room.owner,
137+
&ScreenMessage::Stopped {
138+
share: share.clone(),
139+
},
140+
&[&room.channel],
141+
KIND_HUDDLE_CAMERA,
142+
)?,
143+
)
144+
.await?;
145+
assert!(matches!(viewer.recv_event(DEADLINE).await?,
146+
RelayMessage::Event { subscription_id, .. } if subscription_id == "cameras"));
147+
accepted(
148+
&mut room.publisher,
149+
screen_event(&room.owner, &packets[1], &[&room.channel])?,
150+
)
151+
.await?;
152+
assert!(matches!(viewer.recv_event(DEADLINE).await?,
153+
RelayMessage::Event { subscription_id, .. } if subscription_id == "screens"));
154+
let mut outsider = BuzzTestClient::connect(&room.url, &room.outsider).await?;
155+
assert!(
156+
!outsider
157+
.send_event(video_event(
158+
&room.outsider,
159+
&packets[0],
160+
&[&room.channel],
161+
KIND_HUDDLE_CAMERA
162+
)?)
163+
.await?
164+
.accepted
165+
);
166+
let mut other_tenant = BuzzTestClient::connect(&room.other_url, &room.owner).await?;
167+
assert!(
168+
!other_tenant
169+
.send_event(video_event(
170+
&room.owner,
171+
&packets[0],
172+
&[&room.channel],
173+
KIND_HUDDLE_CAMERA
174+
)?)
175+
.await?
176+
.accepted
177+
);
178+
let stored: i64 = sqlx::query_scalar(
179+
"SELECT count(*) FROM events WHERE community_id = $1 AND channel_id = $2 AND kind = ANY($3)",
180+
).bind(room.community).bind(Uuid::parse_str(&room.channel)?)
181+
.bind(vec![KIND_HUDDLE_CAMERA as i32, KIND_HUDDLE_SCREEN as i32])
182+
.fetch_one(&room.pool).await?;
183+
assert_eq!(stored, 0, "neither video track may persist pixels");
184+
outsider.disconnect().await?;
185+
other_tenant.disconnect().await?;
186+
viewer.disconnect().await?;
187+
room.publisher.disconnect().await?;
188+
room.pool.close().await;
189+
Ok(())
190+
}
191+
78192
async fn accepted(client: &mut BuzzTestClient, event: Event) -> Result<()> {
79193
let response = client.send_event(event).await?;
80194
assert!(

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export default defineConfig({
106106
"**/project-conversation-load-failure.spec.ts",
107107
"**/huddle-thread-load-failure.spec.ts",
108108
"**/huddle-screen-sharing.spec.ts",
109+
"**/huddle-camera.spec.ts",
109110
"**/workspace-rail.spec.ts",
110111
"**/community-rail.spec.ts",
111112
"**/boot-splash.spec.ts",

‎desktop/src-tauri/Info.plist‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<key>NSMicrophoneUsageDescription</key>
1010
<string>Buzz needs microphone access for voice huddles and voice notes.</string>
1111
<key>NSCameraUsageDescription</key>
12-
<string>Buzz needs camera access to record animated avatars.</string>
12+
<string>Buzz needs camera access for opt-in huddle video and animated avatars.</string>
1313
<key>NSLocalNetworkUsageDescription</key>
1414
<string>Buzz uses your local network for optional Share Compute and local relay connections. Remote messaging does not require this access.</string>
1515
</dict>

‎desktop/src-tauri/src/commands/workspace.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ pub async fn apply_workspace(
269269
};
270270
if !same_relay || !same_signer {
271271
state.huddle()?.stop_screen_capture();
272+
state.huddle()?.stop_camera_capture();
272273
state.emit_huddle_state_changed();
273274
}
274275

0 commit comments

Comments
 (0)