UNPKG

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
# ๐ŸŽง 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.