mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 08:45:38 +02:00
✨ feat: Host a relay on Hetzner (#114)
We are hosting a [MoQ](https://quic.video) relay on a remote (bare metal) server on Hetzner With a lot of help from @victorpahuus
This commit is contained in:
45
packages/moq/contribute/segment.ts
Normal file
45
packages/moq/contribute/segment.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Chunk } from "./chunk"
|
||||
|
||||
export class Segment {
|
||||
id: number
|
||||
|
||||
// Take in a stream of chunks
|
||||
input: WritableStream<Chunk>
|
||||
|
||||
// Output a stream of bytes, which we fork for each new subscriber.
|
||||
#cache: ReadableStream<Uint8Array>
|
||||
|
||||
timestamp = 0
|
||||
|
||||
constructor(id: number) {
|
||||
this.id = id
|
||||
|
||||
// Set a max size for each segment, dropping the tail if it gets too long.
|
||||
// We tee the reader, so this limit applies to the FASTEST reader.
|
||||
const backpressure = new ByteLengthQueuingStrategy({ highWaterMark: 8_000_000 })
|
||||
|
||||
const transport = new TransformStream<Chunk, Uint8Array>(
|
||||
{
|
||||
transform: (chunk: Chunk, controller) => {
|
||||
// Compute the max timestamp of the segment
|
||||
this.timestamp = Math.max(chunk.timestamp + chunk.duration)
|
||||
|
||||
// Push the chunk to any listeners.
|
||||
controller.enqueue(chunk.data)
|
||||
},
|
||||
},
|
||||
undefined,
|
||||
backpressure,
|
||||
)
|
||||
|
||||
this.input = transport.writable
|
||||
this.#cache = transport.readable
|
||||
}
|
||||
|
||||
// Split the output reader into two parts.
|
||||
chunks(): ReadableStream<Uint8Array> {
|
||||
const [tee, cache] = this.#cache.tee()
|
||||
this.#cache = cache
|
||||
return tee
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user