|
| 1 | +import { RtspConfig, RTSP_METHOD } from '../components/rtsp-session' |
| 2 | +import { WSConfig } from '../components/ws-source/openwebsocket' |
| 3 | +import { WSSource } from '../components/ws-source' |
| 4 | +import { AuthConfig, Auth } from '../components/auth' |
| 5 | +import { RtspPipeline } from './rtsp-pipeline' |
| 6 | + |
| 7 | +export interface TransformConfig { |
| 8 | + ws?: WSConfig |
| 9 | + rtsp?: RtspConfig |
| 10 | + auth?: AuthConfig |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Pipeline that can receive the SDP object for an RTS stream. |
| 15 | + * |
| 16 | + * @class WsSdpPipeline |
| 17 | + * @extends {RtspPipeline} |
| 18 | + */ |
| 19 | +export class WsSdpPipeline extends RtspPipeline { |
| 20 | + public onServerClose?: () => void |
| 21 | + public ready: Promise<void> |
| 22 | + |
| 23 | + private _src?: WSSource |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates an instance of Html5VideoPipeline. |
| 27 | + * @param {any} [config={}] Component options |
| 28 | + * @memberof Html5VideoPipeline |
| 29 | + */ |
| 30 | + constructor(config: TransformConfig) { |
| 31 | + const { ws: wsConfig, rtsp: rtspConfig, auth: authConfig } = config |
| 32 | + |
| 33 | + super(rtspConfig) |
| 34 | + |
| 35 | + if (authConfig) { |
| 36 | + const auth = new Auth(authConfig) |
| 37 | + this.insertBefore(this.rtsp, auth) |
| 38 | + } |
| 39 | + |
| 40 | + const waitForWs = WSSource.open(wsConfig) |
| 41 | + this.ready = waitForWs.then(wsSource => { |
| 42 | + wsSource.onServerClose = () => { |
| 43 | + this.onServerClose && this.onServerClose() |
| 44 | + } |
| 45 | + this.prepend(wsSource) |
| 46 | + this._src = wsSource |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + close() { |
| 51 | + this._src && this._src.outgoing.end() |
| 52 | + } |
| 53 | + |
| 54 | + get sdp() { |
| 55 | + return this.ready.then(() => { |
| 56 | + const sdpPromise = new Promise(resolve => { |
| 57 | + this.rtsp.onSdp = resolve |
| 58 | + }) |
| 59 | + this.rtsp.send({ method: RTSP_METHOD.DESCRIBE }) |
| 60 | + this.rtsp.send({ method: RTSP_METHOD.TEARDOWN }) |
| 61 | + return sdpPromise |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
0 commit comments