UNPKG

multipeer

Version:

A javascript library that implemets the essential functionalities of WebRTC to make it easier to implement a multiple peer connections typically needed for a video conferencing application.

95 lines (94 loc) 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Multipeer = exports.RTCEventType = void 0; var RTCEventType; (function (RTCEventType) { RTCEventType["NEW_ICE_CANDIDATE"] = "NEW_ICE_CANDIDATE"; RTCEventType["VIDEO_OFFER"] = "VIDEO_OFFER"; RTCEventType["VIDEO_ANSWER"] = "VIDEO_ANSWER"; RTCEventType["NEW_TRACK"] = "NEW_TRACK"; })(RTCEventType = exports.RTCEventType || (exports.RTCEventType = {})); var Multipeer = /** @class */ (function () { // Parameters may be declared in a variety of syntactic forms /** * @param {RTCConfiguration} config - RTCConfiguration object with ICE Server config etc. * @param {Function} signalFunction - a callback function which will be called every time an RTC event occurs. An object of type RTCEvent is passed to this signal function. * @param {MediaStreamConstraints=} mediaStreamConstraints <optional> - MediaStreamConstraints if any */ function Multipeer(config, signalFunction, mediaStreamConstraints) { var _this = this; this._allPeerConnections = {}; this._localStream = new MediaStream(); this._remoteStream = new Array(); this._defaultMediaStreamConstraints = { audio: true, video: true, }; this.initializeLocalStream = function (mediaConstraints) { navigator.mediaDevices.getUserMedia(mediaConstraints) .then(function (localStream) { return _this._localStream = localStream; }); }; this.createPeerConnection = function (id) { if (_this._allPeerConnections[id] !== undefined) { throw (new Error("Id is not unique")); } _this._allPeerConnections[id] = new RTCPeerConnection(_this._rtcPeerConfig); _this._allPeerConnections[id].onicecandidate = function (event) { return _this.handleICECandidateEvent(id, event); }; _this._allPeerConnections[id].ontrack = function (event) { return _this.handleTrackEvent(id, event); }; _this._localStream.getTracks().forEach(function (track) { _this._allPeerConnections[id].sender = _this._allPeerConnections[id].addTrack(track, _this._localStream); }); }; this.handleICECandidateEvent = function (id, event) { if (event.candidate) { _this._signalFunction({ id: id, event: RTCEventType.NEW_ICE_CANDIDATE, candidate: event.candidate, }); } }; this.handleTrackEvent = function (id, event) { if (event.streams && event.streams[0]) { _this._signalFunction({ id: id, event: RTCEventType.NEW_TRACK, stream: event.streams[0], }); } }; this.createNewOffer = function (id) { _this._allPeerConnections[id].createOffer() .then(function (offer) { return _this._allPeerConnections[id].setLocalDescription(offer); }) .then(function () { _this._signalFunction({ id: id, event: RTCEventType.VIDEO_OFFER, sdp: _this._allPeerConnections[id].localDescription }); }); }; this.handleVideoOffer = function (id, sdp) { var sessionDescription = new RTCSessionDescription(sdp); _this._allPeerConnections[id].setRemoteDescription(sessionDescription) .then(function () { return _this._allPeerConnections[id].createAnswer(); }) .then(function (answer) { return _this._allPeerConnections[id].setLocalDescription(answer); }) .then(function () { _this._signalFunction({ id: id, event: RTCEventType.VIDEO_ANSWER, sdp: _this._allPeerConnections[id].localDescription }); }); }; this.handleVideoAnswer = function (id, sdp) { var sessionDescription = new RTCSessionDescription(sdp); _this._allPeerConnections[id].setRemoteDescription(sessionDescription); }; this._rtcPeerConfig = config; this._signalFunction = signalFunction; this.initializeLocalStream(mediaStreamConstraints || this._defaultMediaStreamConstraints); } return Multipeer; }()); exports.Multipeer = Multipeer;