import { inject , injectable } from 'inversify' ; import * as apid from '../../../../../../api' ; import Util from '../../../../util/Util' ; import IStreamApiModel from '../../..//api/streams/IStreamApiModel' ; import IRecordedApiModel from '../../../api/recorded/IRecordedApiModel' ; import IVideoApiModel from '../../../api/video/IVideoApiModel' ; import IRecordedHLSStreamingVideoState from './IRecordedHLSStreamingVideoState' ; import RecordedStreamingVideoState from './RecordedStreamingVideoState' ; @ injectable ( ) class RecordedHLSStreamingVideoState extends RecordedStreamingVideoState implements IRecordedHLSStreamingVideoState { private streamApiModel : IStreamApiModel ; private streamId : apid . StreamId | null = null ; private keepTimerId : number | undefined ; private isStarting : boolean = false ; constructor ( @ inject ( 'IStreamApiModel' ) streamApiModel , @ inject ( 'IVideoApiModel' ) videoApiModel , @ inject ( 'IRecordedApiModel' ) recordedApiModel ) { super ( videoApiModel , recordedApiModel ) ; this . streamApiModel = streamApiModel ; } param videoFileId param playPosition param mode return public async start ( videoFileId , playPosition , mode ) { if ( this . isStarting === true ) { return ; } if ( this . streamId !== null ) { await this . stop ( ) ; } this . isStarting = true ; try { this . streamId = await this . streamApiModel . startRecordedHLS ( videoFileId , playPosition , mode ) ; this . isStarting = false ; } catch ( err ) { this . isStarting = false ; throw err ; } this . keepTimerId = setInterval ( async ( ) => { if ( this . streamId === null ) { return ; } await this . streamApiModel . keep ( this . streamId ) ; } , RecordedHLSStreamingVideoState . KEEP_INTERVAL * 1000 ) ; await Util . sleep ( 1000 ) ; } return public async stop ( ) { if ( typeof this . keepTimerId !== 'undefined' ) { clearInterval ( this . keepTimerId ) ; this . keepTimerId = undefined ; } if ( this . streamId !== null ) { await this . streamApiModel . stop ( this . streamId ) ; this . streamId = null ; } } return public getStreamId ( ) { return this . streamId ; } return public async isEnabled ( ) { const info = await this . streamApiModel . getStreamInfo ( true ) ; for ( const item of info . items ) { if ( item . streamId === this . streamId && item . isEnable === true ) { return true ; } } return false ; } } namespace RecordedHLSStreamingVideoState { export const KEEP_INTERVAL = 10 ; } export default RecordedHLSStreamingVideoState ;