├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Mathias Buus 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # webm-cluster-stream 2 | 3 | Transform stream that splits a webm stream into a header buffer and cluster buffers. 4 | Useful if you have a webm live stream and wants to make it seekable. See the [webm spec](http://www.webmproject.org) for more info 5 | 6 | ``` 7 | npm install webm-cluster-stream 8 | ``` 9 | 10 | ## Usage 11 | 12 | ``` js 13 | const WCS = require('webm-cluster-stream') 14 | const fs = require('fs') 15 | 16 | const cl = new WCS() 17 | 18 | cl.once('data', function (header) { 19 | // first buffer is header 20 | console.log('header:', header) 21 | cl.on('data', function (cluster) { 22 | // next buffers are "Cluster" objects 23 | console.log('cluster:', cluster) 24 | }) 25 | }) 26 | 27 | fs.createReadStream('movie.webm').pipe(cl) 28 | ``` 29 | 30 | ## License 31 | 32 | MIT 33 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { Decoder } = require('ebml') 2 | const b4a = require('b4a') 3 | const { Duplex } = require('streamx') 4 | 5 | module.exports = class WebmClusterStream extends Duplex { 6 | constructor () { 7 | super() 8 | 9 | this.decoder = new Decoder() 10 | this.buffer = [] 11 | this.byteOffset = 0 12 | this._ondrain = null 13 | 14 | this.decoder.on('data', this._ondecode.bind(this)) 15 | this.decoder.on('end', this._onend.bind(this)) 16 | this.decoder.on('error', this.destroy.bind(this)) 17 | this.decoder.on('drain', this._continueWrite.bind(this)) 18 | } 19 | 20 | _onend () { 21 | if (this.buffer.length === 0) return 22 | const chunk = this._combine() 23 | this.push(chunk) 24 | this.push(null) 25 | } 26 | 27 | _ondecode (data) { 28 | if (data[0] === 'start' && data[1].name === 'Cluster') { 29 | const chunk = this._combine() 30 | const rel = data[1].start - this.byteOffset 31 | const prev = chunk.subarray(0, rel) 32 | this.buffer.push(chunk.subarray(rel)) 33 | this.byteOffset += rel 34 | if (this.push(prev) === false) this.decoder.pause() 35 | } 36 | } 37 | 38 | _read (cb) { 39 | this.decoder.resume() 40 | this._continueWrite() 41 | cb(null) 42 | } 43 | 44 | _write (data, cb) { 45 | this.buffer.push(data) 46 | 47 | if (this.decoder.write(data) === false) { 48 | this._ondrain = cb 49 | return 50 | } 51 | 52 | cb(null) 53 | } 54 | 55 | _predestroy () { 56 | this._continueWrite() 57 | } 58 | 59 | _continueWrite () { 60 | const cb = this._ondrain 61 | if (cb === null) return 62 | this._ondrain = null 63 | cb(null) 64 | } 65 | 66 | _final (cb) { 67 | this.decoder.end() 68 | cb(null) 69 | } 70 | 71 | _combine () { 72 | const buffer = this.buffer 73 | this.buffer = [] 74 | const chunk = buffer.length === 1 ? buffer[0] : b4a.concat(buffer) 75 | return chunk 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webm-cluster-stream", 3 | "version": "2.0.0", 4 | "description": "Transform stream that splits a webm stream into a header buffer and cluster buffers", 5 | "main": "index.js", 6 | "dependencies": { 7 | "b4a": "^1.6.6", 8 | "ebml": "^3.0.0", 9 | "streamx": "^2.18.0" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/mafintosh/webm-cluster-stream.git" 14 | }, 15 | "scripts": { 16 | "test": "standard" 17 | }, 18 | "author": "Mathias Buus (@mafintosh)", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/mafintosh/webm-cluster-stream/issues" 22 | }, 23 | "homepage": "https://github.com/mafintosh/webm-cluster-stream", 24 | "devDependencies": { 25 | "standard": "^17.1.0" 26 | } 27 | } 28 | --------------------------------------------------------------------------------