tauri/examples/api/src/components/WebRTC.svelte
Amr Bashir f87c04d70b
refactor(examples/api): use vite (#2998)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
2021-12-09 01:16:00 -03:00

54 lines
1.5 KiB
Svelte

<script>
import { onMount,onDestroy } from "svelte";
export let onMessage;
const constraints = window.constraints = {
audio: true,
video: true
};
function handleSuccess(stream) {
const video = document.querySelector('video');
const videoTracks = stream.getVideoTracks();
onMessage('Got stream with constraints:', constraints);
onMessage(`Using video device: ${videoTracks[0].label}`);
window.stream = stream; // make variable available to browser console
video.srcObject = stream;
}
function handleError(error) {
if (error.name === 'ConstraintNotSatisfiedError') {
const v = constraints.video;
onMessage(`The resolution ${v.width.exact}x${v.height.exact} px is not supported by your device.`);
} else if (error.name === 'PermissionDeniedError') {
onMessage('Permissions have not been granted to use your camera and ' +
'microphone, you need to allow the page access to your devices in ' +
'order for the demo to work.');
}
onMessage(`getUserMedia error: ${error.name}`, error);
}
onMount(async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
handleSuccess(stream);
} catch (e) {
handleError(e);
}
})
onDestroy(() => {
window.stream.getTracks().forEach(function(track) {
track.stop();
});
})
</script>
<div>
<div class="alert"><p>Not available for Linux</p></div>
<video id="localVideo" autoplay playsinline>
<track kind="captions" />
</video>
</div>