('')
-
- const localStreamId = getStorageStream()
- const [_, setMeetingJoined] = useAtom(meetingJoinedAtom)
-
- const { id, stream, setUserName, setSyncUserStatus, restart, userStatus } = useWhipClient(localStreamId)
-
- const join = async () => {
- setLoading(true)
-
- setUserName(displayName || localStreamId)
-
- setMeetingJoined(true)
- setStorageName(displayName)
- await restart()
- setSyncUserStatus((status) => setStream(id, status))
- setLoading(false)
- }
-
- useEffect(() => {
- setDisplayName(getStorageName() || '')
- }, [])
-
- return (
-
-
-
-
-
-
-
- setDisplayName(e.target.value)}
- />
-
-
-
-
-
-
-
-
-
-
-
- )
-}
diff --git a/webapp/components/prepare.vue b/webapp/components/prepare.vue
new file mode 100644
index 0000000..55ef76c
--- /dev/null
+++ b/webapp/components/prepare.vue
@@ -0,0 +1,111 @@
+
+
+
+
+
+
+
+
+
+
+
{{ joinError }}
+
+
+
+
+
+
+
+
diff --git a/webapp/components/setting-item.vue b/webapp/components/setting-item.vue
new file mode 100644
index 0000000..9544d5b
--- /dev/null
+++ b/webapp/components/setting-item.vue
@@ -0,0 +1,43 @@
+
+
+
+
+
diff --git a/webapp/components/settings-advanced.vue b/webapp/components/settings-advanced.vue
new file mode 100644
index 0000000..8892a75
--- /dev/null
+++ b/webapp/components/settings-advanced.vue
@@ -0,0 +1,106 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Room Owner
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/webapp/components/settings-general.vue b/webapp/components/settings-general.vue
new file mode 100644
index 0000000..7467689
--- /dev/null
+++ b/webapp/components/settings-general.vue
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
+
+
diff --git a/webapp/components/settings-media.vue b/webapp/components/settings-media.vue
new file mode 100644
index 0000000..63f7eec
--- /dev/null
+++ b/webapp/components/settings-media.vue
@@ -0,0 +1,50 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/webapp/components/settings.tsx b/webapp/components/settings.tsx
deleted file mode 100644
index bd10d22..0000000
--- a/webapp/components/settings.tsx
+++ /dev/null
@@ -1,252 +0,0 @@
-import { useAtom } from 'jotai'
-import { useState } from 'react'
-import { meetingIdAtom } from './../store/atom'
-
-import {
- SvgClose,
- SvgGeneral,
- SvgMedia,
- SvgAdvanced,
- SvgTip,
-} from './svg/setting'
-import { delStorage } from '../lib/storage'
-import { useSettingStore } from '../store/settingStore'
-
-interface SettingsProps {
- onClose: () => void
-}
-
-interface SettingItemProps {
- children?: React.ReactNode;
- label: string;
- tooltips?: string[];
-}
-
-function SettingItem(props: SettingItemProps) {
- const [tooltipVisible, setTooltipVisible] = useState(false)
- return (
-
-
-
-
- {props.tooltips && (
-
setTooltipVisible(true)}
- onMouseLeave={() => setTooltipVisible(false)}
- >
-
- {tooltipVisible && (
-
- )}
-
- )}
-
- {props.children}
-
- {props.tooltips && tooltipVisible && (
-
-
- {props.tooltips.map((tip, index) => (
-
- {tip}
-
- ))}
-
-
- )}
-
- )
-}
-
-function SettingGeneral() {
- const language = useSettingStore(state => state.language)
- const setLanguage = useSettingStore(state => state.setLanguage)
- const languageOptions = ['English']
- const onChangeLanguage = (language: string) => {
- setLanguage(language)
-
- // todo: add support of more languages
- }
- return (
-
-
-
-
-
- )
-}
-
-function SettingMedia() {
- const videoResolution = useSettingStore(state => state.videoResolution)
- const setVideoResolution = useSettingStore(state => state.setVideoResolution)
- const screenShareResolution = useSettingStore(state => state.screenShareResolution)
- const setScreenShareResolution = useSettingStore(state => state.setScreenShareResolution)
- const videoResolutionOptions = [
- { label: '480p (Default)', value: '480' },
- ]
- const screenShareResolutionOptions = [
- { label: '720p (Default)', value: '720' },
- { label: '1080p', value: '1080' },
- { label: 'Native', value: 'native' }
- ]
- const onChangeVideoResolution = (resolution: string) => {
- setVideoResolution(resolution)
-
- // todo: add support of more video resolutions
- }
- const onChangeScreenShareResolution = async (resolution: string) => {
- setScreenShareResolution(resolution)
- }
- return (
-
-
-
-
-
-
-
-
- )
-}
-
-function SettingAdvanced() {
- const [meetingId] = useAtom(meetingIdAtom)
- const screenShareButtonShowed = useSettingStore(state => state.screenShareButtonShowed)
- const setScreenShareButtonShowed = useSettingStore(state => state.setScreenShareButtonShowed)
- const [isReset, setIsReset] = useState(false)
-
- return (
-
-
-
-
-
-
-
-
- )
-}
-
-export default function Settings(props: SettingsProps) {
- const [activeTab, setActiveTab] = useState('General')
- const settingsOptions = [
- { label: 'General', icon: SvgGeneral },
- { label: 'Media', icon: SvgMedia },
- { label: 'Advanced', icon: SvgAdvanced }
- ]
- return (
-
-
-
-
Settings
-
-
-
-
-
- {activeTab}
- {activeTab === 'General' && }
- {activeTab === 'Media' && }
- {activeTab === 'Advanced' && }
-
-
-
-
- )
-}
diff --git a/webapp/components/settings.vue b/webapp/components/settings.vue
new file mode 100644
index 0000000..60d80fd
--- /dev/null
+++ b/webapp/components/settings.vue
@@ -0,0 +1,56 @@
+
+
+
+
+
+
+
Settings
+
+
+
+
+
+ {{ activeTab }}
+
+
+
+
+
+
+
+
diff --git a/webapp/components/svg/advanced.vue b/webapp/components/svg/advanced.vue
new file mode 100644
index 0000000..4e9f5de
--- /dev/null
+++ b/webapp/components/svg/advanced.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/audio.tsx b/webapp/components/svg/audio.tsx
deleted file mode 100644
index fd7ca9d..0000000
--- a/webapp/components/svg/audio.tsx
+++ /dev/null
@@ -1,8 +0,0 @@
-export default function SvgAudio() {
- return (
-
- )
-}
diff --git a/webapp/components/svg/audio.vue b/webapp/components/svg/audio.vue
new file mode 100644
index 0000000..cbd4ac7
--- /dev/null
+++ b/webapp/components/svg/audio.vue
@@ -0,0 +1,6 @@
+
+
+
diff --git a/webapp/components/svg/background-cancel.vue b/webapp/components/svg/background-cancel.vue
new file mode 100644
index 0000000..1b40f06
--- /dev/null
+++ b/webapp/components/svg/background-cancel.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/background.tsx b/webapp/components/svg/background.tsx
deleted file mode 100644
index ba05175..0000000
--- a/webapp/components/svg/background.tsx
+++ /dev/null
@@ -1,15 +0,0 @@
-export function SvgBackground() {
- return (
-
- )
-}
-export function SvgBackgroundCancel() {
- return (
-
- )
-}
\ No newline at end of file
diff --git a/webapp/components/svg/background.vue b/webapp/components/svg/background.vue
new file mode 100644
index 0000000..1daf7b4
--- /dev/null
+++ b/webapp/components/svg/background.vue
@@ -0,0 +1,6 @@
+
+
+
diff --git a/webapp/components/svg/chat.vue b/webapp/components/svg/chat.vue
new file mode 100644
index 0000000..0cfc0db
--- /dev/null
+++ b/webapp/components/svg/chat.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/close.vue b/webapp/components/svg/close.vue
new file mode 100644
index 0000000..2cf2834
--- /dev/null
+++ b/webapp/components/svg/close.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/done.tsx b/webapp/components/svg/done.tsx
deleted file mode 100644
index 8e30aee..0000000
--- a/webapp/components/svg/done.tsx
+++ /dev/null
@@ -1,7 +0,0 @@
-export default function SvgDone() {
- return (
-
- )
-}
diff --git a/webapp/components/svg/done.vue b/webapp/components/svg/done.vue
new file mode 100644
index 0000000..f605d99
--- /dev/null
+++ b/webapp/components/svg/done.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/end.tsx b/webapp/components/svg/end.tsx
deleted file mode 100644
index 5984dd5..0000000
--- a/webapp/components/svg/end.tsx
+++ /dev/null
@@ -1,7 +0,0 @@
-export default function SvgEnd() {
- return (
-
- )
-}
diff --git a/webapp/components/svg/end.vue b/webapp/components/svg/end.vue
new file mode 100644
index 0000000..0b192c5
--- /dev/null
+++ b/webapp/components/svg/end.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/exit-fullscreen.vue b/webapp/components/svg/exit-fullscreen.vue
new file mode 100644
index 0000000..d34e4c8
--- /dev/null
+++ b/webapp/components/svg/exit-fullscreen.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/exit-pip.vue b/webapp/components/svg/exit-pip.vue
new file mode 100644
index 0000000..38b3c7b
--- /dev/null
+++ b/webapp/components/svg/exit-pip.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/fullscreen.vue b/webapp/components/svg/fullscreen.vue
new file mode 100644
index 0000000..29b72be
--- /dev/null
+++ b/webapp/components/svg/fullscreen.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/general.vue b/webapp/components/svg/general.vue
new file mode 100644
index 0000000..ec48486
--- /dev/null
+++ b/webapp/components/svg/general.vue
@@ -0,0 +1,6 @@
+
+
+
diff --git a/webapp/components/svg/loading.tsx b/webapp/components/svg/loading.tsx
deleted file mode 100644
index f72395d..0000000
--- a/webapp/components/svg/loading.tsx
+++ /dev/null
@@ -1,8 +0,0 @@
-export default function Loading() {
- return (
-
- )
-}
diff --git a/webapp/components/svg/loading.vue b/webapp/components/svg/loading.vue
new file mode 100644
index 0000000..0abbad4
--- /dev/null
+++ b/webapp/components/svg/loading.vue
@@ -0,0 +1,6 @@
+
+
+
diff --git a/webapp/components/svg/media.vue b/webapp/components/svg/media.vue
new file mode 100644
index 0000000..18621d3
--- /dev/null
+++ b/webapp/components/svg/media.vue
@@ -0,0 +1,6 @@
+
+
+
diff --git a/webapp/components/svg/members.vue b/webapp/components/svg/members.vue
new file mode 100644
index 0000000..676549a
--- /dev/null
+++ b/webapp/components/svg/members.vue
@@ -0,0 +1,8 @@
+
+
+
diff --git a/webapp/components/svg/mic.vue b/webapp/components/svg/mic.vue
new file mode 100644
index 0000000..0d24077
--- /dev/null
+++ b/webapp/components/svg/mic.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/muted.vue b/webapp/components/svg/muted.vue
new file mode 100644
index 0000000..e77109f
--- /dev/null
+++ b/webapp/components/svg/muted.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/pip.vue b/webapp/components/svg/pip.vue
new file mode 100644
index 0000000..d6ca3a7
--- /dev/null
+++ b/webapp/components/svg/pip.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/player.tsx b/webapp/components/svg/player.tsx
deleted file mode 100644
index 18598a4..0000000
--- a/webapp/components/svg/player.tsx
+++ /dev/null
@@ -1,55 +0,0 @@
-export function SvgMuted() {
- return (
-
- )
-}
-
-export function SvgUnmuted() {
- return(
-
- )
-}
-
-export function SvgFullscreen() {
- return(
-
- )
-}
-
-export function SvgExitFullscreen() {
- return(
-
- )
-}
-
-export function SvgPictureInPicture() {
- return(
-
- )
-}
-
-export function SvgExitPictureInPicture() {
- return(
-
- )
-}
-
-export function SvgMic() {
- return(
-
- )
-}
\ No newline at end of file
diff --git a/webapp/components/svg/present-cancel.vue b/webapp/components/svg/present-cancel.vue
new file mode 100644
index 0000000..48f3812
--- /dev/null
+++ b/webapp/components/svg/present-cancel.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/present.tsx b/webapp/components/svg/present.tsx
deleted file mode 100644
index 6723a75..0000000
--- a/webapp/components/svg/present.tsx
+++ /dev/null
@@ -1,20 +0,0 @@
-function SvgPresentToAll() {
- return (
-
- )
-}
-
-function SvgPresentCancel() {
- return (
-
- )
-}
-
-export {
- SvgPresentToAll,
- SvgPresentCancel,
-}
diff --git a/webapp/components/svg/present.vue b/webapp/components/svg/present.vue
new file mode 100644
index 0000000..6cbd129
--- /dev/null
+++ b/webapp/components/svg/present.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/progress.tsx b/webapp/components/svg/progress.tsx
deleted file mode 100644
index 019ddbe..0000000
--- a/webapp/components/svg/progress.tsx
+++ /dev/null
@@ -1,7 +0,0 @@
-export default function SvgProgress() {
- return (
-
- )
-}
diff --git a/webapp/components/svg/progress.vue b/webapp/components/svg/progress.vue
new file mode 100644
index 0000000..4c7cea1
--- /dev/null
+++ b/webapp/components/svg/progress.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/setting.tsx b/webapp/components/svg/setting.tsx
deleted file mode 100644
index 393f0c4..0000000
--- a/webapp/components/svg/setting.tsx
+++ /dev/null
@@ -1,49 +0,0 @@
-export function SvgSetting() {
- return (
-
- )
-}
-
-export function SvgClose() {
- return (
-
- )
-}
-
-export function SvgGeneral() {
- return (
-
- )
-}
-
-export function SvgMedia() {
- return (
-
- )
-}
-
-export function SvgAdvanced() {
- return (
-
- )
-
-}
-export function SvgTip() {
- return (
-
- )
-}
\ No newline at end of file
diff --git a/webapp/components/svg/setting.vue b/webapp/components/svg/setting.vue
new file mode 100644
index 0000000..d400dcd
--- /dev/null
+++ b/webapp/components/svg/setting.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/speaker.tsx b/webapp/components/svg/speaker.tsx
deleted file mode 100644
index b969f4f..0000000
--- a/webapp/components/svg/speaker.tsx
+++ /dev/null
@@ -1,7 +0,0 @@
-export default function SvgSpeaker() {
- return (
-
- )
-}
\ No newline at end of file
diff --git a/webapp/components/svg/speaker.vue b/webapp/components/svg/speaker.vue
new file mode 100644
index 0000000..768a109
--- /dev/null
+++ b/webapp/components/svg/speaker.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/switch-camera.vue b/webapp/components/svg/switch-camera.vue
new file mode 100644
index 0000000..d523d3b
--- /dev/null
+++ b/webapp/components/svg/switch-camera.vue
@@ -0,0 +1,9 @@
+
+
+
diff --git a/webapp/components/svg/tip.vue b/webapp/components/svg/tip.vue
new file mode 100644
index 0000000..cd727ae
--- /dev/null
+++ b/webapp/components/svg/tip.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/unmuted.vue b/webapp/components/svg/unmuted.vue
new file mode 100644
index 0000000..634fced
--- /dev/null
+++ b/webapp/components/svg/unmuted.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/svg/video.tsx b/webapp/components/svg/video.tsx
deleted file mode 100644
index 0010850..0000000
--- a/webapp/components/svg/video.tsx
+++ /dev/null
@@ -1,7 +0,0 @@
-export default function SvgVideo() {
- return (
-
- )
-}
diff --git a/webapp/components/svg/video.vue b/webapp/components/svg/video.vue
new file mode 100644
index 0000000..19e2af7
--- /dev/null
+++ b/webapp/components/svg/video.vue
@@ -0,0 +1,5 @@
+
+
+
diff --git a/webapp/components/use/imageSegmentation.ts b/webapp/components/use/imageSegmentation.ts
index 39864b4..7d47da4 100644
--- a/webapp/components/use/imageSegmentation.ts
+++ b/webapp/components/use/imageSegmentation.ts
@@ -1,4 +1,7 @@
-import { ImageSegmenter, type ImageSegmenterResult } from '@mediapipe/tasks-vision'
+import {
+ ImageSegmenter,
+ type ImageSegmenterResult,
+} from '@mediapipe/tasks-vision'
// import mediapipe wasm files as url https://vite.dev/guide/assets#explicit-url-imports
import wasmLoaderPath from '@mediapipe/tasks-vision/wasm/vision_wasm_internal.js?url'
@@ -9,7 +12,7 @@ import modelAssetPath from '@/assets/models/selfie_multiclass_256x256.tflite?url
const wasmFileset = {
wasmLoaderPath,
- wasmBinaryPath
+ wasmBinaryPath,
}
export class VirtualBackgroundStream {
@@ -45,12 +48,17 @@ export class VirtualBackgroundStream {
this.canvas = document.createElement('canvas')
this.canvas.width = this.videoWidth
this.canvas.height = this.videoHeight
- this.canvasCtx = this.canvas.getContext('2d')!
+ const canvasCtx = this.canvas.getContext('2d')
+ if (!canvasCtx) throw new Error('Canvas 2D context is not available')
+ this.canvasCtx = canvasCtx
this.tempCanvas = document.createElement('canvas')
this.tempCanvas.width = this.videoWidth
this.tempCanvas.height = this.videoHeight
- this.tempCtx = this.tempCanvas.getContext('2d')!
+ const tempCtx = this.tempCanvas.getContext('2d')
+ if (!tempCtx)
+ throw new Error('Temporary canvas 2D context is not available')
+ this.tempCtx = tempCtx
}
private async createImageSegmenter() {
@@ -58,10 +66,10 @@ export class VirtualBackgroundStream {
this.segmenter = await ImageSegmenter.createFromOptions(wasmFileset, {
baseOptions: {
modelAssetPath,
- delegate: 'GPU'
+ delegate: 'GPU',
},
outputCategoryMask: true,
- runningMode: 'VIDEO'
+ runningMode: 'VIDEO',
})
} catch (error) {
console.error('failed to create a segmenter:', error)
@@ -69,8 +77,13 @@ export class VirtualBackgroundStream {
}
private callbackForVideo = (segmentationResult: ImageSegmenterResult) => {
- if (!segmentationResult || !segmentationResult.categoryMask) return
- const imageData = this.tempCtx.getImageData(0, 0, this.video.videoWidth, this.video.videoHeight).data
+ if (!segmentationResult?.categoryMask) return
+ const imageData = this.tempCtx.getImageData(
+ 0,
+ 0,
+ this.video.videoWidth,
+ this.video.videoHeight
+ ).data
// get results of segmentation
// 0 - background
// 1 - hair
@@ -78,7 +91,9 @@ export class VirtualBackgroundStream {
// 3 - face-skin
// 4 - clothes
// 5 - others (accessories)
- const maskData = segmentationResult.categoryMask.getAsFloat32Array().map(val => Math.round(val * 255.0))
+ const maskData = segmentationResult.categoryMask
+ .getAsFloat32Array()
+ .map(val => Math.round(val * 255.0))
const categoryChosen = 0
// draw the top layer on tempCanvas
@@ -88,7 +103,7 @@ export class VirtualBackgroundStream {
}
const w = this.videoWidth
const h = this.videoHeight
- const getPixelIndex = (x: number, y: number) => (y * w + x)
+ const getPixelIndex = (x: number, y: number) => y * w + x
for (let i = 0; i < h; i++) {
for (let j = 0; j < w; j++) {
let sum = 0
@@ -113,13 +128,18 @@ export class VirtualBackgroundStream {
this.tempCtx.putImageData(dataNew, 0, 0)
}
-
- // draw the bottom layer on canvas
- {
- this.canvasCtx.clearRect(0, 0, this.videoWidth, this.videoHeight)
- if (this.backgroundImage.complete && this.backgroundImage.naturalHeight !== 0) {
- this.canvasCtx.drawImage(this.backgroundImage, 0, 0, this.video.videoWidth, this.video.videoHeight)
- }
+ this.canvasCtx.clearRect(0, 0, this.videoWidth, this.videoHeight)
+ if (
+ this.backgroundImage.complete &&
+ this.backgroundImage.naturalHeight !== 0
+ ) {
+ this.canvasCtx.drawImage(
+ this.backgroundImage,
+ 0,
+ 0,
+ this.video.videoWidth,
+ this.video.videoHeight
+ )
}
// put two layers together
@@ -132,8 +152,18 @@ export class VirtualBackgroundStream {
if (!this.segmenter || !this.webcamRunning) return
try {
// draw video frame on tempCanvas
- this.tempCtx.drawImage(this.video, 0, 0, this.video.videoWidth, this.video.videoHeight)
- this.segmenter.segmentForVideo(this.video, performance.now(), this.callbackForVideo)
+ this.tempCtx.drawImage(
+ this.video,
+ 0,
+ 0,
+ this.video.videoWidth,
+ this.video.videoHeight
+ )
+ this.segmenter.segmentForVideo(
+ this.video,
+ performance.now(),
+ this.callbackForVideo
+ )
} catch (error) {
console.error('error when processing frame:', error)
}
@@ -147,8 +177,8 @@ export class VirtualBackgroundStream {
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
- deviceId: this.deviceId
- }
+ deviceId: this.deviceId,
+ },
})
return new Promise(resolve => {
this.video.srcObject = stream
@@ -170,7 +200,9 @@ export class VirtualBackgroundStream {
if (stream === null) return
this.webcamRunning = false
const tracks = stream.getTracks()
- tracks.forEach(track => track.stop())
+ tracks.forEach(track => {
+ track.stop()
+ })
this.video.srcObject = null
this.canvasCtx.clearRect(0, 0, this.videoWidth, this.videoHeight)
}
diff --git a/webapp/components/use/volumeExtraction.ts b/webapp/components/use/volumeExtraction.ts
index 8314849..9b0258b 100644
--- a/webapp/components/use/volumeExtraction.ts
+++ b/webapp/components/use/volumeExtraction.ts
@@ -2,7 +2,7 @@ export default class VolumeExtractor {
private audioContext: AudioContext
private source: MediaStreamAudioSourceNode
private analyser: AnalyserNode
- private dataArray: Uint8Array
+ private dataArray: Uint8Array
private bufferLength: number
constructor(stream: MediaStream) {
@@ -28,4 +28,10 @@ export default class VolumeExtractor {
const volumeValue = Math.round(perceptual * 100)
return volumeValue
}
-}
\ No newline at end of file
+
+ public close = () => {
+ this.source.disconnect()
+ this.analyser.disconnect()
+ void this.audioContext.close().catch(() => {})
+ }
+}
diff --git a/webapp/components/use/wakeLock.ts b/webapp/components/use/wakeLock.ts
index 3db0aba..4272634 100644
--- a/webapp/components/use/wakeLock.ts
+++ b/webapp/components/use/wakeLock.ts
@@ -32,7 +32,10 @@ export default class WakeLock {
console.error('Failed to release wake lock:', err)
} finally {
this.lock = null
- document.removeEventListener('visibilitychange', this.handleVisibilityChange)
+ document.removeEventListener(
+ 'visibilitychange',
+ this.handleVisibilityChange
+ )
}
}
}
diff --git a/webapp/components/use/whep.ts b/webapp/components/use/whep.ts
index dcbbd16..20bd162 100644
--- a/webapp/components/use/whep.ts
+++ b/webapp/components/use/whep.ts
@@ -1,39 +1,47 @@
-import { useSyncExternalStore } from 'react'
-import { event, Context, Data } from './whxp'
-import { StreamState, Stream } from '../../lib/api'
+import { onScopeDispose, type ShallowRef, shallowRef } from 'vue'
import { WHEPClient } from 'whip-whep/whep'
+import { type Stream, StreamState, token } from '../../lib/api'
+import { log } from '../../lib/log'
+import { Context, type Data, event } from './whxp'
-interface WHIPData extends Data {
+interface WHEPData extends Data {
connStatus: string
- setRemoteStatus: (userStatus: Stream) => void,
+ setRemoteStatus: (userStatus: Stream) => void
+}
+
+interface ViewSession {
+ generation: number
+ client: WHEPClient
+ pc: RTCPeerConnection
+ onStateChange: () => void
+ view?: Promise
+ cleanup?: Promise
}
class WHEPContext extends Context {
- client: WHEPClient = new WHEPClient()
- cache: WHIPData
+ cache: WHEPData
remoteStatus: Stream
- connStatus: string = 'new'
+ connStatus: string = StreamState.New
+
+ private session: ViewSession | undefined
+ private generation = 0
+ private wantsRunning = false
+ private startOperation:
+ | { generation: number; promise: Promise }
+ | undefined
+ private mutedTicks = 0
+
constructor(id: string) {
super(id)
this.cache = this.clone()
- this.remoteStatus = Object.assign({}, this.userStatus)
+ this.remoteStatus = { ...this.userStatus }
}
- private newPeerConnection() {
- const { pc, setStream } = this
- pc.addTransceiver('video', { 'direction': 'recvonly' })
- pc.addTransceiver('audio', { 'direction': 'recvonly' })
- pc.ontrack = ev => setStream(new MediaStream([...this.stream.getTracks(), ev.track]))
+ setRemoteStatus = (userStatus: Stream) => {
+ this.remoteStatus = userStatus
}
- setStream = (stream: MediaStream) => {
- this.stream = stream
- this.sync()
- }
-
- setRemoteStatus = (userStatus: Stream) => this.remoteStatus = userStatus
-
- clone(): WHIPData {
+ clone(): WHEPData {
return {
id: this.id,
stream: this.stream,
@@ -53,72 +61,243 @@ class WHEPContext extends Context {
this.dispatchEvent(event)
}
- onconnectionstatechange = () => {
- this.userStatus.state = this.pc.connectionState as StreamState
- this.connStatus = this.pc.connectionState
- this.sync()
+ private isRunningGeneration(generation: number) {
+ return this.wantsRunning && generation === this.generation
}
- async start() {
- const { id, pc, client, userStatus } = this
- pc.addEventListener('connectionstatechange', this.onconnectionstatechange)
- userStatus.state = StreamState.Signaled
+ private clearStream() {
+ this.stream.getTracks().forEach(track => {
+ track.stop()
+ })
+ this.stream = new MediaStream()
this.sync()
- this.newPeerConnection()
-
- try {
- const url = location.origin + `/whep/${id}`
- await client.view(pc, url)
- } catch (e) {
- console.log(e)
- userStatus.state = StreamState.Failed
+ }
+
+ private createSession(generation: number): ViewSession {
+ const pc = new RTCPeerConnection()
+ const session: ViewSession = {
+ generation,
+ client: new WHEPClient(),
+ pc,
+ onStateChange: () => {},
+ }
+ session.onStateChange = () => {
+ if (
+ this.session !== session ||
+ !this.isRunningGeneration(session.generation)
+ )
+ return
+ this.userStatus.state = pc.connectionState as StreamState
+ this.connStatus = pc.connectionState
+ this.mutedTicks = 0
+ if (
+ this.userStatus.state === StreamState.Failed ||
+ this.userStatus.state === StreamState.Disconnected
+ ) {
+ log('warn', 'whep_state', pc.connectionState, { id: this.id })
+ }
this.sync()
}
+ pc.addEventListener('connectionstatechange', session.onStateChange)
+ pc.addTransceiver('video', { direction: 'recvonly' })
+ pc.addTransceiver('audio', { direction: 'recvonly' })
+ pc.ontrack = ev => {
+ if (
+ this.session !== session ||
+ !this.isRunningGeneration(session.generation)
+ )
+ return
+ if (!this.stream.getTracks().includes(ev.track)) {
+ this.stream = new MediaStream([...this.stream.getTracks(), ev.track])
+ this.sync()
+ }
+ }
+ return session
+ }
+
+ private cleanupSession(session: ViewSession): Promise {
+ if (session.cleanup) return session.cleanup
+ session.cleanup = (async () => {
+ const { client, pc } = session
+ pc.removeEventListener('connectionstatechange', session.onStateChange)
+ pc.onconnectionstatechange = null
+ pc.onicecandidate = null
+ pc.ontrack = null
+ pc.close()
+ try {
+ if (session.view) await session.view.catch(() => {})
+ await client.stop()
+ } catch {
+ // Signaling can fail before an upstream resource exists.
+ } finally {
+ pc.close()
+ }
+ })()
+ return session.cleanup
+ }
+
+ private armWatchdog() {
+ if (this.timer === undefined) {
+ this.timer = window.setInterval(() => this.run(), 5000)
+ }
+ }
- if (!this.timer) this.timer = setInterval(() => this.run(), 5000)
+ private setFailedState() {
+ this.userStatus.state = StreamState.Failed
+ this.connStatus = StreamState.Failed
+ this.sync()
+ }
+
+ start(force = false): Promise {
+ if (!force && this.wantsRunning && (this.session || this.startOperation)) {
+ return this.startOperation?.promise || Promise.resolve()
+ }
+ this.wantsRunning = true
+ const generation = ++this.generation
+ const previous = this.session
+ this.session = undefined
+ this.mutedTicks = 0
+ this.clearStream()
+ const cleanup = previous ? this.cleanupSession(previous) : Promise.resolve()
+
+ const operation = (async () => {
+ try {
+ await cleanup
+ if (!this.isRunningGeneration(generation)) return
+ log('info', 'whep_start', '', { id: this.id })
+ const session = this.createSession(generation)
+ this.session = session
+ this.userStatus.state = StreamState.Signaled
+ this.connStatus = StreamState.Signaled
+ this.sync()
+ try {
+ session.view = session.client.view(
+ session.pc,
+ `${location.origin}/whep/${this.id}`,
+ token
+ )
+ await session.view
+ } catch (e) {
+ if (
+ this.session === session &&
+ this.isRunningGeneration(generation)
+ ) {
+ log('error', 'whep_view_failed', String(e), { id: this.id })
+ this.setFailedState()
+ }
+ }
+ if (this.session !== session || !this.isRunningGeneration(generation)) {
+ await this.cleanupSession(session)
+ return
+ }
+ this.armWatchdog()
+ } catch (e) {
+ if (this.isRunningGeneration(generation)) {
+ log('error', 'whep_start_failed', String(e), { id: this.id })
+ this.setFailedState()
+ this.armWatchdog()
+ }
+ } finally {
+ if (this.startOperation?.generation === generation)
+ this.startOperation = undefined
+ }
+ })()
+ this.startOperation = { generation, promise: operation }
+ return operation
}
async stop() {
- if (this.timer) {
+ this.wantsRunning = false
+ this.generation++
+ this.mutedTicks = 0
+ if (this.timer !== undefined) {
clearInterval(this.timer)
this.timer = undefined
}
- try {
- await this.client.stop()
- this.pc.removeEventListener('connectionstatechange', this.onconnectionstatechange)
- } catch (e) {
- console.log(e)
- }
+ const session = this.session
+ this.session = undefined
+ this.startOperation = undefined
+ this.clearStream()
+ this.userStatus.state = StreamState.Closed
+ this.connStatus = StreamState.Closed
+ this.sync()
+ if (session) await this.cleanupSession(session)
}
- async restart() {
- await this.stop()
- this.stream = new MediaStream()
- this.pc = new RTCPeerConnection()
- await this.start()
+ restart(): Promise {
+ return this.start(true)
}
run() {
- // WatchDog, Auto Restart
- const restartStates = [StreamState.Disconnected, StreamState.Closed, StreamState.Failed]
- if (restartStates.find(i => i === this.userStatus.state) && this.remoteStatus.state === StreamState.Connected) this.restart()
+ if (!this.wantsRunning || this.startOperation) return
+ const restartStates = [
+ StreamState.Disconnected,
+ StreamState.Closed,
+ StreamState.Failed,
+ ]
+ if (
+ restartStates.includes(this.userStatus.state) &&
+ this.remoteStatus.state === StreamState.Connected
+ ) {
+ log('warn', 'whep_watchdog_restart', this.userStatus.state, {
+ id: this.id,
+ })
+ void this.restart()
+ return
+ }
+
+ if (this.userStatus.state !== StreamState.Connected) {
+ this.mutedTicks = 0
+ return
+ }
+ const expectsVideo =
+ this.remoteStatus.state === StreamState.Connected &&
+ this.remoteStatus.video
+ const videoMuted = this.stream.getVideoTracks().some(track => track.muted)
+ const noVideoTrack = this.stream.getVideoTracks().length === 0
+ if (expectsVideo && (videoMuted || noVideoTrack)) {
+ this.mutedTicks++
+ if (this.mutedTicks >= 2) {
+ log('warn', 'whep_watchdog_muted_restart', this.userStatus.state, {
+ id: this.id,
+ })
+ this.mutedTicks = 0
+ void this.restart()
+ }
+ } else {
+ this.mutedTicks = 0
+ }
}
}
-const contexts: WHEPContext[] = []
+interface ContextEntry {
+ context: WHEPContext
+ references: number
+}
-export default function useWhepClient(id: string) {
- const newContext = (id: string) => {
- const context = new WHEPContext(id)
- contexts.push(context)
- return context
- }
+const contexts: ContextEntry[] = []
- const context = contexts.find(ctx => ctx.id === id) || newContext(id)
- return useSyncExternalStore((callback: () => void) => {
- context.addEventListener(event.type, callback)
- return () => {
- context.removeEventListener(event.type, callback)
+export default function useWhepClient(id: string): ShallowRef {
+ let entry = contexts.find(item => item.context.id === id)
+ if (!entry) {
+ entry = { context: new WHEPContext(id), references: 0 }
+ contexts.push(entry)
+ }
+ entry.references++
+ const context = entry.context
+ const data = shallowRef(context.export())
+ const update = () => {
+ data.value = context.export()
+ }
+ context.addEventListener(event.type, update)
+ onScopeDispose(() => {
+ context.removeEventListener(event.type, update)
+ entry.references--
+ if (entry.references === 0) {
+ void context.stop()
+ const index = contexts.indexOf(entry)
+ if (index >= 0) contexts.splice(index, 1)
}
- }, () => context.export())
+ })
+ return data
}
diff --git a/webapp/components/use/whip.ts b/webapp/components/use/whip.ts
index c62817e..3915814 100644
--- a/webapp/components/use/whip.ts
+++ b/webapp/components/use/whip.ts
@@ -1,43 +1,103 @@
-import { useSyncExternalStore } from 'react'
-import { event, Context, Data } from './whxp'
-import { Stream, StreamState, token } from '../../lib/api'
+import { onScopeDispose, type ShallowRef, shallowRef } from 'vue'
import { WHIPClient } from 'whip-whep/whip'
+import { type Stream, StreamState, token } from '../../lib/api'
import {
+ asyncGetAudioStream,
+ asyncGetVideoStream,
deviceNone,
deviceScreen,
deviceSegmenter,
- asyncGetAudioStream,
- asyncGetVideoStream,
} from '../../lib/device'
+import { log } from '../../lib/log'
import { VirtualBackgroundStream } from './imageSegmentation'
+import { Context, type Data, event } from './whxp'
interface WHIPData extends Data {
- setUserName: (name: string) => void,
- setSyncUserStatus: (callback: (userStatus: Stream) => void) => void,
-
- currentDeviceAudio: string,
- currentDeviceVideo: string,
- setCurrentDeviceAudio: (current: string) => Promise,
- setCurrentDeviceVideo: (current: string, constraints?: MediaTrackConstraints) => Promise,
- toggleEnableAudio: () => Promise,
- toggleEnableVideo: () => Promise,
+ setUserName: (name: string) => void
+ setSyncUserStatus: (callback: (userStatus: Stream) => void) => void
+
+ currentDeviceAudio: string
+ currentDeviceVideo: string
+ virtualBackgroundEnabled: boolean
+ setCurrentDeviceAudio: (current: string) => Promise
+ setCurrentDeviceVideo: (
+ current: string,
+ constraints?: MediaTrackConstraints
+ ) => Promise
+ toggleEnableAudio: () => Promise
+ toggleEnableVideo: () => Promise
+ stopScreenSharing: () => Promise
+ revokeScreenSharing: () => Promise
toggleEnableVirtualBackground: () => Promise
}
+interface PublishSession {
+ generation: number
+ client: WHIPClient
+ pc: RTCPeerConnection
+ onStateChange: () => void
+ publish?: Promise
+ cleanup?: Promise
+}
+
+const stopTracks = (stream: MediaStream, kind?: 'audio' | 'video') => {
+ const tracks =
+ kind === 'audio'
+ ? stream.getAudioTracks()
+ : kind === 'video'
+ ? stream.getVideoTracks()
+ : stream.getTracks()
+ tracks.forEach(track => {
+ track.stop()
+ })
+}
+
class WHIPContext extends Context {
- client: WHIPClient = new WHIPClient()
cache: WHIPData
currentDeviceAudio = deviceNone.deviceId
currentDeviceVideo = deviceNone.deviceId
currentVideoConstraints: MediaTrackConstraints | undefined = undefined
- toggleEnableAudio = async () => this.setCurrentDeviceAudio(this.userStatus.audio ? deviceNone.deviceId : this.currentDeviceAudio)
- toggleEnableVideo = async () => this.setCurrentDeviceVideo(this.userStatus.video ? deviceNone.deviceId : this.currentDeviceVideo)
- toggleEnableVirtualBackground = async () => this.setCurrentDeviceVideo(this.virtualBackgroundEnabled ? this.currentDeviceVideo : deviceSegmenter.deviceId)
virtualBackgroundEnabled = false
segmentation: VirtualBackgroundStream | null = null
+ private session: PublishSession | undefined
+ private generation = 0
+ private wantsRunning = false
+ private startOperation:
+ | { generation: number; promise: Promise }
+ | undefined
+ private cancelTrackWait: (() => void) | undefined
+ private audioGeneration = 0
+ private videoGeneration = 0
+ private pendingScreenGeneration: number | undefined
+
+ toggleEnableAudio = async () =>
+ this.setCurrentDeviceAudio(
+ this.userStatus.audio ? deviceNone.deviceId : this.currentDeviceAudio
+ )
+ toggleEnableVideo = async () =>
+ this.setCurrentDeviceVideo(
+ this.userStatus.video ? deviceNone.deviceId : this.currentDeviceVideo
+ )
+ toggleEnableVirtualBackground = async () =>
+ this.setCurrentDeviceVideo(
+ this.virtualBackgroundEnabled
+ ? this.currentDeviceVideo
+ : deviceSegmenter.deviceId
+ )
+ stopScreenSharing = async () =>
+ this.updateVideo(deviceNone.deviceId, undefined, true)
+ revokeScreenSharing = async () => {
+ const pending = this.pendingScreenGeneration
+ if (pending !== undefined) {
+ this.pendingScreenGeneration = undefined
+ if (pending === this.videoGeneration) this.videoGeneration++
+ }
+ if (this.userStatus.screen) await this.stopScreenSharing()
+ }
+
constructor(id: string) {
super(id)
this.cache = this.clone()
@@ -49,18 +109,13 @@ class WHIPContext extends Context {
this.syncUserStatus = callback
}
- setStream = (stream: MediaStream) => {
- this.stream = stream
- this.sync()
- }
-
setUserName = (name: string) => {
this.userStatus.name = name
this.sync()
this.syncUserStatus(this.userStatus)
}
- clone() {
+ clone(): WHIPData {
return {
id: this.id,
stream: this.stream,
@@ -70,14 +125,22 @@ class WHIPContext extends Context {
restart: () => this.restart(),
setUserName: (name: string) => this.setUserName(name),
- setSyncUserStatus: (callback: (userStatus: Stream) => void) => this.setSyncUserStatus(callback),
+ setSyncUserStatus: (callback: (userStatus: Stream) => void) =>
+ this.setSyncUserStatus(callback),
currentDeviceAudio: this.currentDeviceAudio,
currentDeviceVideo: this.currentDeviceVideo,
- setCurrentDeviceAudio: (current: string) => this.setCurrentDeviceAudio(current),
- setCurrentDeviceVideo: (current: string, constraints?: MediaTrackConstraints) => this.setCurrentDeviceVideo(current, constraints),
+ virtualBackgroundEnabled: this.virtualBackgroundEnabled,
+ setCurrentDeviceAudio: (current: string) =>
+ this.setCurrentDeviceAudio(current),
+ setCurrentDeviceVideo: (
+ current: string,
+ constraints?: MediaTrackConstraints
+ ) => this.setCurrentDeviceVideo(current, constraints),
toggleEnableAudio: () => this.toggleEnableAudio(),
toggleEnableVideo: () => this.toggleEnableVideo(),
+ stopScreenSharing: () => this.stopScreenSharing(),
+ revokeScreenSharing: () => this.revokeScreenSharing(),
toggleEnableVirtualBackground: () => this.toggleEnableVirtualBackground(),
}
@@ -90,165 +153,368 @@ class WHIPContext extends Context {
this.dispatchEvent(event)
}
- private newPeerConnection() {
- const { pc, stream } = this
+ private isRunningGeneration(generation: number) {
+ return this.wantsRunning && generation === this.generation
+ }
- // NOTE: array audio index is: 0
- if (!stream.getAudioTracks().length) {
- pc.addTransceiver('audio', { 'direction': 'sendonly' })
+ private addPublishTracks(pc: RTCPeerConnection) {
+ const audioTracks = this.stream.getAudioTracks()
+ const videoTracks = this.stream.getVideoTracks()
+ if (audioTracks.length) {
+ audioTracks.forEach(track => {
+ pc.addTrack(track)
+ })
} else {
- stream.getAudioTracks().map(track => pc.addTrack(track))
+ pc.addTransceiver('audio', { direction: 'sendonly' })
}
-
- // NOTE: array video index is: 1
- if (!stream.getVideoTracks().length) {
- pc.addTransceiver('video', { 'direction': 'sendonly' })
+ if (videoTracks.length) {
+ videoTracks.forEach(track => {
+ pc.addTrack(track)
+ })
} else {
- stream.getVideoTracks().map(track => pc.addTrack(track))
+ pc.addTransceiver('video', { direction: 'sendonly' })
}
}
- onChangedDeviceAudio() {
- const { pc, stream } = this
- // If WebRTC is connected, switch track
- // NOTE: array audio index is: 0
- pc.getSenders().filter((_, i) => i === 0).map(sender => {
- if (stream) {
- stream.getAudioTracks().map(track => sender.replaceTrack(track))
+ private createSession(generation: number): PublishSession {
+ const pc = new RTCPeerConnection()
+ const session: PublishSession = {
+ generation,
+ client: new WHIPClient(),
+ pc,
+ onStateChange: () => {},
+ }
+ session.onStateChange = () => {
+ if (
+ this.session !== session ||
+ !this.isRunningGeneration(session.generation)
+ )
+ return
+ this.userStatus.state = pc.connectionState as StreamState
+ if (
+ this.userStatus.state === StreamState.Failed ||
+ this.userStatus.state === StreamState.Disconnected
+ ) {
+ log('warn', 'whip_state', pc.connectionState, { id: this.id })
}
- })
+ this.sync()
+ this.syncUserStatus(this.userStatus)
+ }
+ pc.addEventListener('connectionstatechange', session.onStateChange)
+ this.addPublishTracks(pc)
+ return session
}
- async setCurrentDeviceAudio(current: string) {
- const { stream, setStream, userStatus, currentDeviceAudio } = this
-
- if (current !== currentDeviceAudio || !userStatus.audio) {
- // Closed old tracks
- stream.getAudioTracks().map(track => {
- track.stop()
- stream.removeTrack(track)
- })
-
- const mediaStream = await asyncGetAudioStream(current)
+ private cleanupSession(session: PublishSession): Promise {
+ if (session.cleanup) return session.cleanup
+ session.cleanup = (async () => {
+ const { client, pc } = session
+ pc.removeEventListener('connectionstatechange', session.onStateChange)
+ pc.onconnectionstatechange = null
+ pc.onicecandidate = null
+ pc.ontrack = null
+ pc.close()
+ try {
+ if (session.publish) await session.publish.catch(() => {})
+ await client.stop()
+ } catch {
+ // Signaling can fail before an upstream resource exists.
+ } finally {
+ pc.close()
+ }
+ })()
+ return session.cleanup
+ }
- const videoTracks = stream.getVideoTracks()
- const audioTracks = mediaStream.getAudioTracks()
+ private async replaceTrack(
+ kind: 'audio' | 'video',
+ track: MediaStreamTrack | null
+ ) {
+ const session = this.session
+ if (!session || !this.isRunningGeneration(session.generation)) return
+ const transceiver = session.pc
+ .getTransceivers()
+ .find(item => item.receiver.track.kind === kind)
+ if (transceiver) await transceiver.sender.replaceTrack(track)
+ }
- setStream(new MediaStream([...audioTracks, ...videoTracks]))
- userStatus.audio = current === deviceNone.deviceId ? false : true
- this.currentDeviceAudio = current === deviceNone.deviceId ? this.currentDeviceAudio : current
+ async setCurrentDeviceAudio(current: string) {
+ const generation = ++this.audioGeneration
+ if (current === this.currentDeviceAudio && this.userStatus.audio) return
- this.sync()
- this.syncUserStatus(userStatus)
- this.onChangedDeviceAudio()
+ const mediaStream = await asyncGetAudioStream(current)
+ if (generation !== this.audioGeneration) {
+ stopTracks(mediaStream)
+ return
}
- }
- onChangedDeviceVideo() {
- const { pc, stream } = this
- // If WebRTC is connected, switch track
- // NOTE: array video index is: 1
- pc.getSenders().filter((_, i) => i === 1).map(sender => {
- if (stream) {
- stream.getVideoTracks().map(track => sender.replaceTrack(track))
- }
- })
+ const currentStream = this.stream
+ const audioTracks = mediaStream.getAudioTracks()
+ this.stream = new MediaStream([
+ ...audioTracks,
+ ...currentStream.getVideoTracks(),
+ ])
+ stopTracks(currentStream, 'audio')
+ this.userStatus.audio = current !== deviceNone.deviceId
+ if (current !== deviceNone.deviceId) this.currentDeviceAudio = current
+ this.sync()
+ this.syncUserStatus(this.userStatus)
+ await this.replaceTrack('audio', audioTracks[0] || null)
+ if (generation !== this.audioGeneration) return
}
- async setCurrentDeviceVideo(current: string, constraints?: MediaTrackConstraints) {
- const { stream, setStream, userStatus, currentDeviceVideo, currentVideoConstraints } = this
-
- if (current !== currentDeviceVideo || currentVideoConstraints !== constraints || !userStatus.video || this.virtualBackgroundEnabled) {
- // Closed old tracks
- stream.getVideoTracks().map(track => {
- track.stop()
- stream.removeTrack(track)
- })
- let mediaStream: MediaStream
+ private async updateVideo(
+ current: string,
+ constraints?: MediaTrackConstraints,
+ force = false
+ ) {
+ const generation = ++this.videoGeneration
+ this.pendingScreenGeneration =
+ current === deviceScreen.deviceId ? generation : undefined
+ if (
+ !force &&
+ current === this.currentDeviceVideo &&
+ this.currentVideoConstraints === constraints &&
+ this.userStatus.video &&
+ !this.virtualBackgroundEnabled
+ )
+ return
+
+ const previousSegmentation = this.segmentation
+ let mediaStream: MediaStream
+ let segmentation: VirtualBackgroundStream | null = null
+ try {
if (current === deviceSegmenter.deviceId) {
- this.virtualBackgroundEnabled = true
- this.segmentation = new VirtualBackgroundStream(current)
- mediaStream = await this.segmentation.startStream()
- } else {
- this.virtualBackgroundEnabled = false
- if (this.segmentation) {
- this.segmentation.destroyStream()
+ if (this.currentDeviceVideo === deviceNone.deviceId) {
+ throw new Error('No camera available for virtual background')
}
+ segmentation = new VirtualBackgroundStream(this.currentDeviceVideo)
+ mediaStream = await segmentation.startStream()
+ } else {
mediaStream = await asyncGetVideoStream(current, constraints)
}
- const audioTracks = stream.getAudioTracks()
- const videoTracks = mediaStream.getVideoTracks()
+ } catch (e) {
+ if (this.pendingScreenGeneration === generation)
+ this.pendingScreenGeneration = undefined
+ segmentation?.destroyStream()
+ throw e
+ }
- setStream(new MediaStream([...audioTracks, ...videoTracks]))
- userStatus.video = current === deviceNone.deviceId ? false : true
- // NOTE: screen share
- userStatus.screen = current !== deviceScreen.deviceId ? false : true
- this.currentDeviceVideo = (current === deviceNone.deviceId || current === deviceSegmenter.deviceId || current === deviceScreen.deviceId) ? this.currentDeviceVideo : current
- this.currentVideoConstraints = constraints
+ if (this.pendingScreenGeneration === generation)
+ this.pendingScreenGeneration = undefined
+ if (generation !== this.videoGeneration) {
+ stopTracks(mediaStream)
+ segmentation?.destroyStream()
+ return
+ }
+ const videoTracks = mediaStream.getVideoTracks()
+ if (current !== deviceNone.deviceId && videoTracks.length === 0) {
+ stopTracks(mediaStream)
+ segmentation?.destroyStream()
+ throw new Error('No video track available')
+ }
- this.sync()
- this.syncUserStatus(userStatus)
- this.onChangedDeviceVideo()
+ const currentStream = this.stream
+ currentStream.getVideoTracks().forEach(track => {
+ track.onended = null
+ })
+ this.stream = new MediaStream([
+ ...currentStream.getAudioTracks(),
+ ...videoTracks,
+ ])
+ if (current === deviceScreen.deviceId) {
+ videoTracks.forEach(track => {
+ track.onended = () => {
+ if (
+ this.userStatus.screen &&
+ this.stream.getVideoTracks().includes(track)
+ ) {
+ void this.stopScreenSharing()
+ }
+ }
+ })
}
- }
- onconnectionstatechange = () => {
- this.userStatus.state = this.pc.connectionState as StreamState
+ stopTracks(currentStream, 'video')
+ if (previousSegmentation !== segmentation)
+ previousSegmentation?.destroyStream()
+ this.segmentation = segmentation
+ this.virtualBackgroundEnabled = current === deviceSegmenter.deviceId
+ this.userStatus.video = current !== deviceNone.deviceId
+ this.userStatus.screen = current === deviceScreen.deviceId
+ if (
+ ![
+ deviceNone.deviceId,
+ deviceSegmenter.deviceId,
+ deviceScreen.deviceId,
+ ].includes(current)
+ ) {
+ this.currentDeviceVideo = current
+ }
+ this.currentVideoConstraints = constraints
this.sync()
this.syncUserStatus(this.userStatus)
+ await this.replaceTrack('video', videoTracks[0] || null)
+ if (generation !== this.videoGeneration) return
}
- async start() {
- const { id, pc, stream, client, userStatus } = this
- if (stream.getTracks().length === 0) return
- pc.addEventListener('connectionstatechange', this.onconnectionstatechange)
- userStatus.state = StreamState.Signaled
- this.sync()
- this.syncUserStatus(userStatus)
- this.newPeerConnection()
+ async setCurrentDeviceVideo(
+ current: string,
+ constraints?: MediaTrackConstraints
+ ) {
+ await this.updateVideo(current, constraints)
+ }
- try {
- const url = location.origin + `/whip/${id}`
- await client.publish(pc, url, token)
- } catch (e) {
- console.log(e)
- userStatus.state = StreamState.Failed
- this.syncUserStatus(userStatus)
- this.sync()
+ private waitForTracks(generation: number): Promise {
+ if (this.stream.getTracks().length) return Promise.resolve(true)
+ return new Promise(resolve => {
+ let settled = false
+ const finish = (result: boolean) => {
+ if (settled) return
+ settled = true
+ clearTimeout(timer)
+ this.removeEventListener(event.type, onSync)
+ if (this.cancelTrackWait === cancel) this.cancelTrackWait = undefined
+ resolve(result)
+ }
+ const onSync = () => {
+ if (this.stream.getTracks().length) finish(true)
+ }
+ const cancel = () => finish(false)
+ const timer = setTimeout(() => finish(false), 10000)
+ this.cancelTrackWait = cancel
+ this.addEventListener(event.type, onSync)
+ if (!this.isRunningGeneration(generation)) finish(false)
+ else onSync()
+ })
+ }
+
+ private armWatchdog() {
+ if (this.timer === undefined) {
+ this.timer = window.setInterval(() => this.run(), 5000)
}
+ }
+
+ private setFailedState() {
+ this.userStatus.state = StreamState.Failed
+ this.syncUserStatus(this.userStatus)
+ this.sync()
+ }
- if (!this.timer) this.timer = setInterval(() => this.run(), 5000)
+ start(force = false): Promise {
+ if (!force && this.wantsRunning && (this.session || this.startOperation)) {
+ return this.startOperation?.promise || Promise.resolve()
+ }
+ this.wantsRunning = true
+ const generation = ++this.generation
+ this.cancelTrackWait?.()
+ const previous = this.session
+ this.session = undefined
+ const cleanup = previous ? this.cleanupSession(previous) : Promise.resolve()
+
+ const operation = (async () => {
+ try {
+ await cleanup
+ if (!this.isRunningGeneration(generation)) return
+ if (this.stream.getTracks().length === 0) {
+ log('info', 'whip_start_wait_tracks', '', { id: this.id })
+ const gotTracks = await this.waitForTracks(generation)
+ if (!this.isRunningGeneration(generation)) return
+ if (!gotTracks) {
+ log(
+ 'warn',
+ 'whip_start_no_tracks',
+ 'timed out waiting for media tracks',
+ { id: this.id }
+ )
+ this.setFailedState()
+ this.armWatchdog()
+ return
+ }
+ }
+ if (!this.isRunningGeneration(generation)) return
+
+ log('info', 'whip_start', '', { id: this.id })
+ const session = this.createSession(generation)
+ this.session = session
+ this.userStatus.state = StreamState.Signaled
+ this.sync()
+ this.syncUserStatus(this.userStatus)
+ try {
+ session.publish = session.client.publish(
+ session.pc,
+ `${location.origin}/whip/${this.id}`,
+ token
+ )
+ await session.publish
+ } catch (e) {
+ if (
+ this.session === session &&
+ this.isRunningGeneration(generation)
+ ) {
+ log('error', 'whip_publish_failed', String(e), { id: this.id })
+ this.setFailedState()
+ }
+ }
+ if (this.session !== session || !this.isRunningGeneration(generation)) {
+ await this.cleanupSession(session)
+ return
+ }
+ this.armWatchdog()
+ } catch (e) {
+ if (this.isRunningGeneration(generation)) {
+ log('error', 'whip_start_failed', String(e), { id: this.id })
+ this.setFailedState()
+ this.armWatchdog()
+ }
+ } finally {
+ if (this.startOperation?.generation === generation)
+ this.startOperation = undefined
+ }
+ })()
+ this.startOperation = { generation, promise: operation }
+ return operation
}
async stop() {
- if (this.timer) {
+ this.wantsRunning = false
+ this.generation++
+ this.cancelTrackWait?.()
+ if (this.timer !== undefined) {
clearInterval(this.timer)
this.timer = undefined
}
- try {
- await this.client.stop()
- this.pc.removeEventListener('connectionstatechange', this.onconnectionstatechange)
- } catch (e) {
- console.log(e)
- }
+ const session = this.session
+ this.session = undefined
+ this.startOperation = undefined
+ this.userStatus.state = StreamState.Closed
+ this.sync()
+ if (session) await this.cleanupSession(session)
}
- async restart() {
- await this.stop()
- this.pc = new RTCPeerConnection()
- await this.start()
+ restart(): Promise {
+ return this.start(true)
}
run() {
- // WatchDog, Auto Restart
- const restartStates = [StreamState.Disconnected, StreamState.Closed, StreamState.Failed]
- if (restartStates.find(i => i === this.userStatus.state)) this.restart()
+ if (!this.wantsRunning || this.startOperation) return
+ const restartStates = [
+ StreamState.Disconnected,
+ StreamState.Closed,
+ StreamState.Failed,
+ ]
+ if (restartStates.includes(this.userStatus.state)) {
+ log('warn', 'whip_watchdog_restart', this.userStatus.state, {
+ id: this.id,
+ })
+ void this.restart()
+ }
}
}
const contexts: WHIPContext[] = []
-export default function useWhipClient(id: string) {
+export default function useWhipClient(id: string): ShallowRef {
const newContext = (id: string) => {
const context = new WHIPContext(id)
contexts.push(context)
@@ -256,10 +522,13 @@ export default function useWhipClient(id: string) {
}
const context = contexts.find(ctx => ctx.id === id) || newContext(id)
- return useSyncExternalStore((callback: () => void) => {
- context.addEventListener(event.type, callback)
- return () => {
- context.removeEventListener(event.type, callback)
- }
- }, () => context.export())
+ const data = shallowRef(context.export())
+ const update = () => {
+ data.value = context.export()
+ }
+ context.addEventListener(event.type, update)
+ onScopeDispose(() => {
+ context.removeEventListener(event.type, update)
+ })
+ return data
}
diff --git a/webapp/components/use/whxp.ts b/webapp/components/use/whxp.ts
index da99f87..073743d 100644
--- a/webapp/components/use/whxp.ts
+++ b/webapp/components/use/whxp.ts
@@ -1,11 +1,11 @@
-import { Stream, StreamState } from '../../lib/api'
+import { type Stream, StreamState } from '../../lib/api'
const event = new Event('sync')
interface Data {
- id: string,
- stream: MediaStream,
- userStatus: Stream,
+ id: string
+ stream: MediaStream
+ userStatus: Stream
stop: () => Promise
start: () => Promise
@@ -14,7 +14,6 @@ interface Data {
class Context extends EventTarget {
id: string = ''
- pc: RTCPeerConnection = new RTCPeerConnection()
stream: MediaStream = new MediaStream()
userStatus: Stream = {
name: '',
@@ -35,16 +34,9 @@ class Context extends EventTarget {
async start() {}
async restart() {
await this.stop()
- this.pc = new RTCPeerConnection()
await this.start()
}
}
-export {
- Context,
- event,
-}
-
-export type {
- Data,
-}
+export type { Data }
+export { Context, event }
diff --git a/webapp/index.css b/webapp/index.css
index cbcae61..77baa75 100644
--- a/webapp/index.css
+++ b/webapp/index.css
@@ -1,31 +1,33 @@
body {
margin: 0;
padding: 0;
- background-image: linear-gradient(
- 77deg,
- rgb(11 17 33) 0%,
- rgb(12 38 61) 30%,
- rgb(50 30 58) 70%,
- rgb(12 38 61) 100%
- );
- background-size: cover;
- background-repeat: no-repeat;
+ background-color: #0a0f1e;
+ background-image:
+ radial-gradient(
+ 60rem 40rem at 15% 0%,
+ rgba(67, 56, 202, 0.28),
+ transparent 60%
+ ),
+ radial-gradient(
+ 50rem 36rem at 90% 100%,
+ rgba(162, 28, 175, 0.16),
+ transparent 60%
+ );
background-attachment: fixed;
- font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
- 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
- sans-serif;
+ font-family:
+ -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu",
+ "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
- font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
- monospace;
+ font-family:
+ source-code-pro, Menlo, Monaco, Consolas, "Courier New", monospace;
}
.font-border {
font: caption;
-webkit-text-stroke: 0.5px black;
- text-stroke: 0.5px black;
}
diff --git a/webapp/index.html b/webapp/index.html
index 6959043..709b4e0 100644
--- a/webapp/index.html
+++ b/webapp/index.html
@@ -2,12 +2,18 @@
-
+
+
+
+
+
+
+
WOOM
-
+
diff --git a/webapp/lib/api.ts b/webapp/lib/api.ts
index 78bf21a..89d311b 100644
--- a/webapp/lib/api.ts
+++ b/webapp/lib/api.ts
@@ -1,11 +1,56 @@
-/* eslint-disable @typescript-eslint/no-explicit-any */
-
interface Room {
- roomId: string,
- locked: false,
- owner: string,
- presenter?: string,
- streams: any,
+ roomId: string
+ owner: string
+ topic: string
+ locked: boolean
+ presenter: string
+ createdAt: number
+}
+
+interface Meeting {
+ meetingNo: string
+ roomId: string
+ startedAt: number
+ endedAt?: number
+ maxConcurrent: number
+}
+
+interface Participant {
+ userId: string
+ name: string
+ role: 'owner' | 'member'
+ joinAt: number
+ leaveAt?: number
+}
+
+interface RoomDetail extends Room {
+ meeting: Meeting | null
+ participants: Participant[]
+}
+
+interface JoinResponse {
+ meetingNo: string
+ userId: string
+ role: 'owner' | 'member'
+ name: string
+}
+
+interface MeetingRecord {
+ meetingNo: string
+ roomId: string
+ startedAt: number
+ endedAt: number
+ maxConcurrent: number
+ participants: Participant[]
+ messages: Message[]
+}
+
+interface Message {
+ id: string
+ userId: string
+ name: string
+ content: string
+ ts: number
}
/**
@@ -22,121 +67,230 @@ enum StreamState {
}
interface Stream {
- name: string,
+ name: string
state: StreamState
- audio: boolean,
- video: boolean,
- screen: boolean,
+ audio: boolean
+ video: boolean
+ screen: boolean
}
interface User {
- streamId: string,
- token: string,
+ streamId: string
+ token: string
+}
+
+/** Snapshot pushed over SSE, same shape for the initial event and updates */
+interface RoomSnapshot {
+ type: 'snapshot'
+ revision: number
+ meetingNo: string | null
+ streams: Record
+ presenter: string
+ locked: boolean
+ participants: Participant[]
+ messages: Message[]
+ muteAllAt: number
+}
+
+interface RoomMessageEvent {
+ type: 'message'
+ revision: number
+ message: Message
+}
+
+type RoomEvent = RoomSnapshot | RoomMessageEvent
+
+class ApiError extends Error {
+ status: number
+
+ constructor(status: number, message: string) {
+ super(message)
+ this.name = 'ApiError'
+ this.status = status
+ }
}
let token = ''
-let roomId = ''
function setApiToken(str: string) {
token = str
}
-function setRoomId(str: string) {
- roomId = str
+function authHeaders(): HeadersInit {
+ return {
+ Authorization: `Bearer ${token}`,
+ 'Content-Type': 'application/json',
+ }
}
async function newUser(): Promise {
- return (await fetch('/user/', {
- method: 'POST',
- })).json()
+ return (
+ await fetch('/user/', {
+ method: 'POST',
+ })
+ ).json()
}
async function newRoom(): Promise {
- return (await fetch('/room/', {
- headers: {
- 'Authorization': `Bearer ${token}`,
- },
- method: 'POST',
- })).json()
+ return (
+ await fetch('/room/', {
+ headers: authHeaders(),
+ method: 'POST',
+ })
+ ).json()
}
-async function getRoom(roomId: string): Promise {
- return (await fetch(`/room/${roomId}`, {
- headers: {
- 'Authorization': `Bearer ${token}`,
- },
- })).json()
+async function getRoom(id: string): Promise {
+ const res = await fetch(`/room/${id}`, {
+ headers: authHeaders(),
+ })
+ if (!res.ok) throw new ApiError(res.status, await res.text())
+ return res.json()
}
-async function setRoom(roomId: string, data: any): Promise {
- return (await fetch(`/room/${roomId}`, {
- headers: {
- 'Authorization': `Bearer ${token}`,
- 'Content-Type': 'application/json',
- },
+async function patchRoom(
+ id: string,
+ data: Partial<
+ Pick & { password: string }
+ >
+): Promise {
+ const res = await fetch(`/room/${id}`, {
+ headers: authHeaders(),
method: 'PATCH',
body: JSON.stringify(data),
- })).json()
+ })
+ if (!res.ok) throw new ApiError(res.status, await res.text())
+ return res.json()
}
-async function delRoom(roomId: string): Promise {
- return (await fetch(`/room/${roomId}`, {
- headers: {
- 'Authorization': `Bearer ${token}`,
- },
- method: 'DELETE',
- })).json()
+async function joinMeeting(
+ id: string,
+ name: string,
+ password?: string
+): Promise {
+ const res = await fetch(`/room/${id}/join`, {
+ headers: authHeaders(),
+ method: 'POST',
+ body: JSON.stringify({ name, password }),
+ })
+ if (!res.ok) throw new ApiError(res.status, await res.text())
+ return res.json()
}
-async function newStream(roomId: string): Promise {
- return (await fetch(`/room/${roomId}/stream`, {
- headers: {
- 'Authorization': `Bearer ${token}`,
- },
+async function leaveMeeting(id: string): Promise {
+ await fetch(`/room/${id}/leave`, {
+ headers: authHeaders(),
method: 'POST',
- })).json()
+ })
}
-async function setStream(streamId: string, data: any): Promise {
- return (await fetch(`/room/${roomId}/stream/${streamId}`, {
- headers: {
- 'Authorization': `Bearer ${token}`,
- 'Content-Type': 'application/json',
- },
- method: 'PATCH',
- body: JSON.stringify(data),
- })).json()
+/** keepalive variant for page-unload cleanup */
+function leaveMeetingKeepalive(id: string) {
+ void fetch(`/room/${id}/leave`, {
+ headers: authHeaders(),
+ method: 'POST',
+ keepalive: true,
+ }).catch(() => {})
}
-async function delStream(roomId: string, streamId: string): Promise {
- return fetch(`/room/${roomId}/stream/${streamId}`, {
- headers: {
- 'Authorization': `Bearer ${token}`,
- },
- method: 'DELETE',
- keepalive: true,
+async function endMeeting(id: string): Promise {
+ const res = await fetch(`/room/${id}/end`, {
+ headers: authHeaders(),
+ method: 'POST',
})
+ if (!res.ok) throw new ApiError(res.status, await res.text())
}
-export {
- setRoomId,
- setApiToken,
- newUser,
+async function muteAll(id: string): Promise {
+ const res = await fetch(`/room/${id}/mute`, {
+ headers: authHeaders(),
+ method: 'POST',
+ })
+ if (!res.ok) throw new ApiError(res.status, await res.text())
+}
- newRoom,
- getRoom,
- setRoom,
- delRoom,
+async function kickParticipant(id: string, userId: string): Promise {
+ const res = await fetch(`/room/${id}/participant/${userId}`, {
+ headers: authHeaders(),
+ method: 'DELETE',
+ })
+ if (!res.ok) throw new ApiError(res.status, await res.text())
+}
- newStream,
- setStream,
- delStream,
+async function setPresence(id: string, status: Stream): Promise {
+ try {
+ const res = await fetch(`/room/${id}/presence`, {
+ headers: authHeaders(),
+ method: 'PATCH',
+ body: JSON.stringify(status),
+ })
+ if (!res.ok) console.warn('presence rejected:', res.status)
+ return res.ok
+ } catch (e) {
+ console.warn('presence request failed:', e)
+ return false
+ }
+}
- StreamState,
+async function getMyRecords(): Promise {
+ return (
+ await fetch('/user/records', {
+ headers: authHeaders(),
+ })
+ ).json()
+}
- token,
+async function sendMessage(id: string, content: string): Promise {
+ const res = await fetch(`/room/${id}/message`, {
+ headers: authHeaders(),
+ method: 'POST',
+ body: JSON.stringify({ content }),
+ })
+ if (!res.ok) throw new ApiError(res.status, await res.text())
+ return res.json()
+}
+
+async function openRoomEvents(id: string): Promise {
+ const res = await fetch(`/room/${id}/events/ticket`, {
+ headers: authHeaders(),
+ method: 'POST',
+ })
+ if (!res.ok) throw new ApiError(res.status, await res.text())
+ const { ticket } = (await res.json()) as { ticket: string; expiresAt: number }
+ return new EventSource(
+ `/room/${id}/events?ticket=${encodeURIComponent(ticket)}`
+ )
}
export type {
+ JoinResponse,
+ Meeting,
+ MeetingRecord,
+ Message,
+ Participant,
+ Room,
+ RoomDetail,
+ RoomEvent,
+ RoomSnapshot,
Stream,
}
+export {
+ ApiError,
+ endMeeting,
+ getMyRecords,
+ getRoom,
+ joinMeeting,
+ kickParticipant,
+ leaveMeeting,
+ leaveMeetingKeepalive,
+ muteAll,
+ newRoom,
+ newUser,
+ openRoomEvents,
+ patchRoom,
+ StreamState,
+ sendMessage,
+ setApiToken,
+ setPresence,
+ token,
+}
diff --git a/webapp/lib/device.ts b/webapp/lib/device.ts
index 72de411..b5d1642 100644
--- a/webapp/lib/device.ts
+++ b/webapp/lib/device.ts
@@ -1,6 +1,8 @@
+import { log } from './log'
+
interface Device {
- deviceId: string,
- label: string,
+ deviceId: string
+ label: string
}
const deviceNone = {
@@ -15,29 +17,58 @@ const deviceScreen = {
const deviceSegmenter = {
deviceId: 'segmenter',
- lable: 'segmenter'
+ lable: 'segmenter',
}
async function asyncGetAudioStream(deviceId: string): Promise {
let stream: MediaStream = new MediaStream()
if (deviceId !== 'none') {
- stream = await navigator.mediaDevices.getUserMedia({ audio: { deviceId: deviceId }, video: false })
+ try {
+ stream = await navigator.mediaDevices.getUserMedia({
+ audio: { deviceId: deviceId },
+ video: false,
+ })
+ } catch (e) {
+ log('error', 'get_user_media_failed', String(e), {
+ kind: 'audio',
+ deviceId,
+ })
+ throw e
+ }
}
return stream
}
-async function asyncGetVideoStream(deviceId: string, constraints?: MediaTrackConstraints): Promise {
+async function asyncGetVideoStream(
+ deviceId: string,
+ constraints?: MediaTrackConstraints
+): Promise {
let stream: MediaStream = new MediaStream()
- if (deviceId === 'none') {
- /* empty */
- } else if (deviceId === 'screen') {
- stream = await navigator.mediaDevices.getDisplayMedia({ audio: false, video: constraints ?? { height: 720 } })
- } else {
- stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: constraints ?? { height: 480, deviceId: deviceId } })
+ try {
+ if (deviceId === 'none') {
+ /* empty */
+ } else if (deviceId === 'screen') {
+ stream = await navigator.mediaDevices.getDisplayMedia({
+ audio: false,
+ video: constraints ?? { height: 720 },
+ })
+ } else {
+ stream = await navigator.mediaDevices.getUserMedia({
+ audio: false,
+ video: constraints ?? { height: 480, deviceId: deviceId },
+ })
+ }
+ } catch (e) {
+ log('error', 'get_user_media_failed', String(e), {
+ kind: 'video',
+ deviceId,
+ })
+ throw e
}
return stream
}
+export type { Device }
export {
asyncGetAudioStream,
asyncGetVideoStream,
@@ -45,7 +76,3 @@ export {
deviceScreen,
deviceSegmenter,
}
-
-export type {
- Device
-}
diff --git a/webapp/lib/log.ts b/webapp/lib/log.ts
new file mode 100644
index 0000000..6ee4233
--- /dev/null
+++ b/webapp/lib/log.ts
@@ -0,0 +1,87 @@
+/**
+ * Browser log reporter: buffers key events and ships them to the server,
+ * so operators can diagnose user-side failures from server logs.
+ *
+ * Coverage:
+ * - window errors and unhandled promise rejections
+ * - console.warn / console.error (mirrored)
+ * - explicit instrumentation: WHIP/WHEP lifecycle, join, device errors
+ */
+
+import { token as apiToken } from './api'
+
+type Level = 'info' | 'warn' | 'error'
+
+interface Entry {
+ ts: number
+ level: Level
+ event: string
+ msg?: string
+ data?: unknown
+}
+
+const sessionId = crypto.randomUUID()
+let meetingId = ''
+let queue: Entry[] = []
+let flushing = false
+
+export function setLogMeeting(id: string) {
+ meetingId = id
+}
+
+export function log(level: Level, event: string, msg?: string, data?: unknown) {
+ queue.push({ ts: Date.now(), level, event, msg, data })
+ // errors flush immediately; the rest ride the next interval tick
+ if (level === 'error') flush()
+}
+
+function flush() {
+ if (!queue.length || flushing) return
+ flushing = true
+ const logs = queue
+ queue = []
+ fetch('/log', {
+ method: 'POST',
+ headers: {
+ Authorization: `Bearer ${apiToken}`,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ sessionId,
+ meetingId,
+ url: window.location.href,
+ userAgent: navigator.userAgent,
+ logs,
+ }),
+ // keepalive lets entries survive page unload
+ keepalive: true,
+ })
+ .catch(() => {
+ /* never log the logger's own failure (loop risk) */
+ })
+ .finally(() => {
+ flushing = false
+ })
+}
+
+const FLUSH_INTERVAL = 5000
+setInterval(flush, FLUSH_INTERVAL)
+window.addEventListener('beforeunload', flush)
+
+window.addEventListener('error', e => {
+ log('error', 'window_error', e.message, {
+ source: e.filename,
+ line: e.lineno,
+ })
+})
+window.addEventListener('unhandledrejection', e => {
+ log('error', 'unhandled_rejection', String(e.reason))
+})
+
+for (const level of ['warn', 'error'] as const) {
+ const original = console[level]
+ console[level] = (...args: unknown[]) => {
+ log(level, 'console', args.map(String).join(' '))
+ original.apply(console, args)
+ }
+}
diff --git a/webapp/lib/storage.ts b/webapp/lib/storage.ts
index 9dc9abe..2c313ef 100644
--- a/webapp/lib/storage.ts
+++ b/webapp/lib/storage.ts
@@ -4,26 +4,50 @@ const TokenKey = 'token'
const NameKey = 'name'
interface Storage {
- meeting?: string,
- stream?: string,
- token?: string,
- name?: string,
+ meeting?: string
+ stream?: string
+ token?: string
+ name?: string
}
-function setStorageMeeting(value: string) { localStorage.setItem(MeetingKey, value) }
-function setStorageStream(value: string) { localStorage.setItem(StreamKey, value) }
-function setStorageToken(value: string) { localStorage.setItem(TokenKey, value) }
-function setStorageName(value: string) { localStorage.setItem(NameKey, value) }
+function setStorageMeeting(value: string) {
+ localStorage.setItem(MeetingKey, value)
+}
+function setStorageStream(value: string) {
+ localStorage.setItem(StreamKey, value)
+}
+function setStorageToken(value: string) {
+ localStorage.setItem(TokenKey, value)
+}
+function setStorageName(value: string) {
+ localStorage.setItem(NameKey, value)
+}
-function getStorageMeeting(): string { return localStorage.getItem(MeetingKey) || '' }
-function getStorageStream(): string { return localStorage.getItem(StreamKey) || '' }
-function getStorageToken(): string { return localStorage.getItem(TokenKey) || '' }
-function getStorageName(): string { return localStorage.getItem(NameKey) || '' }
+function getStorageMeeting(): string {
+ return localStorage.getItem(MeetingKey) || ''
+}
+function getStorageStream(): string {
+ return localStorage.getItem(StreamKey) || ''
+}
+function getStorageToken(): string {
+ return localStorage.getItem(TokenKey) || ''
+}
+function getStorageName(): string {
+ return localStorage.getItem(NameKey) || ''
+}
-function delStorageMeeting() { localStorage.removeItem(MeetingKey) }
-function delStorageStream() { localStorage.removeItem(StreamKey) }
-function delStorageToken() { localStorage.removeItem(TokenKey) }
-function delStorageName() { localStorage.removeItem(NameKey) }
+function delStorageMeeting() {
+ localStorage.removeItem(MeetingKey)
+}
+function delStorageStream() {
+ localStorage.removeItem(StreamKey)
+}
+function delStorageToken() {
+ localStorage.removeItem(TokenKey)
+}
+function delStorageName() {
+ localStorage.removeItem(NameKey)
+}
function setStorage(opt: Storage) {
if (opt.meeting) setStorageMeeting(opt.meeting)
@@ -49,17 +73,15 @@ function delStorage() {
}
export {
- setStorageMeeting,
- setStorageStream,
- setStorageToken,
- setStorageName,
-
+ delStorage,
+ getStorage,
getStorageMeeting,
+ getStorageName,
getStorageStream,
getStorageToken,
- getStorageName,
-
setStorage,
- getStorage,
- delStorage,
+ setStorageMeeting,
+ setStorageName,
+ setStorageStream,
+ setStorageToken,
}
diff --git a/webapp/lib/util.ts b/webapp/lib/util.ts
index a42bcf3..8c6b106 100644
--- a/webapp/lib/util.ts
+++ b/webapp/lib/util.ts
@@ -18,19 +18,26 @@ function isWechat() {
return /MicroMessenger/i.test(window.navigator.userAgent)
}
-const isFullscreenSupported = typeof document.exitFullscreen === 'function' &&
- typeof document.body.requestFullscreen === 'function'
+const isFullscreenSupported =
+ typeof document.exitFullscreen === 'function' &&
+ typeof document.body.requestFullscreen === 'function'
-const isPictureInPictureSupported = typeof document.exitPictureInPicture === 'function' &&
- typeof HTMLVideoElement.prototype.requestPictureInPicture === 'function'
+const isPictureInPictureSupported =
+ typeof document.exitPictureInPicture === 'function' &&
+ typeof HTMLVideoElement.prototype.requestPictureInPicture === 'function'
-const isScreenShareSupported = !(/Mobi|Android|iPhone|iPad|HarmonyOS|HMSCore/i.test(navigator.userAgent))
+const isMobile = /Mobi|Android|iPhone|iPad|HarmonyOS|HMSCore/i.test(
+ navigator.userAgent
+)
+
+const isScreenShareSupported = !isMobile
export {
addSplitSymbol,
delSplitSymbol,
- isWechat,
isFullscreenSupported,
+ isMobile,
isPictureInPictureSupported,
isScreenShareSupported,
+ isWechat,
}
diff --git a/webapp/main.ts b/webapp/main.ts
new file mode 100644
index 0000000..0d84bf0
--- /dev/null
+++ b/webapp/main.ts
@@ -0,0 +1,17 @@
+import { createApp } from 'vue'
+
+import '@unocss/reset/tailwind-compat.css'
+import 'virtual:uno.css'
+
+import App from './app.vue'
+import './index.css'
+import './lib/log'
+
+// PWA: installable app shell (production only; dev proxies would fight the SW cache)
+if (import.meta.env.PROD && 'serviceWorker' in navigator) {
+ window.addEventListener('load', () => {
+ navigator.serviceWorker.register('/sw.js')
+ })
+}
+
+createApp(App).mount('#root')
diff --git a/webapp/main.tsx b/webapp/main.tsx
deleted file mode 100644
index 26e13b7..0000000
--- a/webapp/main.tsx
+++ /dev/null
@@ -1,19 +0,0 @@
-import React from 'react'
-import { createRoot } from 'react-dom/client'
-import { DevTools } from 'jotai-devtools'
-
-import '@unocss/reset/tailwind-compat.css'
-import 'virtual:uno.css'
-import 'jotai-devtools/styles.css'
-
-import App from './app'
-import './index.css'
-
-const container = document.getElementById('root')
-const root = createRoot(container!)
-root.render(
-
-
-
- ,
-)
diff --git a/webapp/pages/meeting.tsx b/webapp/pages/meeting.tsx
deleted file mode 100644
index 828fa09..0000000
--- a/webapp/pages/meeting.tsx
+++ /dev/null
@@ -1,27 +0,0 @@
-import { useAtom } from 'jotai'
-import { meetingJoinedAtom } from '../store/atom'
-import Layout from '../components/layout'
-import Prepare from '../components/prepare'
-import { useEffect } from 'react'
-import WakeLock from '../components/use/wakeLock'
-
-export default function Meeting(props: { meetingId: string }) {
- const [meetingJoined] = useAtom(meetingJoinedAtom)
- useEffect(() => {
- const lock = new WakeLock()
- if (meetingJoined) {
- lock.request()
- }
- return () => {
- lock.release()
- }
- }, [meetingJoined])
- return (
-
- {meetingJoined
- ?
- :
- }
-
- )
-}
diff --git a/webapp/pages/meeting.vue b/webapp/pages/meeting.vue
new file mode 100644
index 0000000..8984404
--- /dev/null
+++ b/webapp/pages/meeting.vue
@@ -0,0 +1,34 @@
+
+
+
+
+
diff --git a/webapp/pages/welcome.tsx b/webapp/pages/welcome.tsx
deleted file mode 100644
index 8f2176a..0000000
--- a/webapp/pages/welcome.tsx
+++ /dev/null
@@ -1,15 +0,0 @@
-import Join from '../components/join'
-
-export default function Welcome() {
- return (
-
- )
-}
diff --git a/webapp/pages/welcome.vue b/webapp/pages/welcome.vue
new file mode 100644
index 0000000..5f73918
--- /dev/null
+++ b/webapp/pages/welcome.vue
@@ -0,0 +1,48 @@
+
+
+
+
+
+
+
+
+
Meeting Records
+
+ -
+
{{ r.roomId }}
+ {{ formatTime(r.startedAt) }}
+ {{ formatDuration(r.startedAt, r.endedAt) }}
+ {{ r.participants.length }} participants
+
+
+
+
+
diff --git a/webapp/public/apple-touch-icon.png b/webapp/public/apple-touch-icon.png
new file mode 100644
index 0000000..9b029ac
Binary files /dev/null and b/webapp/public/apple-touch-icon.png differ
diff --git a/webapp/public/icon-192.png b/webapp/public/icon-192.png
new file mode 100644
index 0000000..29cfb85
Binary files /dev/null and b/webapp/public/icon-192.png differ
diff --git a/webapp/public/icon-512.png b/webapp/public/icon-512.png
new file mode 100644
index 0000000..d7678e1
Binary files /dev/null and b/webapp/public/icon-512.png differ
diff --git a/webapp/public/icon-maskable-512.png b/webapp/public/icon-maskable-512.png
new file mode 100644
index 0000000..e45ae91
Binary files /dev/null and b/webapp/public/icon-maskable-512.png differ
diff --git a/webapp/public/logo.svg b/webapp/public/logo.svg
index 263804b..14cd458 100644
--- a/webapp/public/logo.svg
+++ b/webapp/public/logo.svg
@@ -1,4 +1,5 @@