UNPKG

traceablepeerconnection

Version:

Lowlevel RTCPeerConnection wrapper which allows tracing the API calls

220 lines (200 loc) 7.22 kB
// based on https://github.com/ESTOS/strophe.jingle/ // adds wildemitter support var util = require('util'); var adapter = require('webrtc-adapter'); // jshint ignore:line var WildEmitter = require('wildemitter'); function dumpSDP(description) { return { type: description.type, sdp: description.sdp }; } function dumpStream(stream) { var info = { label: stream.id, }; if (stream.getAudioTracks().length) { info.audio = stream.getAudioTracks().map(function (track) { return track.id; }); } if (stream.getVideoTracks().length) { info.video = stream.getVideoTracks().map(function (track) { return track.id; }); } return info; } function TraceablePeerConnection(config, constraints) { var self = this; WildEmitter.call(this); this.peerconnection = new window.RTCPeerConnection(config, constraints); this.trace = function (what, info) { self.emit('PeerConnectionTrace', { time: new Date(), type: what, value: info || "" }); }; this.onicecandidate = null; this.peerconnection.onicecandidate = function (event) { self.trace('onicecandidate', event.candidate); if (self.onicecandidate !== null) { self.onicecandidate(event); } }; this.onaddstream = null; this.peerconnection.onaddstream = function (event) { self.trace('onaddstream', dumpStream(event.stream)); if (self.onaddstream !== null) { self.onaddstream(event); } }; this.onremovestream = null; this.peerconnection.onremovestream = function (event) { self.trace('onremovestream', dumpStream(event.stream)); if (self.onremovestream !== null) { self.onremovestream(event); } }; this.onsignalingstatechange = null; this.peerconnection.onsignalingstatechange = function (event) { self.trace('onsignalingstatechange', self.signalingState); if (self.onsignalingstatechange !== null) { self.onsignalingstatechange(event); } }; this.oniceconnectionstatechange = null; this.peerconnection.oniceconnectionstatechange = function (event) { self.trace('oniceconnectionstatechange', self.iceConnectionState); if (self.oniceconnectionstatechange !== null) { self.oniceconnectionstatechange(event); } }; this.onnegotiationneeded = null; this.peerconnection.onnegotiationneeded = function (event) { self.trace('onnegotiationneeded'); if (self.onnegotiationneeded !== null) { self.onnegotiationneeded(event); } }; self.ondatachannel = null; this.peerconnection.ondatachannel = function (event) { self.trace('ondatachannel', event); if (self.ondatachannel !== null) { self.ondatachannel(event); } }; this.getLocalStreams = this.peerconnection.getLocalStreams.bind(this.peerconnection); this.getRemoteStreams = this.peerconnection.getRemoteStreams.bind(this.peerconnection); } util.inherits(TraceablePeerConnection, WildEmitter); ['signalingState', 'iceConnectionState', 'localDescription', 'remoteDescription'].forEach(function (prop) { Object.defineProperty(TraceablePeerConnection.prototype, prop, { get: function () { return this.peerconnection[prop]; } }); }); TraceablePeerConnection.prototype.addStream = function (stream) { this.trace('addStream', dumpStream(stream)); this.peerconnection.addStream(stream); }; TraceablePeerConnection.prototype.removeStream = function (stream) { this.trace('removeStream', dumpStream(stream)); this.peerconnection.removeStream(stream); }; TraceablePeerConnection.prototype.createDataChannel = function (label, opts) { this.trace('createDataChannel', label, opts); return this.peerconnection.createDataChannel(label, opts); }; TraceablePeerConnection.prototype.setLocalDescription = function (description, successCallback, failureCallback) { var self = this; this.trace('setLocalDescription', dumpSDP(description)); return this.peerconnection.setLocalDescription( description ).then( function () { self.trace('setLocalDescriptionOnSuccess'); if (successCallback) successCallback(); }, function (err) { self.trace('setLocalDescriptionOnFailure', err); if (failureCallback) failureCallback(err); } ); }; TraceablePeerConnection.prototype.setRemoteDescription = function (description, successCallback, failureCallback) { var self = this; this.trace('setRemoteDescription', dumpSDP(description)); return this.peerconnection.setRemoteDescription( description ).then( function () { self.trace('setRemoteDescriptionOnSuccess'); if (successCallback) successCallback(); }, function (err) { self.trace('setRemoteDescriptionOnFailure', err); if (failureCallback) failureCallback(err); } ); }; TraceablePeerConnection.prototype.close = function () { this.trace('stop'); if (this.peerconnection.signalingState != 'closed') { this.peerconnection.close(); } }; TraceablePeerConnection.prototype.createOffer = function (successCallback, failureCallback, constraints) { var self = this; this.trace('createOffer', constraints); return this.peerconnection.createOffer( constraints ).then( function (offer) { self.trace('createOfferOnSuccess', dumpSDP(offer)); if (successCallback) successCallback(offer); }, function (err) { self.trace('createOfferOnFailure', err); if (failureCallback) failureCallback(err); } ); }; TraceablePeerConnection.prototype.createAnswer = function (successCallback, failureCallback, constraints) { var self = this; this.trace('createAnswer', constraints); return this.peerconnection.createAnswer( constraints ).then( function (answer) { self.trace('createAnswerOnSuccess', dumpSDP(answer)); if (successCallback) successCallback(answer); }, function (err) { self.trace('createAnswerOnFailure', err); if (failureCallback) failureCallback(err); } ); }; TraceablePeerConnection.prototype.addIceCandidate = function (candidate, successCallback, failureCallback) { var self = this; this.trace('addIceCandidate', candidate); return this.peerconnection.addIceCandidate( candidate ).then( function () { //self.trace('addIceCandidateOnSuccess'); if (successCallback) successCallback(); }, function (err) { self.trace('addIceCandidateOnFailure', err); if (failureCallback) failureCallback(err); } ); }; TraceablePeerConnection.prototype.getStats = function () { this.peerconnection.getStats.apply(this.peerconnection, arguments); }; module.exports = TraceablePeerConnection;