rtcpeerconnection
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "rtcpeerconnection", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "A tiny browser module that gives normalizes and simplifies the API for WebRTC peer connections.", | ||
@@ -18,3 +18,4 @@ "main": "rtcpeerconnection.js", | ||
"dependencies": { | ||
"wildemitter": "0.0.5" | ||
"wildemitter": "0.0.5", | ||
"webrtcsupport": "0.3.1" | ||
}, | ||
@@ -21,0 +22,0 @@ "devDependencies": { |
140
README.md
# PeerConnection | ||
## Warning | ||
This is fresh and untested! | ||
## What is this? | ||
@@ -25,2 +21,138 @@ | ||
### Instantiation | ||
Instantiation takes the same options as a normal peer connection constructor: | ||
```js | ||
var PeerConnection = require('rtcpeerconnection'); | ||
// init it like a normal peer connection object | ||
// passing in ice servers/constraints | ||
var pc = new PeerConnection({servers}, {constraints}); | ||
``` | ||
### Events | ||
Unlike stock Peer Connections this is an event emitter. Powered by [WildEmitter](http://github.com/henrikjoreteg/wildemitter) which has a very familiar API if you're used to node.js/jQUery/Backbone but also includes a wildcard handler so you can easily debug events. Just do `emitter.on('*')` to log them out or whatnot. | ||
But instead of doing `pc.onicecandidate = function () {}` on a peer connection you listen for events like this: | ||
```js | ||
// ice candidates | ||
pc.on('ice', function (candidate) { | ||
// it's your job to send these to someone | ||
connection.send('ice', candidate); | ||
}); | ||
// you can listen for end of candidates (not particularly useful) | ||
pc.on('endOfCandidates', function () { | ||
// no more ice candidates | ||
}); | ||
// remote stream added | ||
pc.on('streamAdded', function (stream) { | ||
// do something with the remote stream | ||
// probably attach it to a <video> element | ||
// and play it. | ||
}); | ||
// remote stream removed | ||
pc.on('streamRemoved', function (stream) { | ||
// remote stream removed | ||
// now you could hide/disable removed video | ||
}); | ||
// you can chose to listen for events for | ||
// offers and answers instead, if you prefer | ||
pc.on('answer', function (answer) { ... }); | ||
pc.on('offer', function (offer) { ... }); | ||
// on peer connection close | ||
pc.on('close', function () { ... }); | ||
``` | ||
### Methods | ||
The whole offer/answer cycle looks like this: | ||
```js | ||
// assumptions | ||
var pc = new PeerConnection(servers, constraints); | ||
var connection = new RealTimeConnection(); // could be socket.io or whatever | ||
// create an offer | ||
pc.offer(function (offer) { | ||
connection.send('offer', offer) | ||
}); | ||
// you can also optionally pass in constraints | ||
// when creating an offer. | ||
pc.offer({ | ||
mandatory: { | ||
OfferToReceiveAudio: true, | ||
OfferToReceiveVideo: false | ||
} | ||
}, | ||
function (offer) { | ||
// offer | ||
} | ||
); | ||
// when you get an offer, you can answer | ||
// with various options | ||
connection.on('offer', function (offer) { | ||
// you can just call answer | ||
pc.answer(offer, function (answer) { | ||
connection.send('answer', answer); | ||
}); | ||
// you can call answer with contstraints | ||
pc.answer(offer, MY_CONSTRAINTS, function (answer) { | ||
connection.send('answer', answer); | ||
}); | ||
// or you can use one of the shortcuts answers | ||
// for video only | ||
pc.answerVideoOnly(offer, function (answer) { ... }); | ||
// and audio only | ||
pc.answerAudioOnly(offer, function (answer) { ... }); | ||
}); | ||
// when you get an answer, you just call | ||
// handleAnswer | ||
connection.on('answer', function (answer) { | ||
pc.handleAnswer(answer); | ||
}); | ||
// the only other thing you have to do is listen, transmit, and process ice candidates | ||
// you have to send them when generated | ||
pc.on('ice', function (candidate) { | ||
connection.send('ice', candidate); | ||
}); | ||
// process incoming ones | ||
connection.on('ice', function (candidate) { | ||
pc.processIce(candidate); | ||
}); | ||
``` | ||
That's it! | ||
## More | ||
If you want higher level functionality look at [SimpleWebRTC](http://simplewebrtc.com) that uses this library. | ||
## License | ||
@@ -27,0 +159,0 @@ |
(function(e){if("function"==typeof bootstrap)bootstrap("peerconnection",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makePeerConnection=e}else"undefined"!=typeof window?window.PeerConnection=e():global.PeerConnection=e()})(function(){var define,ses,bootstrap,module,exports; | ||
return (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){ | ||
var WildEmitter = require('wildemitter'); | ||
var webrtc = require('webrtcsupport'); | ||
// The RTCPeerConnection object. | ||
var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; | ||
// The RTCSessionDescription object. | ||
var RTCSessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription; | ||
// The RTCIceCandidate object. | ||
var RTCIceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate; | ||
function PeerConnection(config, constraints) { | ||
this.pc = new RTCPeerConnection(config, constraints); | ||
this.pc = new webrtc.PeerConnection(config, constraints); | ||
WildEmitter.call(this); | ||
this.pc.onicemessage = this._onIce.bind(this); | ||
this.pc.onicecandidate = this._onIce.bind(this); | ||
this.pc.onaddstream = this._onAddStream.bind(this); | ||
this.pc.onremovestream = this._onRemoveStream.bind(this); | ||
if (config.debug) { | ||
this.on('*', function (eventName, event) { | ||
console.log('PeerConnection event:', eventName, event); | ||
}); | ||
} | ||
} | ||
@@ -35,15 +33,19 @@ | ||
PeerConnection.prototype._onIce = function (event) { | ||
this.emit('ice', event.candidate); | ||
if (event.candidate) { | ||
this.emit('ice', event.candidate); | ||
} else { | ||
this.emit('endOfCandidates'); | ||
} | ||
}; | ||
PeerConnection.prototype._onAddStream = function () { | ||
PeerConnection.prototype._onAddStream = function (event) { | ||
this.emit('addStream', event); | ||
}; | ||
PeerConnection.prototype._onRemoveStream = function () { | ||
PeerConnection.prototype._onRemoveStream = function (event) { | ||
this.emit('removeStream', event); | ||
}; | ||
PeerConnection.prototype.processIce = function (candidate) { | ||
this.pc.addIceCandidate(new RTCIceCandidate(candidate)); | ||
this.pc.addIceCandidate(new webrtc.IceCandidate(candidate)); | ||
}; | ||
@@ -59,2 +61,3 @@ | ||
}; | ||
var callback = arguments.length === 2 ? cb : constraints; | ||
@@ -64,3 +67,3 @@ this.pc.createOffer(function (sessionDescription) { | ||
self.emit('offer', sessionDescription); | ||
cb && cb(sessionDescription) | ||
if (callback) callback(sessionDescription); | ||
}, null, mediaConstraints); | ||
@@ -92,7 +95,8 @@ }; | ||
PeerConnection.prototype._answer = function (offer, constraints, cb) { | ||
this.setRemoteDescription(new RTCSessionDescription(offer)); | ||
this.createAnswer(function (sessionDescription) { | ||
var self = this; | ||
this.pc.setRemoteDescription(new webrtc.SessionDescription(offer)); | ||
this.pc.createAnswer(function (sessionDescription) { | ||
self.pc.setLocalDescription(sessionDescription); | ||
self.emit('answer', sessionDescription); | ||
cb && cb(sessionDescription); | ||
if (cb) cb(sessionDescription); | ||
}, null, constraints); | ||
@@ -103,5 +107,5 @@ }; | ||
var self = this; | ||
var threeArgs = arguments.length === 3; | ||
var callback = threeArgs ? cb : constraints; | ||
var mediaConstraints = threeArgs ? constraints : { | ||
var hasConstraints = arguments.length === 3; | ||
var callback = hasConstraints ? cb : constraints; | ||
var mediaConstraints = hasConstraints ? constraints : { | ||
mandatory: { | ||
@@ -113,5 +117,9 @@ OfferToReceiveAudio: true, | ||
this._answer(offer, mediaConstraints, cb); | ||
this._answer(offer, mediaConstraints, callback); | ||
}; | ||
PeerConnection.prototype.handleAnswer = function (answer) { | ||
this.pc.setRemoteDescription(new webrtc.SessionDescription(answer)); | ||
}; | ||
PeerConnection.prototype.close = function () { | ||
@@ -124,3 +132,30 @@ this.pc.close(); | ||
},{"wildemitter":2}],2:[function(require,module,exports){ | ||
},{"webrtcsupport":2,"wildemitter":3}],2:[function(require,module,exports){ | ||
// created by @HenrikJoreteg | ||
var PC = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; | ||
var IceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate; | ||
var SessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription; | ||
var prefix = function () { | ||
if (window.mozRTCPeerConnection) { | ||
return 'moz'; | ||
} else if (window.webkitRTCPeerConnection) { | ||
return 'webkit'; | ||
} | ||
}(); | ||
var screenSharing = navigator.userAgent.match('Chrome') && parseInt(navigator.userAgent.match(/Chrome\/(.*) /)[1], 10) >= 26; | ||
var webAudio = !!window.webkitAudioContext; | ||
// export support flags and constructors | ||
module.exports = { | ||
support: !!PC, | ||
dataChannel: !!(PC && PC.prototype.createDataChannel), | ||
prefix: prefix, | ||
webAudio: webAudio, | ||
screenSharing: screenSharing, | ||
PeerConnection: PC, | ||
SessionDescription: SessionDescription, | ||
IceCandidate: IceCandidate | ||
}; | ||
},{}],3:[function(require,module,exports){ | ||
/* | ||
@@ -127,0 +162,0 @@ WildEmitter.js is a slim little event emitter by @henrikjoreteg largely based |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17689
352
165
2
+ Addedwebrtcsupport@0.3.1
+ Addedwebrtcsupport@0.3.1(transitive)