import debug from 'debug' ; import { Tube } from '../component' ; import { Transform } from 'stream' ; import { Message , MessageType } from '../message' ; const MAX_CAPTURE_BYTES = 225000000 ; /**
 * Component that records MP4 data.
 */ export class Mp4Capture extends Tube { private _active : boolean ; private _capture : boolean ; private _captureCallback : ( buffer : Buffer ) => void ; private _bufferOffset : number ; private readonly _bufferSize : number ; private _buffer : Buffer ; constructor ( maxSize = MAX_CAPTURE_BYTES ) { const incoming = new Transform ( { objectMode : true , transform : ( msg , _encoding , callback ) => { if ( this . _active && msg . type === MessageType . ISOM && msg . tracks !== undefined ) { this . _capture = true ; } if ( this . _capture && msg . type === MessageType . ISOM ) { if ( this . _bufferOffset < this . _buffer . byteLength - msg . data . byteLength ) { msg . data . copy ( this . _buffer , this . _bufferOffset ) ; this . _bufferOffset += msg . data . byteLength ; } else { this . stop ( ) ; } } callback ( undefined , msg ) ; } , } ) ; incoming . on ( 'finish' , ( ) => { this . stop ( ) ; } ) ; super ( incoming ) ; this . _buffer = Buffer . allocUnsafe ( 0 ) ; this . _bufferSize = maxSize ; this . _bufferOffset = 0 ; this . _active = false ; this . _capture = false ; this . _captureCallback = ( ) => { } ; } param callback start ( callback ) { if ( ! this . _active ) { debug ( 'msl:capture:start' ) ( callback ) ; this . _captureCallback = callback ; this . _buffer = Buffer . allocUnsafe ( this . _bufferSize ) ; this . _bufferOffset = 0 ; this . _active = true ; } } /**
     * Deactivate video capture. This ends an ongoing capture and prevents
     * any further capturing.
     */ stop ( ) { if ( this . _active ) { debug ( 'msl:capture:stop' ) ( `captured bytes: ${ this . _bufferOffset }` ) ; try { this . _captureCallback ( this . _buffer . slice ( 0 , this . _bufferOffset ) ) ; } catch ( e ) { console . error ( e ) ; } this . _buffer = Buffer . allocUnsafe ( 0 ) ; this . _bufferOffset = 0 ; this . _active = false ; this . _capture = false ; } } }