Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const {
createLocalTracks,
createLocalAudioTrack,
createLocalVideoTrack
} = require('twilio-video');
var localTracks;
// Create default local audio and video tracks
createLocalTracks().then(localTracks => {
console.log('Got default audio and video tracks:', localTracks);
});
// Create default local track of a particular kind
createLocalAudioTrack().then(audioTrack => {
console.log('Got default local audio track:', audioTrack);
});
createLocalVideoTrack().then(videoTrack => {
console.log('Got default local video track:', videoTrack);
});
const { connect, createLocalTracks } = require('twilio-video');
// Option 1
createLocalTracks({
audio: true,
video: { width: 640 }
}).then(localTracks => {
return connect('$TOKEN', {
name: 'my-room-name',
tracks: localTracks
});
}).then(room => {
console.log('Connected to Room:', room.name);
});
// Option 2
connect('$TOKEN', {
audio: true,
name: 'my-room-name',
video: { width: 640 }
document.getElementById('button-preview').onclick = function() {
var localTracksPromise = previewTracks
? Promise.resolve(previewTracks)
: Video.createLocalTracks();
localTracksPromise.then(
function(tracks) {
window.previewTracks = previewTracks = tracks;
var previewContainer = document.getElementById('local-media');
if (!previewContainer.querySelector('video')) {
attachTracks(tracks, previewContainer);
}
},
function(error) {
console.error('Unable to access local media', error);
log('Unable to access Camera and Microphone');
}
);
};
document.getElementById('button-preview').onclick = function() {
var localTracksPromise = previewTracks
? Promise.resolve(previewTracks)
: Video.createLocalTracks();
localTracksPromise.then(
function(tracks) {
window.previewTracks = previewTracks = tracks;
var previewContainer = document.getElementById('local-media');
if (!previewContainer.querySelector('video')) {
attachTracks(tracks, previewContainer);
}
},
function(error) {
console.error('Unable to access local media', error);
log('Unable to access Camera and Microphone');
}
);
};
document.getElementById('button-preview').onclick = function() {
var localTracksPromise = previewTracks
? Promise.resolve(previewTracks)
: Video.createLocalTracks();
localTracksPromise.then(function(tracks) {
window.previewTracks = previewTracks = tracks;
var previewContainer = document.getElementById('local-media');
if (!previewContainer.querySelector('video')) {
attachTracks(tracks, previewContainer);
}
}, function(error) {
console.error('Unable to access local media', error);
log('Unable to access Camera and Microphone');
});
};
connectToTwilio(token, roomName) {
this.token = token;
//throw new Error();
Video.createLocalTracks().then((localTrack) => {
this.localTrack = localTrack;
Video
.connect(token, { name: roomName, track: localTrack })
.then(room => {
this.setState((prevState) => { return { ...prevState, currentRoom: room } })
this.iterateLocalParticipantTracks(room.localParticipant);
room.participants.forEach(this.participantConnected);
room.on('participantConnected', this.participantConnected);
room.on('participantDisconnected', this.participantDisconnected);
room.once('disconnected', error => room.participants.forEach(this.participantDisconnected));
}, (err) => {
this.showTwilioError();
});
}, () => {
this.showTwilioError();
});
async function setupLocalAudioAndVideoTracks(video) {
const audioAndVideoTrack = await createLocalTracks();
audioAndVideoTrack.forEach(track => track.attach(video));
return audioAndVideoTrack;
}