Files
Testing/videocall3/RTCPeerConnection/RTCPeerConnection.js
sean 0e8a8b3028
Some checks are pending
deploy-testing / deploy (push) Waiting to run
Initial testing snapshot
2026-11-17 16:15:01 -05:00

96 lines
3.3 KiB
JavaScript

/* MIT License: https://webrtc-experiment.appspot.com/licence/ */
window.PeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection || window.RTCPeerConnection;
window.SessionDescription = window.RTCSessionDescription || window.mozRTCSessionDescription || window.RTCSessionDescription;
window.IceCandidate = window.RTCIceCandidate || window.mozRTCIceCandidate || window.RTCIceCandidate;
window.defaults = {
iceServers: { "iceServers": [{ "url": "stun:stun.l.google.com:19302" }] },
constraints: { 'mandatory': { 'OfferToReceiveAudio': true, 'OfferToReceiveVideo': true } }
};
var RTCPeerConnection = function(options) {
var iceServers = options.iceServers || defaults.iceServers;
var constraints = options.constraints || defaults.constraints;
var peerConnection = new PeerConnection(iceServers);
peerConnection.onicecandidate = onicecandidate;
peerConnection.onaddstream = onaddstream;
peerConnection.addStream(options.stream);
function onicecandidate(event) {
if (!event.candidate || !peerConnection) return;
if (options.getice) options.getice(event.candidate);
}
function onaddstream(event) {
options.gotstream && options.gotstream(event);
}
function createOffer() {
if (!options.onoffer) return;
peerConnection.createOffer(function(sessionDescription) {
/* opus? use it dear! */
options.isopus && (sessionDescription = codecs.opus(sessionDescription));
peerConnection.setLocalDescription(sessionDescription);
options.onoffer(sessionDescription);
}, null, constraints);
}
createOffer();
function createAnswer() {
if (!options.onanswer) return;
peerConnection.setRemoteDescription(new window.SessionDescription(options.offer));
peerConnection.createAnswer(function(sessionDescription) {
/* opus? use it dear! */
options.isopus && (sessionDescription = codecs.opus(sessionDescription));
peerConnection.setLocalDescription(sessionDescription);
options.onanswer(sessionDescription);
}, null, constraints);
}
createAnswer();
return {
/* offerer got answer sdp; MUST pass sdp over this function */
onanswer: function(sdp) {
peerConnection.setRemoteDescription(new window.SessionDescription(sdp));
},
/* got ICE from other end; MUST pass those candidates over this function */
addice: function(candidate) {
peerConnection.addIceCandidate(new IceCandidate({
sdpMLineIndex: candidate.sdpMLineIndex,
candidate: candidate.candidate
}));
}
};
};
var URL = window.webkitURL || window.URL;
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia;
function getUserMedia(options) {
navigator.getUserMedia(options.constraints || { audio: true, video: true },
function (stream) {
if (options.video)
if (!navigator.mozGetUserMedia) options.video.src = URL.createObjectURL(stream);
else options.video.mozSrcObject = stream;
options.onsuccess && options.onsuccess(stream);
return stream;
}, options.onerror);
}