tiny-server-essentials
Version:
A good utility toolkit to unify Express v5 and Socket.IO v4 into a seamless development experience with modular helpers, server wrappers, and WebSocket tools.
138 lines (90 loc) โข 4.25 kB
Markdown
# ๐ง TinyMediaReceiver
> A lightweight utility for managing continuous audio/video stream buffering using `MediaSource`.
> Ideal for real-time streaming apps, screen-sharing tools, and audio broadcasting via WebRTC or similar protocols.
## ๐งฉ Features
* ๐ Continuous streaming via `MediaSource`
* ๐ง Smart buffer management and auto-cleanup
* ๐ Playback synchronization
* ๐ฏ MIME type validation
* ๐ Support for dynamic push of media chunks
* ๐ง Works with both `<audio>` and `<video>` tags
## ๐ ๏ธ Constructor
```js
new TinyMediaReceiver({
element: 'audio', // or 'video' or an HTMLMediaElement
mimeType: 'audio/webm;codecs=opus',
maxBufferBack: 10, // โช Seconds to keep behind current time
cleanupTime: 100, // โฑ Interval for cleaning buffer (ms)
bufferTolerance: 0.1 // โ๏ธ Margin to avoid desync issues
});
```
## ๐ฆ Constructor Options
| Option | Type | Required | Description |
| ----------------- | ------------------------------ | -------- | ------------------------------------------------------------ |
| `element` | `string` or `HTMLMediaElement` | โ
| Target media element or its tag name (`"audio"` / `"video"`) |
| `mimeType` | `string` | โ
| MIME type of the stream (e.g., `audio/webm;codecs=opus`) |
| `bufferSize` | `number` | โ | Max buffer length in seconds (default depends on media type) |
| `cleanupInterval` | `number` | โ | Time in ms to check for buffer cleaning |
| `tolerance` | `number` | โ | Extra seconds to keep after current playback time |
## ๐ก Events
TinyMediaReceiver emits several events for lifecycle tracking and debugging.
| Event Name | Description |
| --------------- | ----------------------------------------------------------------------- |
| `BufferCleaned` | ๐งผ Emitted after cleaning old segments from the buffer. |
| `SyncTime` | ๐ Used to align time (multi-stream support or drift correction). |
| `Destroyed` | ๐ฅ Emitted when the instance is destroyed. |
| `Error` | โ Triggered if any error occurs during streaming. |
| `SourceOpen` | ๐ Fired when `MediaSource` is open and ready to accept `SourceBuffer`. |
| `FeedQueue` | ๐ Emitted whenever new data is queued into the `SourceBuffer`. |
Use `.addEventListener()` on the instance to listen to these events:
```js
receiver.addEventListener('BufferCleaned', () => {
console.log('Buffer was successfully cleaned!');
});
```
## ๐ Method: `existsEvent(name)`
Checks if a given event name exists in the internal `Events` map.
```js
existsEvent(name: string): boolean
```
## โ๏ธ Core Methods
### `push(buffer: ArrayBuffer)`
Push a new chunk of audio/video data into the playback buffer.
### `pushChunk(buffer: ArrayBuffer)`
Alias for `push`.
### `destroy()`
Gracefully ends the stream and frees resources, stopping intervals and revoking media URLs.
## ๐ฏ Getters & Setters
### `getElement(): HTMLMediaElement`
Returns the internal `<audio>` or `<video>` element.
### `getMaxBufferBack(): number`
Gets the max seconds of buffered media behind current time.
### `setMaxBufferBack(value: number)`
Sets how much data (in seconds) should be kept behind current time.
### `getTolerance(): number`
Returns the current desync tolerance in seconds.
### `setTolerance(value: number)`
Sets the buffer desync tolerance.
## ๐งช Example
```js
const receiver = new TinyMediaReceiver({
element: 'audio',
mimeType: 'audio/webm;codecs=opus',
});
receiver.push(someChunk);
// Later
receiver.destroy();
```
## ๐ Notes
* The internal `SourceBuffer` uses `'sequence'` mode to preserve chunk order.
* Automatically syncs `currentTime` if playback goes outside buffered range.
* Requires a modern browser that supports the MediaSource API.