longpipe 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sam Bhattacharyya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # Longpipe 🐉
2
+
3
+ Hardware-accelerated real-time AI media processing in the browser.
4
+
5
+ Try the live demo: [longpipe.dev/demo](https://longpipe.dev/demo)
6
+
7
+ > **Warning** — This project is very new (v0.0.1) and still under active development. Expect API changes between versions and bugs.
8
+
9
+ ## Features
10
+
11
+ - Real-time portrait matting / virtual backgrounds in the browser
12
+ - WebGPU compute shaders with a WebGL2 fragment-shader fallback
13
+ - Worker-by-default — all model and render work runs off the main thread
14
+ - Background options: blur, image, video, solid color, or none (passthrough)
15
+ - Six trained model presets covering hardware from MacBook Pro down to Chromebook
16
+ - Auto-tuned at init: picks the best preset for the current device, plus the optimal frame transport for the current browser
17
+ - Adaptive at runtime: swaps to a smaller preset when FPS drops, larger when there's headroom
18
+ - Single-call API: `new EffectsPipeline(inputStream, { background })` returns a `MediaStream`
19
+ - Audio passthrough by default
20
+
21
+ ## Install
22
+
23
+ ```
24
+ npm install longpipe
25
+ ```
26
+
27
+ ## Quick start
28
+
29
+ ```ts
30
+ import { EffectsPipeline } from 'longpipe'
31
+
32
+ const inputStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true })
33
+
34
+ const pipeline = new EffectsPipeline(inputStream, {
35
+ background: 'blur',
36
+ })
37
+
38
+ videoEl.srcObject = pipeline.stream // available immediately
39
+ await pipeline.ready // optional — resolves once the effect is live
40
+ ```
41
+
42
+ `pipeline.stream` is wired synchronously and emits the unprocessed input until the model is ready (~1–3s on cold start, depending on hardware), so the consumer sees live video the whole time.
43
+
44
+ ## Backgrounds
45
+
46
+ The `background` option takes a wide set of inputs and normalizes them internally:
47
+
48
+ ```ts
49
+ new EffectsPipeline(stream, { background: 'none' }) // passthrough
50
+ new EffectsPipeline(stream, { background: 'blur' }) // default sigma
51
+ new EffectsPipeline(stream, { background: { blur: { sigma: 12 } } })
52
+
53
+ // images: URL, <img>, ImageBitmap, or { image: ... }
54
+ new EffectsPipeline(stream, { background: 'https://example.com/bg.jpg' })
55
+ new EffectsPipeline(stream, { background: imgElement })
56
+ new EffectsPipeline(stream, { background: imageBitmap })
57
+
58
+ // video: URL, Blob, or <video> — looped, muted, decoded on the main thread
59
+ new EffectsPipeline(stream, { background: { video: 'https://example.com/bg.mp4' } })
60
+
61
+ // solid color
62
+ new EffectsPipeline(stream, { background: { color: [0, 100, 200] } })
63
+ ```
64
+
65
+ Swap at runtime:
66
+
67
+ ```ts
68
+ await pipeline.setBackground({ blur: { sigma: 20 } })
69
+ await pipeline.setBackground('https://example.com/other-bg.jpg')
70
+ ```
71
+
72
+ ## Performance presets
73
+
74
+ ```ts
75
+ new EffectsPipeline(stream, { preset: 'auto' }) // benchmarks at init (default)
76
+ new EffectsPipeline(stream, { preset: 'fast' }) // small model
77
+ new EffectsPipeline(stream, { preset: 'balanced' }) // medium model
78
+ new EffectsPipeline(stream, { preset: 'quality' }) // xl model
79
+ ```
80
+
81
+ `'auto'` runs a microbenchmark at init and picks the largest preset that fits the per-frame budget on the current device. While `'auto'` is in effect an adaptive controller polls FPS / model time and swaps preset up or down as conditions change. Explicit preset choices (`'fast'`, `'balanced'`, `'quality'`, or a manual config) are always respected and never auto-overridden.
82
+
83
+ You can also pass a manual config:
84
+
85
+ ```ts
86
+ new EffectsPipeline(stream, {
87
+ preset: { model: 'large', dtype: 'f16', resolution: { w: 256, h: 144 }, skipFrames: 0 },
88
+ })
89
+ ```
90
+
91
+ ## Other options
92
+
93
+ ```ts
94
+ new EffectsPipeline(stream, {
95
+ background: 'blur',
96
+ preset: 'auto',
97
+ adaptive: true, // default; only applies when preset is 'auto'
98
+ audio: 'passthrough', // or 'drop'
99
+ outputResolution: { w: 1280, h: 720 }, // default: matches the input video track
100
+ weightsBaseUrl: 'https://your-cdn/longpipe/', // default: cdn.longpipe.dev
101
+ enabled: true, // false = pass input through unchanged
102
+ onReady: () => console.log('live'),
103
+ onError: (err) => console.error(err),
104
+ })
105
+ ```
106
+
107
+ Toggle the effect on/off without tearing anything down (cheap to re-enable):
108
+
109
+ ```ts
110
+ pipeline.setEnabled(false)
111
+ ```
112
+
113
+ Tear it all down:
114
+
115
+ ```ts
116
+ pipeline.destroy()
117
+ ```
118
+
119
+ ## Self-hosting weights
120
+
121
+ By default Longpipe fetches model weights from `https://cdn.longpipe.dev/models/v/0.0.1/`. You can browse the available files, sizes, and SHA-256 hashes at [cdn.longpipe.dev/models/v/0.0.1/index.html](https://cdn.longpipe.dev/models/v/0.0.1/index.html) (machine-readable list at [manifest.json](https://cdn.longpipe.dev/models/v/0.0.1/manifest.json)).
122
+
123
+ To serve them yourself, mirror the files under any base URL with the same `model_${name}.bin` naming convention and pass it via `weightsBaseUrl`:
124
+
125
+ ```ts
126
+ new EffectsPipeline(stream, {
127
+ weightsBaseUrl: 'https://your-cdn.example.com/longpipe-weights/',
128
+ })
129
+ ```
130
+
131
+ ## Browser support
132
+
133
+ Works on Chromium (Chrome, Edge), Firefox, and Safari (desktop and iOS). WebGPU is used when available; WebGL2 is the fallback. Longpipe picks the optimal video frame transport for each browser internally — `MediaStreamTrackProcessor`, `transferControlToOffscreen` + `captureStream`, or an `ImageBitmap` shuttle as universal fallback — all invisible to the caller.
134
+
135
+ ## Problem
136
+
137
+ If you want real-time AI media processing in the browser — virtual backgrounds, background noise removal, face filters — one of the few open-source options is [MediaPipe](https://github.com/google-ai-edge/mediapipe). MediaPipe ships some great open-source models, but they all run in WebAssembly, and using them effectively still requires a fair amount of hardware-accelerated pre- and post-processing on top.
138
+
139
+ ![MediaPipe pipeline diagram](https://github.com/user-attachments/assets/62932072-c5b2-445e-b5d8-a2bd5bb72920)
140
+
141
+ In 2021, I built an SDK to implement [hardware-accelerated neural networks directly in WebGL](https://medium.com/vectorly/building-a-more-efficient-background-segmentation-model-than-google-74ecd17392d5) that proved much more efficient than the MediaPipe implementation. It was popular, but it was a commercial SDK — the company was acquired and the technology was never opened up.
142
+
143
+ In 2022, Google Meet adopted a similar approach with their own [hardware-accelerated networks](https://research.google/blog/high-definition-segmentation-in-google-meet/), but that was never open-sourced either.
144
+
145
+ As of late 2025, MediaPipe is still the state of the art for open-source real-time browser ML, and nothing better has come along.
146
+
147
+ ## Solution
148
+
149
+ With WebGPU shipping and WebNN on the way, it is now possible to build efficient implementations of popular real-time media-processing features — background segmentation, audio filtering — by combining hardware-accelerated neural networks in WebGPU/WebNN with efficient pre- and post-processing, so developers can plug in features like virtual backgrounds and noise removal without worrying about the details.
150
+
151
+ Longpipe is that SDK.
152
+
153
+ ## How it works
154
+
155
+ Two layers:
156
+
157
+ - **Model** (`src/model/`) — EfficientNet-Lite-0 encoder with a U-Net decoder, written as TypeScript op classes. Each layer is a class; weights load as binary tensors at init; the backend (WebGPU or WebGL2) is injected at construction. BatchNorm is fused into conv weights at export — there is no BN op at inference. See [docs/ARCHITECTURE.md](../docs/ARCHITECTURE.md) and [docs/MODEL_PLAN.md](../docs/MODEL_PLAN.md).
158
+ - **Pipeline** (`src/pipeline/`) — capability detection, per-browser frame transport selection, worker spawn, audio passthrough, autotune, and the adaptive controller. Designed to absorb the browser/codec/canvas plumbing complexity so consumers don't have to. See [docs/PIPELINE.md](../docs/PIPELINE.md).
159
+
160
+ Six trained presets cover the hardware range:
161
+
162
+ | Preset | Resolution | Encoder | Decoder | Skip frames |
163
+ |---------|------------|---------|----------|-------------|
164
+ | xl | 512×288 | full | 2× ch | 0 |
165
+ | large | 256×144 | full | standard | 0 |
166
+ | medium | 256×144 | full | standard | 1 |
167
+ | compact | 256×144 | full | small | 1 |
168
+ | small | 256×144 | small | standard | 1 |
169
+ | xs | 192×108 | small | standard | 1 |
170
+
171
+ `Skip frames` is how many input frames the model sits out between runs — the compositor still renders every frame using the most recent alpha matte. Autotune picks one of these at init based on a microbenchmark of the actual network on the actual device.
172
+
173
+ ## Development
174
+
175
+ Training scripts, fixture generation, and the weight export pipeline are not yet documented here — coming soon.
176
+
177
+ ## Acknowledgements
178
+
179
+ - [EfficientNet-Lite](https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet/lite) for the backbone architecture.
180
+ - [Google Meet HD Segmentation research](https://research.google/blog/high-definition-segmentation-in-google-meet/) for the architectural choices that hold up on accelerators.
181
+ - [WebGPU](https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API) for making compute-heavy work in the browser realistic.
182
+ - [Mediabunny](https://mediabunny.dev/) — a colleague's project and a model for what an open-source media SDK with sponsorship-backed development can look like.
183
+
184
+ ## Roadmap
185
+
186
+ - [x] Background segmentation / virtual backgrounds
187
+ - [x] WebGPU + WebGL2 backends, f16 + f32
188
+ - [x] Worker-based pipeline with per-browser transport selection
189
+ - [x] Autotuned + adaptive preset selection
190
+ - [ ] Face landmark detection
191
+ - [ ] Lighting correction
192
+ - [ ] Background noise removal (audio, separate pipeline)
193
+ - [ ] Mobile SDKs