import { __awaiter } from "tslib";
import { genId, setVP9Video } from './utils';
import { EventEmitter } from 'ee-ts';
export class SenderSession extends EventEmitter {
    constructor(peerSdp, from, conf, log) {
        super();
        this.log = log;
        this.id = genId();
        this.from = from;
        this.pc = new RTCPeerConnection(conf);
        setVP9Video(this.pc);
        // this.pc.addTransceiver('video', { direction: 'sendonly' });
        this.pc.oniceconnectionstatechange = (e) => this.iceConnectionState(e);
        this.pc.onicecandidate = this.iceCandidate.bind(this);
        this.stream = null;
        this.peerSdp = peerSdp;
        this.closeReason = null;
    }
    close() {
        this.pc.close();
        if (this.stream) {
            this.stream.getTracks().forEach((track) => track.stop());
            this.stream = null;
        }
        this.emit('close', this);
        this.off('*');
    }
    start(stream) {
        return __awaiter(this, void 0, void 0, function* () {
            const pc = this.pc;
            try {
                yield pc.setRemoteDescription({
                    type: 'offer',
                    sdp: this.peerSdp,
                });
                this.stream = stream;
                stream.getTracks().forEach((track) => {
                    this.onTrackStop(track);
                    pc.addTrack(track, stream);
                });
                const answer = yield pc.createAnswer();
                yield pc.setLocalDescription(answer);
            }
            catch (e) {
                throw e;
            }
        });
    }
    onTrackStop(t) {
        t.onmute = () => {
            // TODO
            // this.closeReason = 'mute stream'
            // this.close()
        };
        t.onended = () => {
            this.closeReason = 'ended stream';
            this.close();
        };
    }
    iceConnectionState(e) {
        this.log.debug('sender iceConnectionState ', this.pc.iceConnectionState);
        const pc = this.pc;
        switch (pc.iceConnectionState) {
            case 'disconnected':
            case 'failed':
                this.close();
                break;
            default:
                this.log.debug(`unhandled ICE connection state: ${this.pc.iceConnectionState}`);
        }
    }
    iceCandidate(e) {
        return __awaiter(this, void 0, void 0, function* () {
            const pc = this.pc;
            this.log.debug(`ICE candidate state: ${pc.iceGatheringState}`);
            if (pc.iceGatheringState !== 'complete') {
                return;
            }
            try {
                this.emit('stream', pc.localDescription);
                // tslint:disable-next-line: no-console
                // console.info('sender local sdp\n', pc.localDescription!.sdp)
                // tslint:disable-next-line: no-console
                // tslint:disable-next-line: no-console
                // console.info('sender remote sdp\n', this.peerSdp)
                // tslint:disable-next-line: no-console
            }
            catch (e) {
                this.close();
                throw e;
            }
        });
    }
}
//# sourceMappingURL=sender.js.map