mirror of
https://api.glitch.com/git/yaswvc
synced 2026-01-12 03:18:10 +00:00
172 lines
6.7 KiB
JavaScript
172 lines
6.7 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'
|
|
}),
|
|
stream: null,
|
|
calls: [],
|
|
},
|
|
methods: {
|
|
logMessage: function(message) {
|
|
let newMessage = document.createElement('div');
|
|
newMessage.innerText = message;
|
|
document.querySelector('.messages').appendChild(newMessage);
|
|
},
|
|
renderVideo:function(stream, selector = '#remoteVideo') {
|
|
document.querySelector(selector).srcObject = stream;
|
|
},
|
|
connectToPeer: function(peerId) {
|
|
this.logMessage(`Connecting to ${peerId}...`);
|
|
|
|
let conn = this.peer.connect(peerId);
|
|
conn.on('data', (data) => {
|
|
this.logMessage(`received: ${data}`);
|
|
});
|
|
conn.on('open', () => {
|
|
conn.send('hi!');
|
|
});
|
|
|
|
let call = this.peer.call(peerId, this.stream);
|
|
call.on('stream', this.renderVideo);
|
|
this.calls.push(call);
|
|
},
|
|
disconnectCall: function(call) {
|
|
// if (!call) return;
|
|
console.log('CALLS', this.calls);
|
|
console.log('CALL', call);
|
|
},
|
|
listenForPeerEvents: function () {
|
|
this.peer.on('open', (id) => {
|
|
this.logMessage('My peer ID is: ' + id);
|
|
localStorage.setItem('yaswvc-peerId', id);
|
|
});
|
|
this.peer.on('error', (error) => {
|
|
console.error(error);
|
|
});
|
|
|
|
// Handle incoming data connection
|
|
this.peer.on('connection', (conn) => {
|
|
this.logMessage('incoming peer connection!');
|
|
console.log('incoming peer connection!', conn);
|
|
conn.on('data', (data) => {
|
|
this.logMessage(`received: ${data}`);
|
|
});
|
|
conn.on('open', () => {
|
|
conn.send('hello!');
|
|
});
|
|
conn.on('close', () => {
|
|
this.logMessage('Connection closed.');
|
|
});
|
|
conn.on('error', (error) => {
|
|
console.error('Error:', error);
|
|
this.logMessage('Error'+error);
|
|
});
|
|
});
|
|
|
|
// 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.
|
|
// this.renderVideo(this.stream, '#localVideo');
|
|
call.on('stream', this.renderVideo);
|
|
});
|
|
},
|
|
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);
|
|
}
|
|
});
|