Skip to content

Commit 0df48df

Browse files
authored
feat: add native iPhone LiDAR sensor and web viewer (ruvnet#1684)
* feat(ios): add RuView LiDAR frame protocol * feat(ios): capture ARKit scene depth for RuView * feat(ios): stream compact LiDAR frames over websocket * feat(ios): add native LiDAR capture UI * feat(ios): add RuView LiDAR app entrypoint * feat(web): add LiDAR bridge web package * feat(web): decode RuView LiDAR wire frames * feat(web): add local LiDAR websocket relay * feat(web): add LiDAR browser viewer * feat(web): render live LiDAR point cloud * fix(ios): use wall clock time for LiDAR provenance * feat(web): style LiDAR viewer * test(web): add LiDAR codec tests * docs: add iPhone LiDAR integration guide * docs(adr): define iPhone LiDAR sensor bridge * fix(ios): harden and validate LiDAR bridge * fix(ios): qualify LiDAR wire depth type
1 parent bd110e0 commit 0df48df

17 files changed

Lines changed: 1316 additions & 0 deletions

.github/workflows/iphone-lidar.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: iPhone LiDAR integration
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- 'integrations/iphone-lidar/**'
8+
- 'docs/adr/ADR-340-iphone-lidar-sensor-bridge.md'
9+
- '.github/workflows/iphone-lidar.yml'
10+
pull_request:
11+
paths:
12+
- 'integrations/iphone-lidar/**'
13+
- 'docs/adr/ADR-340-iphone-lidar-sensor-bridge.md'
14+
- '.github/workflows/iphone-lidar.yml'
15+
16+
permissions:
17+
contents: read
18+
19+
jobs:
20+
web:
21+
name: Node relay and codec
22+
runs-on: ubuntu-latest
23+
defaults:
24+
run:
25+
working-directory: integrations/iphone-lidar/web
26+
steps:
27+
- name: Checkout code
28+
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262
29+
30+
- name: Set up Node
31+
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
32+
with:
33+
node-version: '22'
34+
cache: npm
35+
cache-dependency-path: integrations/iphone-lidar/web/package-lock.json
36+
37+
- name: Install dependencies
38+
run: npm ci --ignore-scripts
39+
40+
- name: Run tests
41+
run: npm test
42+
43+
- name: Audit runtime dependencies
44+
run: npm audit --omit=optional --audit-level=high
45+
46+
ios:
47+
name: iOS 17 compile
48+
runs-on: macos-15
49+
steps:
50+
- name: Checkout code
51+
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262
52+
53+
- name: Compile native sources with strict concurrency
54+
shell: bash
55+
run: |
56+
set -euo pipefail
57+
sdk="$(xcrun --sdk iphoneos --show-sdk-path)"
58+
build_dir="$RUNNER_TEMP/ruview-lidar-build"
59+
mkdir -p "$build_dir"
60+
cd "$build_dir"
61+
xcrun swiftc \
62+
-parse-as-library \
63+
-target arm64-apple-ios17.0 \
64+
-sdk "$sdk" \
65+
-module-name RuViewLiDAR \
66+
-strict-concurrency=complete \
67+
-warnings-as-errors \
68+
-emit-module \
69+
-emit-module-path "$build_dir/RuViewLiDAR.swiftmodule" \
70+
-c "$GITHUB_WORKSPACE"/integrations/iphone-lidar/native/RuViewLiDAR/*.swift
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ADR 340: iPhone LiDAR Sensor Bridge
2+
3+
Status: Proposed
4+
5+
## Context
6+
7+
RuView needs a low cost mobile geometry sensor that can contribute calibrated spatial observations without coupling the perception substrate to Apple frameworks.
8+
9+
ARKit exposes rear LiDAR scene depth through `ARFrame.sceneDepth` and `smoothedSceneDepth` on supported devices. Ordinary mobile web pages do not receive this ARKit depth surface directly, so native capture and web visualization must be separated.
10+
11+
## Decision
12+
13+
Use a two layer architecture.
14+
15+
1. Native Swift and ARKit perform acquisition.
16+
2. A modality neutral wire frame transports geometry into browser tools and, next, the RuView HAL.
17+
18+
The native client will capture depth, confidence, camera intrinsics, and world tracking pose. RGB imagery is excluded from the default transport.
19+
20+
The protocol identifier is `ruview.lidar.depth.v1`.
21+
22+
Depth samples are quantized to UInt16 millimeters for transport. Confidence remains UInt8. The default sender downsamples by two spatially and caps transmission at 15 FPS. Full fidelity depth remains available locally for future on device inference.
23+
24+
## RuView integration boundary
25+
26+
The transport must not become a second world model. The production receiver converts each packet into the canonical `ruview-hal::Observation`, then passes it through authenticated sensor identity, provenance, OOD gating, uncertainty aware fusion, spatial memory, and WorldGraph adapters.
27+
28+
Rules:
29+
30+
1. `source=live` is valid only for frames produced by an active ARKit session.
31+
2. Sequence numbers are monotonic per sensor session.
32+
3. Wall clock timestamp is separate from ARKit monotonic frame timing.
33+
4. RGB is off by default and requires an explicit higher privacy capability.
34+
5. Browser clients consume geometry but are not treated as authoritative sensors.
35+
6. Unsupported devices fail closed rather than substituting simulated depth.
36+
37+
## Performance target
38+
39+
`[SYNTHETIC]` A 256 x 192 Float32 depth map is about 196 KB before confidence and metadata. Downsampling to 128 x 96 and encoding each sample as two byte depth plus one byte confidence yields about 36.9 KB raw. At 15 FPS the raw sensor payload is about 553 KB/s. Base64 raises this to roughly 737 KB/s before JSON metadata. These values are arithmetic sizing estimates, not device measurements.
40+
41+
The `[CLAIMED target]` for local network latency is below 150 ms p95. A later binary WebSocket or QUIC transport can remove base64 overhead; the exact end-to-end reduction must be measured before it is claimed.
42+
43+
## Security
44+
45+
The development relay is LAN-facing, requires a random per-run bearer token, bounds message size, and restricts the files it serves. Its default `ws://` transport is not encrypted, so it is not a production trust boundary.
46+
47+
Production requires WSS, authenticated sensor identity, replay protection, message size limits, per tenant authorization, provenance receipts, and explicit retention policy before persistence.
48+
49+
## Consequences
50+
51+
Benefits include commodity hardware, metric depth, tracked camera pose, rapid room scanning, calibration support for RF sensing, and a practical ground truth source for RuView experiments.
52+
53+
The main limitation is that Apple provides processed scene depth rather than the underlying raw transient LiDAR waveform. Therefore this implementation supports direct geometry and sensor fusion now, but does not reproduce research systems that require raw multipath time of flight transients for non line of sight reconstruction.
54+
55+
## Acceptance criteria
56+
57+
A physical LiDAR capable iPhone must stream live geometry to the browser viewer with monotonically increasing sequence numbers, no RGB payload, valid confidence maps, and below 150 ms p95 local network latency over a 60 second run.
58+
59+
CI type-checking and simulator runs do not satisfy this criterion. Until a captured physical-device run records the environment and results, the hardware behavior and latency remain unverified.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# RuView iPhone LiDAR
2+
3+
This experimental integration provides the native and browser components needed to use a LiDAR-capable iPhone as a RuView geometry sensor. The native source is type-checked against the iOS SDK in CI; physical-device validation is tracked separately below.
4+
5+
## Architecture
6+
7+
```text
8+
iPhone LiDAR
9+
-> ARKit sceneDepth
10+
-> depth + confidence + camera intrinsics + device pose
11+
-> compact u16 millimeter wire frame
12+
-> WebSocket relay
13+
-> browser point cloud
14+
-> future RuView HAL / fusion ingest
15+
```
16+
17+
The native path is the sensor. The web path is a receiver and visualization surface. Mobile Safari does not expose ARKit scene depth directly to ordinary web pages, so the browser cannot replace the native capture layer on iPhone today.
18+
19+
## Native iPhone path
20+
21+
Create an iOS SwiftUI app target in Xcode, deployment target iOS 17 or newer, then add the files under `native/RuViewLiDAR/` to the target.
22+
23+
Add this Info.plist value:
24+
25+
```xml
26+
<key>NSCameraUsageDescription</key>
27+
<string>RuView uses the camera and LiDAR scanner to capture local depth geometry.</string>
28+
```
29+
30+
Run on a physical LiDAR capable iPhone or iPad. The simulator does not provide LiDAR scene depth.
31+
32+
The app requests `ARWorldTrackingConfiguration` with `.sceneDepth`, checks `supportsFrameSemantics`, extracts `ARDepthData.depthMap` and `confidenceMap`, and never transmits RGB camera frames.
33+
34+
## Browser path
35+
36+
```bash
37+
cd integrations/iphone-lidar/web
38+
npm ci
39+
npm test
40+
npm start
41+
```
42+
43+
The relay prints a random per-run access token. Open the printed browser URL and set the iPhone endpoint to the printed native URL. They have this form:
44+
45+
```text
46+
http://HOST:8787/?token=TOKEN
47+
ws://HOST:8787/ws/lidar?token=TOKEN
48+
```
49+
50+
Set `RUVIEW_LIDAR_TOKEN` to supply the token explicitly. The token only prevents unauthenticated peers from joining the development relay; because `ws://` does not encrypt it, production use requires TLS and `wss://`.
51+
52+
## Wire format
53+
54+
Schema: `ruview.lidar.depth.v1`
55+
56+
Depth is downsampled by 2 in each dimension by default and streamed at a maximum of 15 FPS. Each depth sample is encoded as little endian UInt16 millimeters plus one UInt8 confidence value. `[SYNTHETIC]` Arithmetic sizing reduces the depth payload from roughly 196 KB per 256 x 192 Float32 frame to roughly 37 KB per 128 x 96 frame before base64 and JSON overhead.
57+
58+
`[SYNTHETIC]` At 15 FPS that is approximately 0.75 MB/s after base64 overhead, versus roughly 8 MB/s for uncompressed Float32 JSON at full resolution. These are sizing estimates, not device or network measurements.
59+
60+
## Privacy and governance
61+
62+
The initial implementation labels provenance as `source=live` and `privacyClass=geometry-only`. It sends depth geometry, confidence, camera intrinsics, pose, sequence, and wall clock timestamp. It does not send RGB imagery.
63+
64+
The development relay requires an ephemeral token and bounds each WebSocket message, but it is not a production trust boundary. Production integration should terminate the WebSocket inside RuView, authenticate the device using the existing sensor identity path, convert each frame into `ruview-hal::Observation`, and attach witness receipts before fusion or persistence.
65+
66+
## Validation status
67+
68+
- `[MEASURED]` The committed Node tests cover wire decoding, malformed inputs, relay authentication, static-file restrictions, and live WebSocket forwarding.
69+
- `[MEASURED]` GitHub Actions type-checks the native sources with strict concurrency against the iOS 17 SDK.
70+
- Physical iPhone capture, end-to-end rendering, confidence-map behavior, and the latency target are not yet measured. A simulator or CI compile does not satisfy the hardware acceptance test.
71+
72+
## Acceptance test
73+
74+
1. Run the relay and browser viewer.
75+
2. Run the native app on a LiDAR capable iPhone.
76+
3. Start LiDAR capture and enable streaming.
77+
4. Move the phone through a room.
78+
5. Verify the browser shows a changing point cloud, sequence increases monotonically, latency stays below the `[CLAIMED target]` of 150 ms p95 on a local WiFi network, and no RGB payload is present in captured WebSocket frames.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import SwiftUI
2+
3+
struct ContentView: View {
4+
@StateObject private var capture = LiDARCaptureManager()
5+
@State private var endpoint = "ws://HOST:8787/ws/lidar?token=TOKEN"
6+
@State private var streaming = false
7+
@State private var status = "Idle"
8+
9+
private let streamer = WebSocketStreamer()
10+
11+
var body: some View {
12+
NavigationStack {
13+
Form {
14+
Section("Sensor") {
15+
HStack {
16+
Text("State")
17+
Spacer()
18+
Text(stateText)
19+
.foregroundStyle(stateColor)
20+
}
21+
HStack {
22+
Text("Capture FPS")
23+
Spacer()
24+
Text(capture.framesPerSecond.formatted(.number.precision(.fractionLength(1))))
25+
}
26+
if let frame = capture.lastFrame {
27+
HStack {
28+
Text("Depth")
29+
Spacer()
30+
Text("\(frame.depth.width) x \(frame.depth.height)")
31+
}
32+
HStack {
33+
Text("Sequence")
34+
Spacer()
35+
Text("\(frame.provenance.sequence)")
36+
}
37+
}
38+
39+
Button("Start LiDAR") {
40+
capture.start(smoothed: false)
41+
}
42+
.disabled(capture.state == .running)
43+
44+
Button("Stop") {
45+
capture.stop()
46+
}
47+
.disabled(capture.state != .running)
48+
}
49+
50+
Section("RuView Stream") {
51+
TextField("ws://host:port/ws/lidar", text: $endpoint)
52+
.textInputAutocapitalization(.never)
53+
.autocorrectionDisabled()
54+
55+
Toggle("Stream geometry", isOn: $streaming)
56+
.onChange(of: streaming) { _, enabled in
57+
Task {
58+
if enabled {
59+
do {
60+
try await streamer.connect(to: endpoint)
61+
status = "Connected"
62+
} catch {
63+
streaming = false
64+
status = error.localizedDescription
65+
}
66+
} else {
67+
await streamer.disconnect()
68+
status = "Disconnected"
69+
}
70+
}
71+
}
72+
73+
Text(status)
74+
.font(.caption)
75+
.foregroundStyle(.secondary)
76+
}
77+
78+
Section("Privacy") {
79+
Text("This implementation transmits depth geometry, confidence, camera intrinsics, and device pose. RGB camera frames are not transmitted.")
80+
.font(.footnote)
81+
}
82+
}
83+
.navigationTitle("RuView LiDAR")
84+
.onAppear {
85+
capture.onFrame = { frame in
86+
guard streaming else { return }
87+
Task {
88+
do {
89+
try await streamer.send(frame, maxFPS: 15, sampleStep: 2)
90+
} catch {
91+
await MainActor.run {
92+
status = error.localizedDescription
93+
}
94+
}
95+
}
96+
}
97+
}
98+
.onDisappear {
99+
capture.stop()
100+
Task { await streamer.disconnect() }
101+
}
102+
}
103+
}
104+
105+
private var stateText: String {
106+
switch capture.state {
107+
case .idle: return "Idle"
108+
case .unsupported: return "No LiDAR"
109+
case .running: return "Live"
110+
case .failed(let message): return "Error: \(message)"
111+
}
112+
}
113+
114+
private var stateColor: Color {
115+
switch capture.state {
116+
case .running: return .green
117+
case .failed, .unsupported: return .red
118+
case .idle: return .secondary
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)