diff --git a/.dockerignore b/.dockerignore index 9384eb4..9626c76 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,11 @@ /woom +/target /dist +/static/dist +/data +/.git +/test-results +/playwright-report .idea .vscode node_modules diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..6313b56 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dd119f2..343006e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,28 +1,34 @@ name: Build -on: [push, pull_request] +on: + push: + branches-ignore: + - main + pull_request: {} + workflow_call: {} + +permissions: + contents: read jobs: - deploy: + build: strategy: matrix: os: [macos-latest, ubuntu-latest, windows-latest] - name: Deploy proxy + name: Build (${{ matrix.os }}) runs-on: ${{ matrix.os }} steps: - name: Checkout - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + uses: actions/checkout@v7 + - uses: actions/setup-node@v7 with: - node-version: latest + node-version: 26 + cache: npm - name: Install npm dependencies run: npm ci - name: Build WebApp run: npm run build - - name: Install golang - uses: actions/setup-go@v5 - with: - go-version: '1.21' + - name: Install Rust + uses: dtolnay/rust-toolchain@stable - name: Build Woom - run: go build -tags release -trimpath - + run: cargo build --release --locked diff --git a/.github/workflows/flyio.yml b/.github/workflows/flyio.yml index 4bc7f98..aa21734 100644 --- a/.github/workflows/flyio.yml +++ b/.github/workflows/flyio.yml @@ -5,17 +5,26 @@ on: branches: - main +permissions: + contents: read + jobs: + build: + uses: ./.github/workflows/build.yml + + test: + uses: ./.github/workflows/test.yml + deploy: - name: Deploy proxy + name: Deploy to Fly + needs: [build, test] runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Set flyctl - uses: superfly/flyctl-actions/setup-flyctl@master + uses: superfly/flyctl-actions/setup-flyctl@98172f70657cb3e15db199af139be99bd3e61200 - name: Deploy to fly.io run: flyctl deploy --remote-only env: FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} - diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..09355d8 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,95 @@ +name: Test + +on: + push: + branches-ignore: + - main + pull_request: {} + workflow_call: {} + +permissions: + contents: read + +jobs: + rust: + name: Rust quality + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: 26 + cache: npm + - name: Build embedded WebApp and audit production dependencies + run: npm ci && npm run build && npm audit --omit=dev + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt, clippy + - name: Check format, lints and tests + run: npm run test:rust + + e2e: + name: Playwright e2e + runs-on: ubuntu-latest + services: + live777: + image: ghcr.io/binbat/liveion:0.10.2 + ports: + - 7777:7777 + steps: + - name: Checkout + uses: actions/checkout@v7 + - uses: actions/setup-node@v7 + with: + node-version: 26 + cache: npm + - name: Install npm dependencies + run: npm ci + - name: Install Playwright Chromium + run: npx playwright install --with-deps chromium + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + - name: Run e2e tests + run: npm run test:e2e + - name: Upload Playwright report + if: ${{ !cancelled() }} + uses: actions/upload-artifact@v7 + with: + name: playwright-report + path: playwright-report/ + retention-days: 7 + + docker: + name: Docker smoke + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Build image + run: docker build -t woom:ci . + - name: Start container + run: | + docker run -d --name woom-ci \ + -p 4000:4000 \ + -v woom-ci-data:/data \ + -e SECRET=ci-9f6c2a84d17b43e0a5c98f21e6d04b7a \ + woom:ci + for attempt in $(seq 1 30); do + if curl -fsS http://localhost:4000/ >/dev/null; then + exit 0 + fi + sleep 1 + done + docker logs woom-ci + exit 1 + - name: Check SPA and missing asset responses + run: | + curl -fsS -H 'Accept: text/html' http://localhost:4000/meeting/example >/dev/null + test "$(curl -sS -o /dev/null -w '%{http_code}' -H 'Accept: application/javascript' http://localhost:4000/assets/missing.js)" = 404 + - name: Clean up + if: ${{ !cancelled() }} + run: | + docker rm -f woom-ci + docker volume rm woom-ci-data diff --git a/.gitignore b/.gitignore index 4ba9994..991e3bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ woom +/target +/data/ .idea .vscode node_modules @@ -6,3 +8,7 @@ node_modules dist dist-ssr *.local +/test-results/ +/playwright-report/ +/blob-report/ +/playwright/.cache/ diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..5dcb349 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,115 @@ +# AGENTS.md + +Guidance for AI agents working on this repository. + +## Project overview + +WOOM is a self-hosted meeting service built on the +[Live777](https://github.com/binbat/live777) WebRTC engine (WHIP/WHEP). + +- `src/` — Rust backend (axum). Serves the REST API (`/user/`, `/room/*`), + reverse-proxies `/whip/*`, `/whep/*` and their `/session/*` resources to + Live777, pushes room state over SSE (`/room/{id}/events`), and serves the + WebApp. Media and room APIs require current active-participant + authorization. SSE uses short-lived tickets from + `POST /room/{id}/events/ticket`; `snapshot` and `message` events carry a + per-room monotonically increasing `revision`. +- `src/store.rs` — Atomic domain commands and a single-process durable JSON + store. Rooms, meeting instances, participants, chat and archived records + are persisted with clone-on-write and atomic file replacement. Presence is + ephemeral with a 45s TTL; participant offline reclamation uses a separate + 24h TTL. `DATA_FILE=:memory:` is available for tests. Do not run multiple + processes against one JSON file. +- `src/config.rs` — Environment configuration. `SECRET` is required and must + contain at least 32 bytes. Other variables: `PORT` 4000, `LIVE777_URL`, + `LIVE777_TOKEN`, `DATA_FILE` (default `./data/woom.json`), and optional + `LOG_READ_TOKEN` for admin-only diagnostic export. Never log secrets, JWTs, + SSE ticket query strings or room passwords. +- `src/events.rs` — Per-room broadcast channels. Subscribe before taking the + initial snapshot, deduplicate by revision, and recover from lag with a full + snapshot. All state-changing API operations publish in commit order. +- Browser failures go to `POST /log` and are written as NDJSON on the + `client_log` tracing target. Intake is intentionally anonymous but bounded + and redacts sensitive fields. `GET /log` requires `LOG_READ_TOKEN` and exports + the most recent 5000 in-memory entries as NDJSON with meeting/session filters + for AI-assisted diagnosis. The buffer is cleared on restart. +- `webapp/` — Vue 3 + TypeScript + Vite + UnoCSS frontend (` + + diff --git a/webapp/components/chat.vue b/webapp/components/chat.vue new file mode 100644 index 0000000..7318ab1 --- /dev/null +++ b/webapp/components/chat.vue @@ -0,0 +1,93 @@ + + + diff --git a/webapp/components/device.tsx b/webapp/components/device.tsx deleted file mode 100644 index 0655231..0000000 --- a/webapp/components/device.tsx +++ /dev/null @@ -1,370 +0,0 @@ -import useWhipClient from './use/whip' -import { useAtom } from 'jotai' -import { useEffect, useState } from 'react' -import { - Device, - deviceNone, - deviceScreen, -} from '../lib/device' -import { isScreenShareSupported } from '../lib/util' -import { - deviceSpeakerAtom, - speakerStatusAtom, -} from './../store/atom' - -import Loading from './svg/loading' -import SvgSpeaker from './svg/speaker' -import SvgAudio from './svg/audio' -import SvgVideo from './svg/video' -import { SvgPresentCancel, SvgPresentToAll } from './svg/present' -import { SvgBackgroundCancel, SvgBackground } from './svg/background' -import { SvgSetting } from './svg/setting' -import Settings from './settings' - -import {useSettingStore} from '../store/settingStore' - -function toDevice(info: MediaDeviceInfo): Device { - const deviceId = info.deviceId - let label = info.label - if (label.length <= 0) { - label = `${info.kind} (${deviceId.substring(0, 8)})` - } - return { deviceId, label } -} - -export default function DeviceBar(props: { streamId: string }) { - const [permissionAudio, setPermissionAudio] = useState('') - const [permissionVideo, setPermissionVideo] = useState('') - - const [loadingSpeaker, setLoadingSpeaker] = useState(false) - const [loadingAudio, setLoadingAudio] = useState(false) - const [loadingVideo, setLoadingVideo] = useState(false) - const [loadingScreen, setLoadingScreen] = useState(false) - const [loadingBackground, setLoadingBackground] = useState(false) - - const [currentDeviceSpeaker, setCurrentDeviceSpeaker] = useAtom(deviceSpeakerAtom) - const [speakerStatus, setSpeakerStatus] = useAtom(speakerStatusAtom) - - const screenShareButtonShowed = useSettingStore(state => state.screenShareButtonShowed) - const [virtualBackgroundEnabled, setVirtualBackgroundEnabled] = useState(false) - - const { - userStatus, - currentDeviceAudio, - currentDeviceVideo, - setCurrentDeviceAudio, - setCurrentDeviceVideo, - toggleEnableAudio, - toggleEnableVideo, - toggleEnableVirtualBackground, - } = useWhipClient(props.streamId) - - const [deviceSpeaker, setDeviceSpeaker] = useState([deviceNone]) - const [deviceAudio, setDeviceAudio] = useState([deviceNone]) - const [deviceVideo, setDeviceVideo] = useState([deviceNone]) - - const [isSetting, setIsSetting] = useState(false) - - const permissionsQuery = async () => - (await Promise.all(['camera', 'microphone'].map( - // NOTE: - // Firefox don't have `camera` and `microphone` in permissions - // https://developer.mozilla.org/en-US/docs/Web/API/Permissions/query#name - // https://searchfox.org/mozilla-central/source/dom/webidl/Permissions.webidl#10 - // - // NOTE: - // PermissionName - // https://w3c.github.io/permissions/ - // Reference: https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API - i => navigator.permissions.query({ name: i as PermissionName }) - ))).map(status => { - // NOTE: - // Chrome: audio_capture, video_capture - // Safari: microphone, camera - if (status.name === 'audio_capture' || status.name === 'microphone') { - setPermissionAudio(status.state) - } - if (status.name === 'video_capture' || status.name === 'camera') { - setPermissionVideo(status.state) - } - }) - - const updateDeviceList = async () => { - // to obtain non-empty device label, there needs to be an active media stream or persistent permission - // https://developer.mozilla.org/en-US/docs/Web/API/MediaDeviceInfo/label#value - try { - await navigator.mediaDevices.getUserMedia({ audio: true, video: true }) - } catch { - try { - await navigator.mediaDevices.getUserMedia({ audio: true }) - } catch { /* empty */ } - - try { - await navigator.mediaDevices.getUserMedia({ video: true }) - } catch { /* empty */ } - } - - const devices = (await navigator.mediaDevices.enumerateDevices()).filter(i => !!i.deviceId) - - const speakers = devices.filter(i => i.kind === 'audiooutput').map(toDevice) - const audios = devices.filter(i => i.kind === 'audioinput').map(toDevice) - const videos = devices.filter(i => i.kind === 'videoinput').map(toDevice) - - if (currentDeviceSpeaker === deviceNone.deviceId) { - const device = speakers[0] - if (device) setCurrentDeviceSpeaker(device.deviceId) - } - - if (currentDeviceAudio === deviceNone.deviceId) { - const device = audios[0] - if (device) await setCurrentDeviceAudio(device.deviceId) - } - - if (currentDeviceVideo === deviceNone.deviceId) { - const device = videos[0] - if (device) await setCurrentDeviceVideo(device.deviceId) - } - - setDeviceSpeaker([...speakers]) - setDeviceAudio([...audios]) - setDeviceVideo([...videos]) - } - - const init = async () => { - try { - (await navigator.mediaDevices.getUserMedia({ video: true, audio: true })).getTracks().map(track => track.stop()) - // NOTE: - // In some device have problem: - // - Android Web Browser - // - Wechat WebView - await permissionsQuery() - } catch { - try { - (await navigator.mediaDevices.getUserMedia({ audio: true })).getTracks().map(track => track.stop()) - await permissionsQuery() - } catch { /* empty */ } - try { - (await navigator.mediaDevices.getUserMedia({ video: true })).getTracks().map(track => track.stop()) - await permissionsQuery() - } catch { /* empty */ } - } - await updateDeviceList() - } - - useEffect(() => { - init() - }, []) - - useEffect(() => { - // Reference: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/devicechange_event - navigator.mediaDevices.addEventListener('devicechange', updateDeviceList) - return () => { navigator.mediaDevices.removeEventListener('devicechange', updateDeviceList) } - }, []) - - const onChangedDeviceSpeaker = async (current: string) => { - setLoadingSpeaker(true) - setCurrentDeviceSpeaker(current) - setLoadingSpeaker(false) - } - - const onChangedDeviceAudio = async (current: string) => { - setLoadingAudio(true) - await setCurrentDeviceAudio(current) - setLoadingAudio(false) - } - - const onChangedDeviceVideo = async (current: string, constraints?: MediaTrackConstraints) => { - setLoadingVideo(true) - await setCurrentDeviceVideo(current, constraints) - if (userStatus.screen) { - setVirtualBackgroundEnabled(false) - } - setLoadingVideo(false) - } - - const screenResolutions = [ - { label: '720p (Default)', value: '720' }, - { label: '1080p', value: '1080' }, - { label: 'Native', value: 'native' } - ] - const screenShareResolution = useSettingStore(state => state.screenShareResolution) - const setScreenShareResolution = useSettingStore(state => state.setScreenShareResolution) - - useEffect(() => { - if (userStatus.screen) { - onChangedDeviceVideo(deviceNone.deviceId) - } - }, [screenShareResolution]) - - const toggleEnableScreen = async () => { - const height = Number.parseInt(screenShareResolution) - const constraints = Number.isNaN(height) ? {} : { height } - setLoadingScreen(true) - await onChangedDeviceVideo(userStatus.screen ? deviceNone.deviceId : deviceScreen.deviceId, constraints) - setLoadingScreen(false) - } - - const onChangeScreenShareResolution = async (resolution: string) => { - setScreenShareResolution(resolution) - const height = Number.parseInt(resolution) - const constraints = Number.isNaN(height) ? {} : { height } - setLoadingScreen(true) - await onChangedDeviceVideo(deviceScreen.deviceId, constraints) - setLoadingScreen(false) - } - - return ( -
-
-
- -
-
- {speakerStatus - ?
- :
- } -
- -
- -
- -
- {permissionAudio === 'granted' - ?
- :
- } - {userStatus.audio - ?
- :
- } -
- -
- -
- -
- {permissionVideo === 'granted' - ?
- :
- } - {userStatus.video - ?
- :
- } -
- -
- -
- -
-
- {!screenShareButtonShowed && ( -
-
- -
- -
-
- )} -
- -
- {isSetting && setIsSetting(false)} />} -
- ) -} diff --git a/webapp/components/device.vue b/webapp/components/device.vue new file mode 100644 index 0000000..c6ed7d0 --- /dev/null +++ b/webapp/components/device.vue @@ -0,0 +1,436 @@ + + + diff --git a/webapp/components/join.tsx b/webapp/components/join.tsx deleted file mode 100644 index 39a502c..0000000 --- a/webapp/components/join.tsx +++ /dev/null @@ -1,82 +0,0 @@ -import { useEffect, useState } from 'react' -import { useAtom } from 'jotai' -import { - locationAtom, - meetingIdAtom, -} from '../store/atom' -import { getStorage, setStorage, delStorage, setStorageStream, setStorageMeeting } from '../lib/storage' -import { newRoom, newUser, setApiToken, setRoomId } from '../lib/api' -import { SvgSetting } from './svg/setting' -import Settings from './settings' - -export default function Join() { - const [loc, setLoc] = useAtom(locationAtom) - const [__, setAtomMeetingId] = useAtom(meetingIdAtom) - const [tmpId, setTmpId] = useState('') - const [isSetting, setIsSetting] = useState(false) - - const getLoginStatus = async () => { - const user = getStorage() - if (!user.token || !user.stream) { - const res = await newUser() - user.token = res.token - user.stream = res.streamId - setStorage(user) - } - - setApiToken(user.token) - if (user.stream) setStorageStream(user.stream) - } - - const newMeeting = async () => { - await getLoginStatus() - const meetingId = (await newRoom()).roomId - enterMeeting(meetingId) - setRoomId(meetingId) - } - - const joinMeeting = async () => { - await getLoginStatus() - const meetingId = tmpId - //await fetch(`/room/${meetingId}`, { - // method: "PATCH" - //}) - enterMeeting(meetingId) - setRoomId(meetingId) - } - - const enterMeeting = (meetingId: string) => { - setStorageMeeting(meetingId) - setAtomMeetingId(meetingId) - setLoc(prev => ({ ...prev, pathname: `/${meetingId}` })) - } - - useEffect(() => { - const id = loc.pathname?.replace('/', '') - if (id) { - setTmpId(id) - } - }, [location]) - - return ( -
-
- - setTmpId(e.target.value)} - maxLength={11} - /> - - -
-
-

If have some problems, Please click this:

- Reset -
- {isSetting && setIsSetting(false)} />} -
- ) -} diff --git a/webapp/components/join.vue b/webapp/components/join.vue new file mode 100644 index 0000000..6ca64af --- /dev/null +++ b/webapp/components/join.vue @@ -0,0 +1,76 @@ + + + diff --git a/webapp/components/layout.tsx b/webapp/components/layout.tsx deleted file mode 100644 index 38d2581..0000000 --- a/webapp/components/layout.tsx +++ /dev/null @@ -1,141 +0,0 @@ -import { useEffect, useState } from 'react' -import { useAtom } from 'jotai' -import Player from './player/player' -import WhipPlayer from './player/whip-player' -import WhepPlayer from './player/whep-player' -import DeviceBar from './device' -import { - UserStatus, - enabledPresentationAtom, - meetingJoinedAtom, - presentationStreamAtom, -} from '../store/atom' -import copy from 'copy-to-clipboard' -import SvgDone from './svg/done' -import SvgEnd from './svg/end' -import { getRoom, delStream, Stream } from '../lib/api' -import { getStorageStream } from '../lib/storage' -import useWhipClient from './use/whip' - -export default function Layout(props: { meetingId: string }) { - const [copyStatus, setCopyStatus] = useState(false) - const [_, setMeetingJoined] = useAtom(meetingJoinedAtom) - - const localStreamId = getStorageStream() - const [remoteUserStatus, setRemoteUserStatus] = useState>({}) - - //const [speaker, setSpeaker] = useState(null) - //const [speakerId, setSpeakerId] = useState("") - const [enabledPresentation] = useAtom(enabledPresentationAtom) - const [presentationStream] = useAtom(presentationStreamAtom) - - const {stop, setSyncUserStatus} = useWhipClient(localStreamId) - - const refresh = async () => { - const data = (await getRoom(props.meetingId)).streams - if (data) { - const r = Object.keys(data) - .filter(i => i !== localStreamId) - .filter(i => !!i) - .reduce((map, i) => { - map[i] = data[i] - return map - }, {} as { [_: string]: Stream }) - setRemoteUserStatus(r) - } - } - - const callEnd = async () => { - delStream(props.meetingId, localStreamId) - - setMeetingJoined(false) - stop() - setSyncUserStatus(() => {}) - } - - useEffect(() => { - const cleanup = () => { - delStream(props.meetingId, localStreamId) - } - // NOTE: - // https://github.com/binbat/woom/pull/27 - // https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event - // `unload` event is Deprecated, But Firefox need `unload` - // - // event | Chrome | Firefox | Safari - // ---------------------------- | ------------------------- | ------------------------- | ----- - // `beforeunload` | ok | console error, no request | console error, request ok - // `unload` | console error, no request | ok | console no request log, no request - // `beforeunload` + `keepalive` | ok | console error, no request | console error, request ok - // `unload` + `keepalive` | console error, request ok | ok | console no request log, request ok - window.addEventListener('beforeunload', cleanup) - window.addEventListener('unload', cleanup) - return () => { - window.removeEventListener('beforeunload', cleanup) - window.removeEventListener('unload', cleanup) - } - }, [props.meetingId, localStreamId]) - - //useEffect(() => { - // let shareScreenId = "" - // const setShareScreenId = (id: string) => shareScreenId = id - // Object.keys(remoteUserStatus).map(i => remoteUserStatus[i].screen && setShareScreenId(i)) - // if (!shareScreenId) { - // setSpeakerId("") - // setSpeaker(null) - // } else { - // setSpeakerId(shareScreenId) - // setSpeaker(remoteUserStatus[shareScreenId]) - // } - //}, [remoteUserStatus]) - - useEffect(() => { - const handle = setInterval(refresh, 3000) - return () => clearInterval(handle) - }, []) - - return ( -
-
- - {enabledPresentation - ? - : null - } - -
- - {Object.keys(remoteUserStatus).map(i => )} -
- -
-
-
- -
- - - -
- -
- -
-
- -
- ) -} diff --git a/webapp/components/layout.vue b/webapp/components/layout.vue new file mode 100644 index 0000000..f80ef23 --- /dev/null +++ b/webapp/components/layout.vue @@ -0,0 +1,435 @@ + + + diff --git a/webapp/components/members.vue b/webapp/components/members.vue new file mode 100644 index 0000000..fbb4523 --- /dev/null +++ b/webapp/components/members.vue @@ -0,0 +1,103 @@ + + + diff --git a/webapp/components/player/circle-wave.vue b/webapp/components/player/circle-wave.vue new file mode 100644 index 0000000..15ed976 --- /dev/null +++ b/webapp/components/player/circle-wave.vue @@ -0,0 +1,85 @@ + + + diff --git a/webapp/components/player/detail.tsx b/webapp/components/player/detail.tsx deleted file mode 100644 index 40dd58c..0000000 --- a/webapp/components/player/detail.tsx +++ /dev/null @@ -1,27 +0,0 @@ -import { UserStatus } from '../../store/atom' - -export default function Detail(props: { streamId: string, connStatus: string, userStatus: UserStatus, restart: () => void }) { - const { streamId, connStatus, userStatus, restart } = props - return ( -
- {userStatus.name} -
-
-

name: {userStatus.name}

-

state: {String(userStatus.state)}

-
-
-

audio: {String(userStatus.audio)}

-

video: {String(userStatus.video)}

-

screen: {String(userStatus.screen)}

-
- {streamId} -
- -
-

{userStatus.state}

- -
-
- ) -} diff --git a/webapp/components/player/detail.vue b/webapp/components/player/detail.vue new file mode 100644 index 0000000..a421eeb --- /dev/null +++ b/webapp/components/player/detail.vue @@ -0,0 +1,33 @@ + + + diff --git a/webapp/components/player/mic.vue b/webapp/components/player/mic.vue new file mode 100644 index 0000000..35cca3d --- /dev/null +++ b/webapp/components/player/mic.vue @@ -0,0 +1,51 @@ + + + diff --git a/webapp/components/player/player.tsx b/webapp/components/player/player.tsx deleted file mode 100644 index c25ca2d..0000000 --- a/webapp/components/player/player.tsx +++ /dev/null @@ -1,266 +0,0 @@ -import { useEffect, useRef, useState } from 'react' -import { useAtom } from 'jotai' -import WaveSurfer from 'wavesurfer.js' -import RecordPlugin from 'wavesurfer.js/dist/plugins/record' -import { - isWechat, - isFullscreenSupported, - isPictureInPictureSupported, -} from '../../lib/util' -import SvgProgress from '../svg/progress' -import { deviceSpeakerAtom, speakerStatusAtom } from '../../store/atom' -import { - SvgMuted, - SvgUnmuted, - SvgFullscreen, - SvgExitFullscreen, - SvgPictureInPicture, - SvgExitPictureInPicture, - SvgMic, -} from '../svg/player' -import VolumeExtractor from '../use/volumeExtraction' - -function AudioWave(props: { stream: MediaStream }) { - const refWave = useRef(null) - - useEffect(() => { - if (refWave.current && !!props.stream?.getAudioTracks().length) { - const wavesurfer = WaveSurfer.create({ - container: refWave.current, - waveColor: 'rgb(200, 100, 0)', - progressColor: 'rgb(100, 50, 0)', - }) - - const record = wavesurfer.registerPlugin(RecordPlugin.create()) - const { onDestroy, onEnd } = record.renderMicStream(new MediaStream(props.stream.getAudioTracks())) - - return () => { - onDestroy() - onEnd() - wavesurfer.destroy() - } - } - }, [refWave.current, props.stream]) - - return
-} - -function Mic(props: { stream: MediaStream }) { - const [volumeValue, setVolumeValue] = useState(0) - const rest = 100 - volumeValue - useEffect(() => { - let done = false - if (props.stream.getAudioTracks().length) { - const extractor = new VolumeExtractor(props.stream) - function updateVolume() { - const value = extractor.calculateVolume() - setVolumeValue(value) - if (!done) requestAnimationFrame(updateVolume) - } - updateVolume() - } - return(() => { - done = true - }) - }, [props.stream]) - return ( -
3 ? '1' : '0' - }} - > - -
- ) -} - -export default function Player(props: { stream: MediaStream, muted: boolean, audio?: boolean, video?: boolean, width: string, self: boolean }) { - const refVideo = useRef(null) - const audioTrack = props.stream.getAudioTracks()[0] - const videoTrack = props.stream.getVideoTracks()[0] - const [currentDeviceSpeaker] = useAtom(deviceSpeakerAtom) - const [speakerStatus] = useAtom(speakerStatusAtom) - const refPlayPromise = useRef>() - const refControls = useRef(null) - const [showControls, setShowControls] = useState(false) - const refTimeoutId = useRef() - const [isMuted, setIsMuted] = useState(false) - const [isFullscreened, setIsFullscreened] = useState(false) - const [isPictureInPictured, setIsPictureInPictured] = useState(false) - const hasTracks = props.stream.getTracks().length > 0 - const mediaEnabled = props.audio || props.video - const showAudio = props.audio && !props.video - const mediaDisabledTip = `All ${props.self ? 'your' : 'their'} media are disabled` - - const handleMouseMove = () => { - setShowControls(true) - if (refTimeoutId.current) clearTimeout(refTimeoutId.current) - const newTimeout = setTimeout(() => { - setShowControls(false) - }, 2000) - refTimeoutId.current = newTimeout - } - - const toggleMute = () => isMuted ? setIsMuted(false) : setIsMuted(true) - - const toggleFullscreen = () => { - const container = refVideo.current!.parentElement! - if (document.fullscreenElement) { - document.exitFullscreen() - } else { - container.requestFullscreen() - } - } - - const togglePictureInPicture = () => { - const container = refVideo.current! - if (document.pictureInPictureElement) { - document.exitPictureInPicture() - } else { - container.requestPictureInPicture() - } - } - - useEffect(() => { - const onFullScreenChange = () => document.fullscreenElement ? setIsFullscreened(true) : setIsFullscreened(false) - if (isFullscreenSupported) document.addEventListener('fullscreenchange', onFullScreenChange) - return () => { - document.removeEventListener('fullscreenchange', onFullScreenChange) - } - }, []) - - useEffect(() => { - const onEnterPictureInPicture = () => setIsPictureInPictured(true) - const onLeavePictureInPicture = () => setIsPictureInPictured(false) - const video = refVideo.current - if (isPictureInPictureSupported) { - video?.addEventListener('enterpictureinpicture', onEnterPictureInPicture) - video?.addEventListener('leavepictureinpicture', onLeavePictureInPicture) - } - return () => { - video?.removeEventListener('enterpictureinpicture', onEnterPictureInPicture) - video?.removeEventListener('leavepictureinpicture', onLeavePictureInPicture) - } - }, []) - - useEffect(() => { - const container = refControls.current?.parentElement - container?.addEventListener('mousemove', handleMouseMove) - return () => { - container?.removeEventListener('mousemove', handleMouseMove) - } - }, []) - - useEffect(() => { - setIsMuted(!speakerStatus) - }, [speakerStatus]) - - useEffect(() => { - const video = refVideo.current - const onVolumeChange = () => video!.muted ? setIsMuted(true) : setIsMuted(false) - video?.addEventListener('volumechange', onVolumeChange) - return () => { - video?.removeEventListener('volumechange', onVolumeChange) - } - }, []) - - useEffect(() => { - if (audioTrack && props.audio && !props.muted) { - const el = document.createElement('audio') - el.srcObject = new MediaStream([audioTrack]) - - if (el.setSinkId) { - el.setSinkId(currentDeviceSpeaker) - } - - el.muted = !speakerStatus || isMuted - refVideo.current!.muted = !speakerStatus || isMuted - refPlayPromise.current = el.play() - - return () => { - refPlayPromise.current?.finally(() => { - el.pause() - el.srcObject = null - el.remove() - }) - } - } - }, [audioTrack, videoTrack, currentDeviceSpeaker, speakerStatus, isMuted]) - - useEffect(() => { - if (refVideo.current && videoTrack) { - refVideo.current.srcObject = new MediaStream([videoTrack]) - - // NOTE: About Autoplay - // Reference: https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide - - // NOTE: - // iOS Wechat WebView - // https://developers.weixin.qq.com/community/develop/doc/0006a61de48ab0165f99e1dcd51c00 - if (isWechat()) refVideo.current.play() - } - }, [refVideo, videoTrack]) - - // NOTE: iOS can't display video - // https://webkit.org/blog/6784/new-video-policies-for-ios/ - return ( -
- {!mediaEnabled ?

{mediaDisabledTip}

: null} - {mediaEnabled && !hasTracks ? : null} -
- ) -} diff --git a/webapp/components/player/player.vue b/webapp/components/player/player.vue new file mode 100644 index 0000000..defdd14 --- /dev/null +++ b/webapp/components/player/player.vue @@ -0,0 +1,263 @@ + + + + diff --git a/webapp/components/player/whep-player.tsx b/webapp/components/player/whep-player.tsx deleted file mode 100644 index d94ee2f..0000000 --- a/webapp/components/player/whep-player.tsx +++ /dev/null @@ -1,60 +0,0 @@ -import { useEffect, useRef } from 'react' -import useWhepClient from '../use/whep' -import { useAtom } from 'jotai' -import Detail from './detail' -import Player from './player' -import { presentationStreamAtom } from '../../store/atom' -import { Stream } from '../../lib/api' - -export default function WhepPlayer(props: { streamId: string, userStatus: Stream, width: string }) { - const refEnabled = useRef(false) - const { stream, restart, start, connStatus, setRemoteStatus } = useWhepClient(props.streamId) - const [presentationStream, setPresentationStream] = useAtom(presentationStreamAtom) - const refPresentationStream = useRef(presentationStream) - refPresentationStream.current = presentationStream - - useEffect(() => { - if (!refEnabled.current) { - refEnabled.current = true - start() - } - // remove "presentation stream" when sharing user exits - // TODO: manage users entering/exiting globally - return () => { - const selfStreamName = `${props.userStatus.name}_Presentation` - if (refPresentationStream.current.name === selfStreamName) { - setPresentationStream({ - name: '', - stream: new MediaStream(), - }) - } - } - }, []) - - useEffect(() => { - setRemoteStatus(props.userStatus) - }, [props.userStatus]) - - useEffect(() => { - // set/clear "presentation stream" when remote user starts/stops sharing - const selfStreamName = `${props.userStatus.name}_Presentation` - if (props.userStatus.screen && refPresentationStream.current.stream !== stream) { - setPresentationStream({ - name: selfStreamName, - stream: stream, - }) - } else if (!props.userStatus.screen && refPresentationStream.current.name === selfStreamName) { - setPresentationStream({ - name: '', - stream: new MediaStream(), - }) - } - }, [props.userStatus.screen, stream]) - - return ( -
- - -
- ) -} diff --git a/webapp/components/player/whep-player.vue b/webapp/components/player/whep-player.vue new file mode 100644 index 0000000..6335060 --- /dev/null +++ b/webapp/components/player/whep-player.vue @@ -0,0 +1,85 @@ + + + diff --git a/webapp/components/player/whip-player.tsx b/webapp/components/player/whip-player.tsx deleted file mode 100644 index c9e1aa4..0000000 --- a/webapp/components/player/whip-player.tsx +++ /dev/null @@ -1,37 +0,0 @@ -import { useEffect, useRef } from 'react' -import useWhipClient from '../use/whip' -import { useAtom } from 'jotai' -import Detail from './detail' -import Player from './player' -import { presentationStreamAtom } from '../../store/atom' - -export default function WhipPlayer(props: { streamId: string, width: string }) { - const { stream, userStatus, restart } = useWhipClient(props.streamId) - // TODO: refactor "presentation stream" handling for more precise control - const [presentationStream, setPresentationStream] = useAtom(presentationStreamAtom) - const refPresentationStream = useRef(presentationStream) - refPresentationStream.current = presentationStream - - useEffect(() => { - // set/clear "presentation stream" when self starts/stops sharing - const selfStreamName = `${userStatus.name}_Presentation` - if (userStatus.screen && refPresentationStream.current.stream !== stream) { - setPresentationStream({ - name: selfStreamName, - stream: stream, - }) - } else if (!userStatus.screen && refPresentationStream.current.name === selfStreamName) { - setPresentationStream({ - name: '', - stream: new MediaStream(), - }) - } - }, [userStatus.screen, stream]) - - return ( -
- - -
- ) -} diff --git a/webapp/components/player/whip-player.vue b/webapp/components/player/whip-player.vue new file mode 100644 index 0000000..d12c58a --- /dev/null +++ b/webapp/components/player/whip-player.vue @@ -0,0 +1,39 @@ + + + diff --git a/webapp/components/prepare.tsx b/webapp/components/prepare.tsx deleted file mode 100644 index b2ea459..0000000 --- a/webapp/components/prepare.tsx +++ /dev/null @@ -1,75 +0,0 @@ -import { useEffect, useState } from 'react' -import { useAtom } from 'jotai' -import useWhipClient from './use/whip' -import DeviceBar from './device' -import Loading from './svg/loading' -import Player from './player/player' -import { - meetingJoinedAtom, -} from '../store/atom' -import { getStorageName, setStorageName, getStorageStream } from '../lib/storage' -import { setStream } from '../lib/api' - -export default function Prepare(_props: { meetingId: string }) { - const [loading, setLoading] = useState(false) - - const [displayName, setDisplayName] = useState('') - - 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 @@ + + + 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 @@ + + + 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 @@ + + + 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 ( -
-
-
-

WOOM

-

A new meeting

-
- -
-
- ) -} 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 @@ + + + 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 @@ +WOOM logo diff --git a/webapp/public/manifest.webmanifest b/webapp/public/manifest.webmanifest new file mode 100644 index 0000000..44ad21e --- /dev/null +++ b/webapp/public/manifest.webmanifest @@ -0,0 +1,32 @@ +{ + "name": "WOOM", + "short_name": "WOOM", + "description": "A new meeting", + "id": "/", + "start_url": "/", + "scope": "/", + "display": "standalone", + "orientation": "any", + "background_color": "#0a0f1e", + "theme_color": "#0a0f1e", + "icons": [ + { + "src": "/icon-192.png", + "sizes": "192x192", + "type": "image/png", + "purpose": "any" + }, + { + "src": "/icon-512.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "any" + }, + { + "src": "/icon-maskable-512.png", + "sizes": "512x512", + "type": "image/png", + "purpose": "maskable" + } + ] +} diff --git a/webapp/public/sw.js b/webapp/public/sw.js new file mode 100644 index 0000000..21f1568 --- /dev/null +++ b/webapp/public/sw.js @@ -0,0 +1,72 @@ +const CACHE_PREFIX = 'woom-shell-' +const CACHE = '__WOOM_CACHE__' +const CORE = JSON.parse('__WOOM_PRECACHE__') +const BYPASS = /^\/(user|room|log|whip|whep|session)(\/|$)/ + +self.addEventListener('install', event => { + event.waitUntil( + (async () => { + const cache = await caches.open(CACHE) + await cache.addAll(CORE) + })() + ) +}) + +self.addEventListener('activate', event => { + event.waitUntil( + (async () => { + const keys = await caches.keys() + await Promise.all( + keys + .filter(key => key.startsWith(CACHE_PREFIX) && key !== CACHE) + .map(key => caches.delete(key)) + ) + })() + ) +}) + +self.addEventListener('fetch', event => { + const url = new URL(event.request.url) + if ( + event.request.method !== 'GET' || + url.origin !== location.origin || + BYPASS.test(url.pathname) + ) + return + + if (event.request.mode === 'navigate') { + event.respondWith( + (async () => { + try { + return await fetch(event.request) + } catch { + const cache = await caches.open(CACHE) + const cached = await cache.match('/index.html') + if (cached) return cached + throw new Error('App shell is not cached') + } + })() + ) + return + } + + event.respondWith( + (async () => { + const cache = await caches.open(CACHE) + const cached = await cache.match(event.request) + if (cached) return cached + + const response = await fetch(event.request) + const contentType = response.headers.get('content-type') || '' + if ( + response.ok && + response.type === 'basic' && + response.status !== 206 && + !contentType.toLowerCase().includes('text/html') + ) { + await cache.put(event.request, response.clone()) + } + return response + })() + ) +}) diff --git a/webapp/store/atom.ts b/webapp/store/atom.ts deleted file mode 100644 index 59009f9..0000000 --- a/webapp/store/atom.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { atom } from 'jotai' -import { atomWithLocation } from 'jotai-location' -import { StreamState } from '../lib/api' - -const locationAtom = atomWithLocation() -locationAtom.debugLabel = 'location' - -interface UserStream { - stream: MediaStream, - name: string -} - -interface UserStatus { - /** - * nick name - */ - name: string - state: StreamState - audio: boolean - video: boolean - screen: boolean -} - -const meetingIdAtom = atom('') -meetingIdAtom.debugLabel = 'meetingIdAtom' -const meetingJoinedAtom = atom(false) -meetingJoinedAtom.debugLabel = 'meetingJoined' - -const presentationStreamAtom = atom({ - stream: new MediaStream(), - name: 'Presentation', -}) -presentationStreamAtom.debugLabel = 'presentationStream' - -const enabledPresentationAtom = atom(get => get(presentationStreamAtom).stream.getVideoTracks().length !== 0) -enabledPresentationAtom.debugLabel = 'enabledPresentation' - -const deviceSpeakerAtom = atom('') -deviceSpeakerAtom.debugLabel = 'deviceSpeaker' -const speakerStatusAtom = atom(false) -speakerStatusAtom.debugLabel = 'speakerStatus' - -export { - locationAtom, - presentationStreamAtom, - - meetingIdAtom, - meetingJoinedAtom, - enabledPresentationAtom, - deviceSpeakerAtom, - speakerStatusAtom, -} - -export type { - UserStream, - UserStatus, -} diff --git a/webapp/store/atoms.ts b/webapp/store/atoms.ts new file mode 100644 index 0000000..154f3e5 --- /dev/null +++ b/webapp/store/atoms.ts @@ -0,0 +1,59 @@ +import { computed, ref, shallowRef } from 'vue' +import type { Participant, StreamState } from '../lib/api' + +interface UserStream { + streamId: string + stream: MediaStream + name: string +} + +interface UserStatus { + /** + * nick name + */ + name: string + state: StreamState + audio: boolean + video: boolean + screen: boolean +} + +const meetingId = ref('') +const meetingJoined = ref(false) +const joinedMeetingNo = ref('') +const joinPassword = ref('') + +const presentationStream = shallowRef({ + streamId: '', + stream: new MediaStream(), + name: 'Presentation', +}) + +const enabledPresentation = computed( + () => presentationStream.value.stream.getVideoTracks().length !== 0 +) + +const deviceSpeaker = ref('') +const speakerStatus = ref(false) + +/** live room flags from the SSE snapshot (presenter/lock can change anytime) */ +const roomInfo = ref<{ owner: string; presenter: string; locked: boolean }>({ + owner: '', + presenter: '', + locked: false, +}) +const roomParticipants = ref([]) + +export type { UserStatus, UserStream } +export { + deviceSpeaker, + enabledPresentation, + joinedMeetingNo, + joinPassword, + meetingId, + meetingJoined, + presentationStream, + roomInfo, + roomParticipants, + speakerStatus, +} diff --git a/webapp/store/setting.ts b/webapp/store/setting.ts new file mode 100644 index 0000000..ba3e766 --- /dev/null +++ b/webapp/store/setting.ts @@ -0,0 +1,46 @@ +import { reactive, watch } from 'vue' + +const STORAGE_KEY = 'setting-storage' + +interface SettingState { + language: string + videoResolution: string + screenShareResolution: string + screenShareButtonShowed: boolean +} + +const defaultState = (): SettingState => ({ + language: 'English', + videoResolution: '480', + screenShareResolution: '720', + screenShareButtonShowed: /Mobi|Android|iPhone|iPad|HarmonyOS|HMSCore/i.test( + navigator.userAgent + ), +}) + +function load(): SettingState { + try { + const raw = JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}') + // the old zustand/persist format stored the fields under `state` + const stored = raw.state ?? raw + return { ...defaultState(), ...stored } + } catch { + return defaultState() + } +} + +const setting = reactive(load()) + +watch(setting, s => { + localStorage.setItem( + STORAGE_KEY, + JSON.stringify({ + language: s.language, + videoResolution: s.videoResolution, + screenShareResolution: s.screenShareResolution, + screenShareButtonShowed: s.screenShareButtonShowed, + }) + ) +}) + +export { setting } diff --git a/webapp/store/settingStore.ts b/webapp/store/settingStore.ts deleted file mode 100644 index c88b444..0000000 --- a/webapp/store/settingStore.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { create } from 'zustand' -import { persist } from 'zustand/middleware' - -interface settingState { - language: string - videoResolution: string - screenShareResolution: string - screenShareButtonShowed: boolean - setLanguage: (input: string) => void - setVideoResolution: (input: string) => void - setScreenShareResolution: (input: string) => void - setScreenShareButtonShowed: (input: boolean) => void -} - -export const useSettingStore = create()( - persist( - (set) => ({ - language: 'English', - videoResolution: '480', - screenShareResolution: '720', - screenShareButtonShowed: /Mobi|Android|iPhone|iPad|HarmonyOS|HMSCore/i.test(navigator.userAgent), - setLanguage: (input: string) => set({ language: input }), - setVideoResolution: (input: string) => set({ videoResolution: input }), - setScreenShareResolution: (input: string) => set({ screenShareResolution: input }), - setScreenShareButtonShowed: (input: boolean) => set({ screenShareButtonShowed: input }), - }), - { - name: 'setting-storage', - partialize: (state) => ({ - language: state.language, - videoResolution: state.videoResolution, - screenShareResolution: state.screenShareResolution, - screenShareButtonShowed: state.screenShareButtonShowed, - }), - } - ) -) diff --git a/webapp/uno.config.ts b/webapp/uno.config.ts index 2a718b4..a03ddbf 100644 --- a/webapp/uno.config.ts +++ b/webapp/uno.config.ts @@ -1,23 +1,30 @@ -import { - defineConfig, - presetAttributify, - presetIcons, - presetUno, -} from 'unocss' +import { defineConfig, presetAttributify, presetIcons, presetUno } from 'unocss' export default defineConfig({ shortcuts: [ - { logo: 'i-logos-react w-6em h-6em transform transition-800 hover:rotate-180' }, - { 'btn-primary': 'py-2 px-4 bg-blue-500 duration-500 text-white font-bold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75 disabled:pointer-events-none disabled:bg-slate-300' } + { + logo: 'i-logos-react w-6em h-6em transform transition-800 hover:rotate-180', + }, + { + 'btn-primary': + 'inline-flex items-center justify-center gap-2 px-5 py-2.5 rounded-xl font-semibold text-white bg-indigo-600 shadow-lg shadow-indigo-950/40 transition-colors duration-200 hover:bg-indigo-500 focus:outline-none focus-visible:ring-2 focus-visible:ring-indigo-400 disabled:pointer-events-none disabled:bg-slate-600 disabled:shadow-none', + panel: + 'bg-white/5 backdrop-blur-md border border-white/10 rounded-2xl shadow-2xl', + 'input-dark': + 'bg-white/10 border border-white/15 rounded-xl px-4 py-2.5 text-white placeholder-slate-400 transition-colors focus:outline-none focus:border-indigo-400 focus:bg-white/15', + 'icon-btn': + 'relative inline-grid place-items-center w-10 h-10 rounded-xl text-slate-200 bg-white/5 border border-white/10 transition-colors duration-200 hover:bg-white/15 disabled:opacity-40 disabled:pointer-events-none', + chip: 'inline-flex items-center gap-1.5 px-2.5 py-1 rounded-lg bg-black/50 backdrop-blur text-xs font-medium text-white', + }, ], presets: [ presetUno(), presetAttributify(), presetIcons({ extraProperties: { - 'display': 'inline-block', - 'vertical-align': 'middle' - } - }) - ] + display: 'inline-block', + 'vertical-align': 'middle', + }, + }), + ], }) diff --git a/webapp/vite.config.ts b/webapp/vite.config.ts index 373775e..d0410dd 100644 --- a/webapp/vite.config.ts +++ b/webapp/vite.config.ts @@ -1,11 +1,54 @@ +import { createHash } from 'node:crypto' +import { readFile, writeFile } from 'node:fs/promises' import path from 'node:path' - -import { defineConfig } from 'vite' +import Vue from '@vitejs/plugin-vue' import UnoCSS from 'unocss/vite' -import React from '@vitejs/plugin-react' +import { defineConfig } from 'vite' const ProjectRoot = path.resolve(import.meta.dirname, '..') const WebAppRoot = path.resolve(ProjectRoot, 'webapp') +const DistRoot = path.resolve(ProjectRoot, 'static/dist') + +const precacheServiceWorker = () => ({ + name: 'woom-precache-service-worker', + apply: 'build' as const, + closeBundle: async () => { + const html = await readFile(path.join(DistRoot, 'index.html'), 'utf8') + const urls = new Set([ + '/index.html', + '/manifest.webmanifest', + '/logo.svg', + '/apple-touch-icon.png', + '/icon-192.png', + '/icon-512.png', + '/icon-maskable-512.png', + ]) + for (const match of html.matchAll(/\b(?:src|href)=["']([^"']+)["']/g)) { + const url = match[1] + if (url.startsWith('/') && !url.startsWith('//')) urls.add(url) + } + + const precache = [...urls].sort() + const hash = createHash('sha256') + for (const url of precache) { + const pathname = decodeURIComponent( + new URL(url, 'http://woom.local').pathname + ) + const contents = await readFile( + path.join(DistRoot, pathname.replace(/^\/+/, '')) + ) + hash.update(url).update(contents) + } + const serviceWorkerPath = path.join(DistRoot, 'sw.js') + const serviceWorker = await readFile(serviceWorkerPath, 'utf8') + hash.update(serviceWorker) + const version = hash.digest('hex').slice(0, 16) + const output = serviceWorker + .replace('__WOOM_CACHE__', `woom-shell-${version}`) + .replace('__WOOM_PRECACHE__', JSON.stringify(precache)) + await writeFile(serviceWorkerPath, output) + }, +}) /** * vite config @@ -16,39 +59,47 @@ export default defineConfig({ publicDir: path.resolve(WebAppRoot, 'public'), server: { proxy: { - '^/session/.*': 'http://localhost:7777', - '^/whip/.*': 'http://localhost:7777', - '^/whep/.*': 'http://localhost:7777', + '^/session/.*': 'http://localhost:4000', + '^/whip/.*': 'http://localhost:4000', + '^/whep/.*': 'http://localhost:4000', + '^/log(?:/|$)': 'http://localhost:4000', '^/room/.*': 'http://localhost:4000', - '^/user/.*': 'http://localhost:4000' - } + '^/user/.*': 'http://localhost:4000', + }, }, build: { - outDir: path.resolve(ProjectRoot, 'static/dist'), - emptyOutDir: true + outDir: DistRoot, + emptyOutDir: true, }, plugins: [ UnoCSS(path.resolve(WebAppRoot, 'uno.config.ts')), - React() + Vue(), + precacheServiceWorker(), ], resolve: { alias: [ { find: '@', - replacement: WebAppRoot + replacement: WebAppRoot, }, // "./wasm" was not defind in "exports" of "@mediapipe/tasks-vision" { find: '@mediapipe/tasks-vision/wasm', - replacement: path.join(ProjectRoot, 'node_modules', '@mediapipe', 'tasks-vision', 'wasm') - } - ] + replacement: path.join( + ProjectRoot, + 'node_modules', + '@mediapipe', + 'tasks-vision', + 'wasm' + ), + }, + ], }, optimizeDeps: { // vite would complain without excluding them exclude: [ '@mediapipe/tasks-vision/wasm/vision_wasm_internal.js?url', - '@mediapipe/tasks-vision/wasm/vision_wasm_internal.wasm?url' - ] + '@mediapipe/tasks-vision/wasm/vision_wasm_internal.wasm?url', + ], }, })