import Promise from 'bluebird'; import JanusPluginMessage from '../../client/misc/plugin-message'; import MediaEntityPlugin from './media-entity-plugin'; class MediaAudioPlugin extends MediaEntityPlugin { _join(id: string | number, options: any): Promise { let body = Object.assign({ request: 'join' }, options); return this.sendWithTransaction({ body }).then(response => { this.setCurrentEntity(id); return response; }); } leave(): Promise { return this.sendWithTransaction({ body: { request: 'leave' } }).then(response => { this.resetCurrentEntity(); return response; }); } _change(id: string | number, options: any): Promise { let body = Object.assign({ request: 'changeroom' }, options); return this.sendWithTransaction({ body }).then(response => { this.setCurrentEntity(id); return response; }); } _connect(id: string | number, options: any): Promise { if (this.hasCurrentEntity(id)) { return Promise.resolve(new JanusPluginMessage({}, this)); } if (this.hasCurrentEntity()) { return this._change(id, options); } return this._join(id, options); } list(): Promise { return this._list(); } configure(options: any, jsep: RTCSessionDescription): Promise { let body = Object.assign({ request: 'configure' }, options); let message: any = { body }; if (jsep) { message.jsep = jsep; } return this.sendWithTransaction(message); } offerStream(stream: MediaStream, offerOptions: RTCOfferOptions, configureOptions: any): Promise { return Promise.try(() => { this.createPeerConnection(); stream.getAudioTracks().forEach(track => this.addTrack(track, stream)); }) .then(() => this.createOffer(offerOptions)) .then(jsep => this.sendSDP(jsep, configureOptions)); } sendSDP(jsep: RTCSessionDescription, options: any): Promise { return this.configure(options, jsep).then(response => { let jsep = response.get('jsep'); if (jsep) { this.setRemoteSDP(jsep); return jsep; } return Promise.reject(new Error('Failed sendSDP. No jsep in response.')); }); } _listParticipants(options: any): Promise { let body = Object.assign({ request: 'listparticipants' }, options); return this.sendWithTransaction({ body }); } } export default MediaAudioPlugin;