import { __awaiter } from "tslib";
import { EventEmitter } from 'events';
const RTCState = {
    Init: 0,
    Processing: 1,
    Ready: 2,
    Error: 3,
};
const timeout = 2000;
export class RTC extends EventEmitter {
    constructor() {
        super();
        this.localStream = null;
        this.peerStream = null;
        this.timer = null;
        this.state = RTCState.Init;
        const configuration = {};
        // const pcConstraints = {"optional": [{"DtlsSrtpKeyAgreement": true}]};
        this.pc = new RTCPeerConnection(configuration);
        this.pc.addEventListener('track', this.gotRemoteStream.bind(this));
        this.pc.addEventListener('icegatheringstatechange', this.iceGatherIngStateChange.bind(this));
    }
    createOffer() {
        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
            this.once('ready', () => {
                resolve(this);
            });
            this.once('failed', reject);
            try {
                yield this._createOffer();
            }
            catch (e) {
                reject(e);
            }
        }));
    }
    handleAnswerFromOffer(answer) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.pc.setRemoteDescription(new RTCSessionDescription(answer));
        });
    }
    createAnswer(offer) {
        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
            this.once('ready', () => {
                resolve(this);
            });
            this.once('failed', reject);
            try {
                yield this._createAnswer(offer);
            }
            catch (e) {
                reject(e);
            }
        }));
    }
    _createAnswer(offer) {
        return __awaiter(this, void 0, void 0, function* () {
            yield this.pc.setRemoteDescription(new RTCSessionDescription(offer));
            const answer = yield this.pc.createAnswer();
            yield this.pc.setLocalDescription(answer);
            this.timer = setTimeout(() => {
                this.clean();
                this.emit('failed');
            }, timeout);
        });
    }
    _createOffer() {
        return __awaiter(this, void 0, void 0, function* () {
            const offer = yield this.pc.createOffer();
            yield this.pc.setLocalDescription(offer);
            this.timer = setTimeout(() => {
                this.clean();
                this.emit('failed');
            }, timeout);
        });
    }
    clean() {
        clearTimeout(this.timer);
        this.timer = null;
        this.pc.removeEventListener('track', this.gotRemoteStream.bind(this));
        this.pc.removeEventListener('icegatheringstatechange', this.iceGatherIngStateChange.bind(this));
        this.pc.close();
        if (this.localStream) {
            this.localStream.getTracks().forEach((track) => track.stop());
        }
        if (this.peerStream && this.peerStream.getTracks) {
            this.peerStream.getTracks().forEach((track) => track.stop());
        }
    }
    initLocalMediaStream() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.localStream) {
                this.localStream = yield navigator.mediaDevices.getUserMedia({
                    audio: true,
                    video: false,
                });
                this.localStream.getTracks().forEach((track) => this.pc.addTrack(track));
                this.emit('localStreams', this.localStream);
            }
            return this.localStream;
        });
    }
    get remoteSdp() {
        return this.pc.remoteDescription.sdp;
    }
    get localSdp() {
        return this.pc.localDescription.sdp;
    }
    gotRemoteStream(e) {
        this.peerStream = e.streams[0];
        this.emit('peerStreams', this.peerStream);
    }
    iceGatherIngStateChange(e) {
        if (this.state !== RTCState.Ready &&
            this.pc.iceGatheringState === 'complete') {
            clearTimeout(this.timer);
            this.state = RTCState.Ready;
            this.emit('ready');
        }
    }
}
//# sourceMappingURL=rtc.js.map