Skip to content

Commit

Permalink
feat(webapp): change device default enable
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Dec 15, 2023
1 parent 1460593 commit a5f0165
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
37 changes: 17 additions & 20 deletions webapp/components/device.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export default function DeviceBar() {
const disableDevice = "none"
const refEnabled = useRef(false)

const [permission, setPermission] = useState("...")
const [permissionAudio, setPermissionAudio] = useState("...")
const [permissionVideo, setPermissionVideo] = useState("...")

Expand Down Expand Up @@ -59,20 +58,6 @@ export default function DeviceBar() {
}
})

const requestPermission = async () => {
try {
permissionsQuery()
const result = await navigator.mediaDevices.getUserMedia({ video: true, audio: true })
result instanceof MediaStream ? setPermission("success") : setPermission("error")
result.getTracks().map(track => track.stop())
//@ts-ignore
} catch ({ name, message }) {
setPermission(message)
} finally {
await permissionsQuery()
}
}

const updateDeviceList = async () => {
const devices = await navigator.mediaDevices.enumerateDevices()
const audios: Device[] = []
Expand All @@ -88,6 +73,22 @@ export default function DeviceBar() {
}
})

if (currentDeviceAudio === disableDevice && currentSelectDeviceAudio === disableDevice) {
let device = audios[0]
if (device) {
setCurrentDeviceAudio(device.deviceId)
setCurrentSelectDeviceAudio(device.deviceId)
}
}

if (currentDeviceVideo === disableDevice && currentSelectDeviceVideo === disableDevice) {
let device = videos[0]
if (device) {
setCurrentDeviceVideo(device.deviceId)
setCurrentSelectDeviceVideo(device.deviceId)
}
}

setDeviceAudio([deviceNone, ...audios])
setDeviceVideo([deviceNone, ...videos, deviceScreen])
}
Expand All @@ -98,7 +99,7 @@ export default function DeviceBar() {
// In some device have problem:
// - Android Web Browser
// - Wechat WebView
await requestPermission()
await permissionsQuery()
} catch { }
await updateDeviceList()
}
Expand Down Expand Up @@ -191,10 +192,6 @@ export default function DeviceBar() {
return (
<div className='flex flex-row flex-wrap justify-around p-xs'>
<center className='basis-full'>
<label className='text-white'>
Your Device Status: <code className={permission === "success" ? "text-green" : "text-red"}>{permission}</code>
</label>

<section className='flex flex-row justify-center text-white'>
<div className='mx-xs'>
Microphone: <code className={permissionAudio === "granted" ? "text-green" : "text-red"}>{permissionAudio}</code>
Expand Down
22 changes: 20 additions & 2 deletions webapp/components/prepare.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import { useAtom } from 'jotai'
import DeviceBar from './device'
import Loading from "./loading"
Expand All @@ -15,10 +15,11 @@ import { asyncGetStreamId } from '../lib/storage'

export default function Prepare(props: { meetingId: string }) {
const [loading, setLoading] = useState<boolean>(false)
const refEnabled = useRef(false)

const [displayName, setDisplayName] = useAtom(displayNameAtom)

const [localStream] = useAtom(localStreamAtom)
const [localStream, setLocalStream] = useAtom(localStreamAtom)
const [localStreamId, setLocalStreamId] = useAtom(localStreamIdAtom)
const [localUserStatus, setLocalUserStatus] = useAtom(localUserStatusAtom)
const [_, setMeetingJoined] = useAtom(meetingJoinedAtom)
Expand All @@ -40,6 +41,23 @@ export default function Prepare(props: { meetingId: string }) {
setMeetingJoined(true)
}

const init = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({ video: { width: 320 }, audio: true })
setLocalStream({
stream: stream,
name: "Me",
})
} catch { }
}

useEffect(() => {
if (!refEnabled.current) {
refEnabled.current = true
init()
}
}, [])

return (
<div className='flex flex-col justify-around'>
<center className='m-xs'>
Expand Down

0 comments on commit a5f0165

Please sign in to comment.