diff options
| author | Paul Oliver <contact@pauloliver.dev> | 2026-01-03 09:20:23 +0000 |
|---|---|---|
| committer | Paul Oliver <contact@pauloliver.dev> | 2026-01-03 10:33:59 +0000 |
| commit | de01f5b7308b83647d823cfb4b2826af849423b7 (patch) | |
| tree | ac6006adfce97ff98f9fe7b6728ce9c17d46f81a /hsm-web/Client/src/CamStream.vue | |
| parent | 864a1d2a22580a33b5e928734fd256c2133fb672 (diff) | |
Adds scaffold for motor control to frontend
Diffstat (limited to 'hsm-web/Client/src/CamStream.vue')
| -rw-r--r-- | hsm-web/Client/src/CamStream.vue | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/hsm-web/Client/src/CamStream.vue b/hsm-web/Client/src/CamStream.vue new file mode 100644 index 0000000..9c6e202 --- /dev/null +++ b/hsm-web/Client/src/CamStream.vue @@ -0,0 +1,110 @@ +<template> + <h2>Camera Stream</h2> + <div id='playerCont'> + <video id='player' muted></video> + </div> + <button @click='toggleStream()' :disabled='disabled'>{{ command }}</button> +</template> + +<script> +import axios from 'axios' +import config from './config' +import GstWebRTCAPI from '@tomoxv/gstwebrtc-api/src/gstwebrtc-api.js' + +export default { + data() { + return { + api: null, + listener: null, + player: null, + session: null, + + command: 'Loading', + disabled: true, + streaming: false + } + }, + mounted() { + this.player = document.getElementById('player') + this.monitor() + this.bindStream() + }, + methods: { + // Continuously checks if WebRTC stream is running on server + async monitor() { + const res = await axios.get(config.api + '/isStreaming') + + switch (res.status) { + case 200: + this.command = res.data ? 'Stop' : 'Play' + this.disabled = false + this.streaming = res.data + break + default: + this.command = 'Error' + this.disabled = true + this.streaming = false + } + + setTimeout(this.monitor, 1000) + }, + + // Toggles WebRTC stream + async toggleStream() { + const ep = this.streaming ? '/stopStream' : '/startStream' + const res = await axios.get(config.api + ep) + + if (res.status != 200) { + console.error(res) + } + }, + + // Binds WebRTC stream to video element + bindStream() { + this.api = new GstWebRTCAPI({ + meta: { name: 'WebClient-' + Date.now() }, + signalingServerUrl: 'ws://' + window.location.hostname + ':8443' + }) + + this.listener = { + producerAdded: (producer) => { + console.log("Producer added", producer) + + this.session = this.api.createConsumerSession(producer.id) + this.session.addEventListener('streamsChanged', () => { + if (this.session.streams.length > 0) { + this.player.srcObject = this.session.streams[0] + this.player.play() + } + }) + + this.session.connect() + }, + + producerRemoved: (producer) => { + console.log("Producer removed", producer) + + this.player.pause() + this.player.srcObject = null + this.session = null + } + } + + this.api.registerProducersListener(this.listener) + } + } +} +</script> + +<style> +#playerCont { + border: 1px solid black; +} +#player { + aspect-ratio: 32 / 27; + display: block; + margin: auto; + width: 480px; + max-width: 100%; +} +</style> |
