// WebRTC realtime Calls!!! (part of WebRTC Experiments by Muaz Khan!) @WebRTCWeb
// https://wwww.webrtc-experiment.com/
// Muaz Khan - www.MuazKhan.com
// MIT License - www.webrtc-experiment.com/licence
// Experiments - github.com/muaz-khan/WebRTC-Experiment
var imageURL = document.getElementById('images-video-container-png').src;
var chatSection = getElement('.chat-section'),
chatOutput = getElement('.chat-output'),
chatInput = getElement('.chat-input input');
var channel = location.href.replace(/\/|:|#|%|\.|\[|\]/g, '');
var sender = Math.round(Math.random() * 999999999) + 999999999;
var SIGNALING_SERVER = 'https://socketio-over-nodejs2.herokuapp.com:443/';
io.connect(SIGNALING_SERVER).emit('new-channel', {
channel: channel,
sender: sender
});
var socket = io.connect(SIGNALING_SERVER + channel);
socket.on('connect', function () {
chatInput.disabled = false;
appendMessage('Socket.io connection is opened.', 'System', 'System');
});
socket.on('disconnect', function() {
chatInput.disabled = true;
appendMessage('Socket.io connection is disconnected.', 'System', 'System');
});
socket.on('error', function () {
appendMessage('Socket.io connection failed.', 'System', 'System');
});
socket.send = function (message) {
socket.emit('message', {
sender: sender,
data: message
});
};
var isGecko = !! navigator.mozGetUserMedia;
function store(item, value) {
localStorage.setItem(item, value);
}
function get(item) {
return localStorage.getItem(item);
}
function isSelected(element) {
return element.className.indexOf('selected') != -1;
}
function unSelect(element) {
element.className = element.className.replace(/( selected|selected)/g, '');
}
function getElement(selector) {
return document.querySelector(selector);
}
function getUserMedia(mediaType, callback) {
var hints = {};
if (mediaType == 'audio') {
hints.audio = true;
window.mediaType = 'audio';
} else window.mediaType = 'video';
// ['1920:1080', '1280:720', '960:720', '640:360', '640:480', '320:240', '320:180'];
if (mediaType == 'video') {
hints = {
audio: true,
video: {
optional: [],
mandatory: {}
}
};
}
if (mediaType == 'screen') {
hints = {
audio: false,
video: !!navigator.webkitGetUserMedia ? {
optional: [],
mandatory: {
maxWidth: 1920,
maxHeight: 1080,
chromeMediaSource: 'screen'
}
} : {
mozMediaSource: 'window',
mediaSource: 'window',
maxWidth: 1920,
maxHeight: 1080
}
};
}
navigator.getUserMedia(hints, function (stream) {
if (peer) peer.mediaType = mediaType == 'audio' ? 'audio' : 'video';
callback(stream);
var mediaElement = document.createElement(mediaType == 'audio' ? 'audio' : 'video');
mediaElement[isGecko ? 'mozSrcObject' : 'src'] = isGecko ? stream : (window.URL || window.webkitURL).createObjectURL(stream);
mediaElement.volume = 0;
mainPreview.innerHTML = '';
mainPreview.appendChild(mediaElement);
mediaElement.play();
if (mediaType == 'audio') mediaElement.controls = true;
else takeSnapshot(peer.userid, mediaElement);
}, function(error) {
alert( JSON.stringify(error, null, '\t') );
});
}
var toolBoxCameraIcon = getElement('.camera-icon'),
toolBoxVoiceIcon = getElement('.voice-icon'),
toolBoxScreenIcon = getElement('.screen-icon');
function leaveCamera() {
// leave camera
if (peer && peer.MediaStream) {
peer.MediaStream.stop();
peer.MediaStream = null;
}
mainPreview.innerHTML = '
';
}
toolBoxCameraIcon.onclick = function () {
if (isSelected(this)) {
unSelect(this);
leaveCamera();
} else {
this.className += ' selected';
getUserMedia('video', function (stream) {
if (peer) peer.addStream(stream);
});
}
};
toolBoxVoiceIcon.onclick = function () {
if (isSelected(this)) {
unSelect(this);
leaveCamera();
} else {
this.className += ' selected';
getUserMedia('audio', function (stream) {
if (peer) peer.addStream(stream);
});
}
};
toolBoxScreenIcon.onclick = function () {
if (isSelected(this)) {
unSelect(this);
} else {
this.className += ' selected';
getUserMedia('screen', function (stream) {
if (peer) peer.addStream(stream);
});
}
};
function appendMessage(message, className, userid) {
if (!message.length) return;
var div = document.createElement('div');
div.className = className;
div.innerHTML = '
' + linkify(message) + '';
chatOutput.appendChild(div);
div.tabIndex = 0;
div.style.opacity = 1;
div.focus();
chatInput.focus();
}
chatInput.onkeyup = function (e) {
if (e.keyCode != 13) return;
socket.send({
userid: peer.userid,
customMessage: true,
textChat: this.value
});
appendMessage(this.value, 'You', peer.userid);
this.value = '';
};
var alertMessages = {
'on-page-load': '
\ Other users can call you using your user-id. \
\\ You can call any user using his user-id. \
\\ A person whose user-id is ' + targetUserid + ', is calling. \
\
';
};
peer.onCustomMessage = function (data) {
if (data.textChat) {
playMessageSound();
appendMessage(data.textChat, 'He', data.userid);
}
if (data.mediaType) {
peer.mediaType = data.mediaType;
}
var mediaElement = mainPreview.querySelector('audio') || mainPreview.querySelector('video');
if (data.muted) {
if (data.mediaType == 'video') {
mediaElement.poster = 'images/call-button-bigger.png';
}
if (data.mediaType == 'all' || data.mediaType == 'audio') {
mediaElement.muted = true;
mediaElement.volume = 0;
}
}
if (data.unmuted) {
if (data.mediaType == 'video') {
mediaElement.poster = '';
}
if (data.mediaType == 'all' || data.mediaType == 'audio') {
mediaElement.muted = false;
mediaElement.volume = 1;
}
mediaElement.play();
}
if (data.sendYourSnapshot) {
socket.send({
userid: peer.userid,
customMessage: true,
snapshot: snapshots.userSnapshot || false
});
}
if (data.snapshot) {
snapshots.targetUserSnapshot = data.snapshot;
}
};
function askForSnapshot() {
socket.send({
userid: peer.userid,
customMessage: true,
sendYourSnapshot: true
});
}
var muteUnmuteVoice = getElement('.call-control-section .voice'),
muteUnmuteVideo = getElement('.call-control-section .video');
muteUnmuteVoice.onclick = function () {
if (this.className.indexOf('no-voice') == -1) {
this.className += ' selected';
this.className = this.className.replace('voice', 'no-voice');
socket.send({
userid: peer.userid,
customMessage: true,
mediaType: 'audio',
muted: true
});
if (peer) {
if (peer.MediaStream.getAudioTracks()[0]) {
peer.MediaStream.getAudioTracks()[0].enabled = false;
}
}
} else {
unSelect(this);
this.className = this.className.replace('no-voice', 'voice');
socket.send({
userid: peer.userid,
customMessage: true,
mediaType: 'audio',
unmuted: true
});
if (peer) {
if (peer.MediaStream.getAudioTracks()[0]) {
peer.MediaStream.getAudioTracks()[0].enabled = true;
}
}
}
};
muteUnmuteVideo.onclick = function () {
if (this.className.indexOf('no-video') == -1) {
this.className += ' selected';
this.className = this.className.replace('video', 'no-video');
socket.send({
userid: peer.userid,
customMessage: true,
mediaType: 'video',
muted: true
});
if (peer) {
peer.MediaStream.getVideoTracks()[0].enabled = false;
}
} else {
unSelect(this);
this.className = this.className.replace('no-video', 'video');
socket.send({
userid: peer.userid,
customMessage: true,
mediaType: 'video',
unmuted: true
});
if (peer) {
peer.MediaStream.getVideoTracks()[0].enabled = true;
}
}
};
function takeSnapshot(userid, video) {
setTimeout(function () {
var canvas = document.createElement('canvas');
canvas.width = video.videoWidth || video.clientWidth;
canvas.height = video.videoHeight || video.clientHeight;
var context = canvas.getContext('2d');
context.drawImage(video, 0, 0, canvas.width, canvas.height);
snapshots[userid] = canvas.toDataURL();
if (userid == peer.userid) {
snapshots.userSnapshot = canvas.toDataURL();
store('user-snapshot', canvas.toDataURL());
} else {
snapshots.targetUserSnapshot = canvas.toDataURL();
store('target-user-snapshot', snapshots[userid]);
}
}, 2000);
}
var snapshots = {
userSnapshot: get('user-snapshot'),
targetUserSnapshot: get('target-user-snapshot')
};
askForSnapshot();
var messageSound = document.querySelector('#message-sound');
function playMessageSound() {
messageSound.play();
}