mirror of
https://api.glitch.com/git/yaswvc
synced 2026-01-12 03:18:10 +00:00
226 lines
9 KiB
JavaScript
226 lines
9 KiB
JavaScript
var Vue = window.Vue;
|
|
var Peer = window.Peer;
|
|
var app = new Vue({
|
|
el: '#app',
|
|
data: {
|
|
peer: new Peer(localStorage.getItem('yaswvc-peerId'), {
|
|
host: '/',
|
|
path: '/peerjs/myapp',
|
|
config: {
|
|
'iceServers': [
|
|
{ urls: 'stun:stun.snikket.freeddns.org:3478' },
|
|
{
|
|
urls: 'turn:turn.snikket.freeddns.org:3478',
|
|
username: 'guest',
|
|
credential: 'somepassword'
|
|
}
|
|
],
|
|
'sdpSemantics': 'unified-plan',
|
|
},
|
|
debug: 3
|
|
}),
|
|
stream: null,
|
|
calls: [],
|
|
connections: [],
|
|
chatInput: '',
|
|
},
|
|
methods: {
|
|
logMessage: function(message) {
|
|
let newMessage = document.createElement('div');
|
|
newMessage.innerText = message;
|
|
document.querySelector('.messages').appendChild(newMessage);
|
|
},
|
|
renderVideo:function(stream, selector = '#remoteVideo') {
|
|
console.log('renderVideo', {stream, selector});
|
|
document.querySelector(selector).srcObject = stream;
|
|
},
|
|
connectToPeer: function(peerId) {
|
|
if (peerId === this.peer.id) {
|
|
this.logMessage(`You played yourself! Can't connect to yourself.`);
|
|
return;
|
|
};
|
|
this.logMessage(`Connecting to ${peerId}...`);
|
|
|
|
let conn = this.peer.connect(peerId);
|
|
this.connections.push(conn);
|
|
console.log('[Out] connected', conn);
|
|
conn.on('data', (data) => {
|
|
this.logMessage(`${conn.peer}: ${data}`);
|
|
});
|
|
conn.on('open', () => {
|
|
conn.send('hi!');
|
|
});
|
|
conn.on('close', () => {
|
|
this.connections = this.connections.filter(c => c.connectionId === conn.connectionId);
|
|
this.calls = this.calls.filter(c => c.peer !== conn.peer);
|
|
this.logMessage(`${conn.peer} closed connection.`);
|
|
console.log(`[Out] Connection closed with ${conn.peer}.`);
|
|
});
|
|
conn.on('error', () => {
|
|
this.connections = this.connections.filter(c => c.connectionId === conn.connectionId);
|
|
this.calls = this.calls.filter(c => c.peer !== conn.peer);
|
|
console.warn(`[Out] Connection error ${conn.connectionId} with ${conn.peer}.`);
|
|
});
|
|
|
|
let call = this.peer.call(peerId, this.stream);
|
|
call.on('stream', (stream) => this.renderVideo(stream, '#video-'+call.connectionId));
|
|
call.on('close', () => {
|
|
console.log(`[OUT] Call with ${peerId} was closed.`);
|
|
this.calls = this.calls.filter(c => c.connectionId === call.connectionId);
|
|
});
|
|
this.calls.push(call);
|
|
},
|
|
hangUp: function() {
|
|
this.calls.forEach(c => {
|
|
console.log('hangUp - closing', c);
|
|
c.close();
|
|
});
|
|
this.connections.forEach(c => {
|
|
console.log('hangUp - closing', c);
|
|
c.close();
|
|
});
|
|
},
|
|
sendMessage: function(text = this.chatInput) {
|
|
this.connections.forEach(c => {
|
|
c.send(text);
|
|
this.logMessage(`Me: ${text}`);
|
|
this.chatInput = '';
|
|
});
|
|
},
|
|
listenForPeerEvents: function () {
|
|
this.peer.on('open', (id) => {
|
|
localStorage.setItem('yaswvc-peerId', id);
|
|
});
|
|
this.peer.on('error', (error) => {
|
|
console.error(error);
|
|
});
|
|
|
|
// Handle incoming data connection
|
|
this.peer.on('connection', (conn) => {
|
|
this.connections.push(conn);
|
|
console.log('[INC] incoming peer connection!', conn);
|
|
conn.on('data', (data) => {
|
|
this.logMessage(`${conn.peer}: ${data}`);
|
|
});
|
|
conn.on('open', () => {
|
|
conn.send('hello!');
|
|
});
|
|
conn.on('close', () => {
|
|
this.connections = this.connections.filter(c => c.connectionId === conn.connectionId);
|
|
this.calls = this.calls.filter(c => c.peer !== conn.peer);
|
|
this.logMessage(`${conn.peer} closed connection.`);
|
|
console.log('[Inc] closed conenction', conn)
|
|
});
|
|
conn.on('error', () => {
|
|
this.connections = this.connections.filter(c => c.connectionId === conn.connectionId);
|
|
console.warn(`[Inc] Connection error ${conn.connectionId} with ${conn.peer}.`);
|
|
});
|
|
});
|
|
|
|
// Handle incoming voice/video connection
|
|
this.peer.on('call', (call) => {
|
|
console.log('INCOMING CALL', call);
|
|
call.answer(this.stream); // Answer the call with an A/V stream.
|
|
call.on('stream', (stream) => this.renderVideo(stream, '#video-'+call.connectionId));
|
|
call.on('close', () => {
|
|
console.log(`[INC] Call with ${call.peer} was closed.`);
|
|
this.calls = this.calls.filter(c => c.connectionId === call.connectionId);
|
|
});
|
|
this.calls.push(call);
|
|
});
|
|
},
|
|
gotDevices: function gotDevices(deviceInfos) {
|
|
const selectors = [document.querySelector('select#audioSource'), document.querySelector('select#audioOutput'), document.querySelector('select#videoSource')];
|
|
// Handles being called several times to update labels. Preserve values.
|
|
const values = selectors.map(select => select.value);
|
|
selectors.forEach(select => {
|
|
while (select.firstChild) {
|
|
select.removeChild(select.firstChild);
|
|
}
|
|
});
|
|
for (let i = 0; i !== deviceInfos.length; ++i) {
|
|
const deviceInfo = deviceInfos[i];
|
|
const option = document.createElement('option');
|
|
option.value = deviceInfo.deviceId;
|
|
if (deviceInfo.kind === 'audioinput') {
|
|
option.text = deviceInfo.label || `microphone ${document.querySelector('select#audioSource').length + 1}`;
|
|
document.querySelector('select#audioSource').appendChild(option);
|
|
} else if (deviceInfo.kind === 'audiooutput') {
|
|
option.text = deviceInfo.label || `speaker ${document.querySelector('select#audioOutput').length + 1}`;
|
|
document.querySelector('select#audioOutput').appendChild(option);
|
|
} else if (deviceInfo.kind === 'videoinput') {
|
|
option.text = deviceInfo.label || `camera ${document.querySelector('select#videoSource').length + 1}`;
|
|
document.querySelector('select#videoSource').appendChild(option);
|
|
} else {
|
|
console.log('Some other kind of source/device: ', deviceInfo);
|
|
}
|
|
}
|
|
selectors.forEach((select, selectorIndex) => {
|
|
if (Array.prototype.slice.call(select.childNodes).some(n => n.value === values[selectorIndex])) {
|
|
select.value = values[selectorIndex];
|
|
}
|
|
});
|
|
},
|
|
// Attach audio output device to video element using device/sink ID.
|
|
attachSinkId :function attachSinkId(element, sinkId) {
|
|
if (typeof element.sinkId !== 'undefined') {
|
|
element.setSinkId(sinkId)
|
|
.then(() => {
|
|
console.log(`Success, audio output device attached: ${sinkId}`);
|
|
})
|
|
.catch(error => {
|
|
let errorMessage = error;
|
|
if (error.name === 'SecurityError') {
|
|
errorMessage = `You need to use HTTPS for selecting audio output device: ${error}`;
|
|
}
|
|
console.error(errorMessage);
|
|
// Jump back to first output device in the list as it's the default.
|
|
document.querySelector('select#audioOutput').selectedIndex = 0;
|
|
});
|
|
} else {
|
|
console.warn('Browser does not support output device selection.');
|
|
}
|
|
},
|
|
changeAudioDestination: function changeAudioDestination() {
|
|
const audioDestination = document.querySelector('select#audioOutput').value;
|
|
this.attachSinkId(document.querySelector('#localVideo'), audioDestination);
|
|
},
|
|
gotStream: function gotStream(stream) {
|
|
this.stream = stream; // make stream available to console
|
|
document.querySelector('#localVideo').srcObject = stream;
|
|
// Refresh button list in case labels have become available
|
|
return navigator.mediaDevices.enumerateDevices();
|
|
},
|
|
handleError: function handleError(error) {
|
|
console.log('navigator.MediaDevices.getUserMedia error: ', error.message, error.name);
|
|
},
|
|
start: function start() {
|
|
if (this.stream) {
|
|
this.stream.getTracks().forEach(track => {
|
|
track.stop();
|
|
});
|
|
}
|
|
const audioSource = document.querySelector('select#audioSource').value;
|
|
const videoSource = document.querySelector('select#videoSource').value;
|
|
const constraints = {
|
|
audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
|
|
video: {deviceId: videoSource ? {exact: videoSource} : undefined}
|
|
};
|
|
navigator.mediaDevices.getUserMedia(constraints).then(this.gotStream).then(this.gotDevices).catch(this.handleError);
|
|
},
|
|
copyToClipboard: function(selector) {
|
|
let element = document.querySelector(selector);
|
|
element.select();
|
|
element.setSelectionRange(0, 99999); /*For mobile devices*/
|
|
document.execCommand("copy");
|
|
alert("Copied the text: " + element.value);
|
|
}
|
|
},
|
|
mounted() {
|
|
console.log('VUE is alive!');
|
|
this.listenForPeerEvents();
|
|
navigator.mediaDevices.enumerateDevices().then(this.gotDevices).catch(this.handleError);
|
|
this.start();
|
|
document.querySelector('select#audioOutput').disabled = !('sinkId' in HTMLMediaElement.prototype);
|
|
}
|
|
});
|