//start getUserMedia
function start() {
trace("Get audio or video by webcam.");
//Set attribute
var id = "videoLocal";
var isAudio = false;
var isVideo = true;
//Set constraints
var constraints =
{
video: isVideo,
audio: isAudio
};
//Set success handler
function success(mediaStream) {
trace("GetUserMedia Successful");
localStream = mediaStream;
video = document.getElementById(id);
//output video on videoLocal
attachMediaStream(video, mediaStream);
}
//Set error handler
function error(e) {
trace("Error by getUserMedia: " + e);
}
//Start getUserMedia
getUserMedia(constraints, success, error);
//Set button
startBtn.disabled = true;
callBtn.disabled = false;
hangUpBtn.disabled = false;
}
function getLocalDescription(desc) {
//local peer set local description
peerLocalConnection.setLocalDescription(desc);
trace("Remote peer get offer from local peer. \n" + desc.sdp);
//remote peer set remote description
peerRemoteConnection.setRemoteDescription(desc);
peerRemoteConnection.createAnswer(getRemoteDescription, descriptionError);
}
function getRemoteDescription(desc) {
//remote peer set local description
peerRemoteConnection.setLocalDescription(desc);
trace("Local peer get offer from remote peer. \n" + desc.sdp);
//local peer set remote description
peerLocalConnection.setRemoteDescription(desc);
}
function descriptionError(error) {
trace("Failed to create description : " + error.toString());
}
function SetLocalPeerConnection() {
peerLocalConnection = new RTCPeerConnection();
function localIceCallBack(event) {
if (event.candidate) {
//send local peer candidate to remote
peerRemoteConnection.addIceCandidate(new RTCIceCandidate(event.candidate),
addIceCandidateSuccess, addIceCandidateError);
trace("Local peer send local candidate : " + event.candidate.candidate + " to remote peer");
}
}
peerLocalConnection.onicecandidate = localIceCallBack;
}
function SetRemotePeerConnection() {
peerRemoteConnection = new RTCPeerConnection();
var id = document.getElementById("videoRemote");
function remoteIceCallBack(event) {
if (event.candidate) {
//send remote peer candidate to local
peerLocalConnection.addIceCandidate(new RTCIceCandidate(event.candidate),
addIceCandidateSuccess, addIceCandidateError);
trace("Remote peer send remote candidate : " + event.candidate.candidate + " to local peer");
}
}
function getStream(e) {
trace("Remote get local media stream.");
attachMediaStream(id, e.stream);
}
peerRemoteConnection.onicecandidate = remoteIceCallBack;
peerRemoteConnection.onaddstream = getStream;
}
function addIceCandidateSuccess() {
trace('AddIceCandidate success.');
}
function addIceCandidateError(error) {
trace('Failed to add ICE Candidate: ' + error.toString());
}