Skip to content

Commit

Permalink
use atom to finish speaker button
Browse files Browse the repository at this point in the history
  • Loading branch information
xdzqyyds committed Nov 21, 2024
1 parent 4cde076 commit fe9f09f
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 37 deletions.
12 changes: 7 additions & 5 deletions webapp/components/device.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import useWhipClient from './use/whip'
import { useAtom } from 'jotai'
import { useEffect, useState } from 'react'
import {
Device,
deviceNone,
deviceScreen,
} from '../lib/device'
import { deviceSpeakerAtom } from './../store/atom'

import Loading from './svg/loading'
import SvgSpeaker from './svg/speaker'
Expand All @@ -30,12 +32,12 @@ export default function DeviceBar(props: { streamId: string }) {
const [loadingVideo, setLoadingVideo] = useState(false)
const [loadingScreen, setLoadingScreen] = useState(false)

const [currentDeviceSpeaker, setCurrentDeviceSpeaker] = useAtom(deviceSpeakerAtom)

const {
userStatus,
currentDeviceSpeaker,
currentDeviceAudio,
currentDeviceVideo,
setCurrentDeviceSpeaker,
setCurrentDeviceAudio,
setCurrentDeviceVideo,
toggleEnableAudio,
Expand Down Expand Up @@ -91,9 +93,9 @@ export default function DeviceBar(props: { streamId: string }) {
const audios = devices.filter(i => i.kind === 'audioinput').map(toDevice)
const videos = devices.filter(i => i.kind === 'videoinput').map(toDevice)

if ( currentDeviceSpeaker === deviceNone.deviceId) {
if (currentDeviceSpeaker === deviceNone.deviceId) {
const device = speakers[0]
if (device) await setCurrentDeviceSpeaker(device.deviceId)
if (device) setCurrentDeviceSpeaker(device.deviceId)
}

Check warning on line 99 in webapp/components/device.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (macos-latest)

Trailing spaces not allowed

Check warning on line 99 in webapp/components/device.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (ubuntu-latest)

Trailing spaces not allowed

Check warning on line 99 in webapp/components/device.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (windows-latest)

Trailing spaces not allowed

if (currentDeviceAudio === deviceNone.deviceId) {
Expand Down Expand Up @@ -144,7 +146,7 @@ export default function DeviceBar(props: { streamId: string }) {

const onChangedDeviceSpeaker = async (current: string) => {
setLoadingSpeaker(true)
await setCurrentDeviceSpeaker(current)
setCurrentDeviceSpeaker(current)
setLoadingSpeaker(false)
}

Expand Down
2 changes: 1 addition & 1 deletion webapp/components/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default function Layout(props: { meetingId: string }) {
<div></div>

{enabledPresentation
? <Player stream={presentationStream.stream} muted={true} width="auto" currentDeviceSpeaker={'none'} />
? <Player stream={presentationStream.stream} muted={true} width="auto" />
: null
}

Expand Down
10 changes: 7 additions & 3 deletions webapp/components/player/player.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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 } from '../../lib/util'
import SvgProgress from '../svg/progress'
import { deviceSpeakerAtom } from '../../store/atom'

function AudioWave(props: { stream: MediaStream }) {
const refWave = useRef<HTMLDivElement>(null)
Expand All @@ -29,11 +31,13 @@ function AudioWave(props: { stream: MediaStream }) {
return <div ref={refWave}></div>
}

export default function Player(props: { stream: MediaStream, muted: boolean, audio?: boolean, video?: boolean, width: string, currentDeviceSpeaker: string}) {
export default function Player(props: { stream: MediaStream, muted: boolean, audio?: boolean, video?: boolean, width: string }) {
const refVideo = useRef<HTMLVideoElement>(null)
const [showAudio, setShowAudio] = useState(false)
const audioTrack = props.stream.getAudioTracks()[0]
const videoTrack = props.stream.getVideoTracks()[0]
const [currentDeviceSpeaker] = useAtom(deviceSpeakerAtom)


useEffect(() => {
if (audioTrack && !videoTrack) {
Expand All @@ -46,7 +50,7 @@ export default function Player(props: { stream: MediaStream, muted: boolean, aud
el.srcObject = new MediaStream([audioTrack])

Check warning on line 51 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (macos-latest)

Trailing spaces not allowed

Check warning on line 51 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (ubuntu-latest)

Trailing spaces not allowed

Check warning on line 51 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (windows-latest)

Trailing spaces not allowed
if (el.setSinkId) {
el.setSinkId(props.currentDeviceSpeaker)
el.setSinkId(currentDeviceSpeaker)

Check warning on line 53 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (macos-latest)

Trailing spaces not allowed

Check warning on line 53 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (ubuntu-latest)

Trailing spaces not allowed

Check warning on line 53 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (windows-latest)

Trailing spaces not allowed
}

Check warning on line 54 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (macos-latest)

Trailing spaces not allowed

Check warning on line 54 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (ubuntu-latest)

Trailing spaces not allowed

Check warning on line 54 in webapp/components/player/player.tsx

View workflow job for this annotation

GitHub Actions / Deploy proxy (windows-latest)

Trailing spaces not allowed
el.play()

Expand All @@ -56,7 +60,7 @@ export default function Player(props: { stream: MediaStream, muted: boolean, aud
el.remove()
}
}
}, [audioTrack, videoTrack, props.currentDeviceSpeaker])
}, [audioTrack, videoTrack, currentDeviceSpeaker])

useEffect(() => {
if (refVideo.current && videoTrack) {
Expand Down
6 changes: 1 addition & 5 deletions webapp/components/player/whep-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import Detail from './detail'
import Player from './player'
import { presentationStreamAtom } from '../../store/atom'
import { Stream } from '../../lib/api'
import useWhipClient from '../use/whip'
import {getStorageStream } from '../../lib/storage'

export default function WhepPlayer(props: { streamId: string, userStatus: Stream, width: string }) {
const refEnabled = useRef(false)
const localStreamId = getStorageStream()
const { currentDeviceSpeaker} = useWhipClient(localStreamId)
const { stream, restart, start, connStatus, setRemoteStatus } = useWhepClient(props.streamId)
const [, setPresentationStream] = useAtom(presentationStreamAtom)

Expand All @@ -35,7 +31,7 @@ export default function WhepPlayer(props: { streamId: string, userStatus: Stream

return (
<center className="flex flex-col">
<Player stream={stream} muted={false} width={props.width} audio={true} video={props.userStatus.video} currentDeviceSpeaker={currentDeviceSpeaker}/>
<Player stream={stream} muted={false} width={props.width} audio={true} video={props.userStatus.video} />
<Detail streamId={props.streamId} connStatus={connStatus} userStatus={props.userStatus} restart={restart} />
</center>
)
Expand Down
2 changes: 1 addition & 1 deletion webapp/components/player/whip-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function WhipPlayer(props: { streamId: string, width: string }) {

return (
<center className="flex flex-col">
<Player stream={stream} muted={true} width={props.width} audio={false} video={userStatus.video} currentDeviceSpeaker={'none'}/>
<Player stream={stream} muted={true} width={props.width} audio={false} video={userStatus.video} />
<Detail streamId={props.streamId} connStatus={userStatus.state} userStatus={userStatus} restart={restart} />
</center>
)
Expand Down
4 changes: 2 additions & 2 deletions webapp/components/prepare.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function Prepare(_props: { meetingId: string }) {
const localStreamId = getStorageStream()
const [_, setMeetingJoined] = useAtom(meetingJoinedAtom)

const { id, stream, currentDeviceSpeaker,setUserName, setSyncUserStatus, start } = useWhipClient(localStreamId)
const { id, stream, setUserName, setSyncUserStatus, start } = useWhipClient(localStreamId)

const join = async () => {
setLoading(true)
Expand All @@ -39,7 +39,7 @@ export default function Prepare(_props: { meetingId: string }) {
return (
<div className="flex flex-col justify-around">
<center className="m-xs">
<Player stream={stream} muted={true} width="320px" audio={true} video={true} currentDeviceSpeaker={currentDeviceSpeaker}/>
<Player stream={stream} muted={true} width="320px" audio={true} video={true} />
</center>

<center className="mb-xs">
Expand Down
20 changes: 0 additions & 20 deletions webapp/components/use/whip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ interface WHIPData extends Data {
setUserName: (name: string) => void,
setSyncUserStatus: (callback: (userStatus: Stream) => void) => void,

currentDeviceSpeaker: string,
currentDeviceAudio: string,
currentDeviceVideo: string,
setCurrentDeviceSpeaker: (current: string) => Promise<void>,
setCurrentDeviceAudio: (current: string) => Promise<void>,
setCurrentDeviceVideo: (current: string) => Promise<void>,
toggleEnableSpeaker: () => Promise<void>,
toggleEnableAudio: () => Promise<void>,
toggleEnableVideo: () => Promise<void>,
}
Expand All @@ -28,10 +25,8 @@ class WHIPContext extends Context {
client: WHIPClient = new WHIPClient()
cache: WHIPData

currentDeviceSpeaker = deviceNone.deviceId
currentDeviceAudio = deviceNone.deviceId
currentDeviceVideo = deviceNone.deviceId
toggleEnableSpeaker = async () => this.setCurrentDeviceSpeaker(this.userStatus.speaker ? deviceNone.deviceId : this.currentDeviceSpeaker)
toggleEnableAudio = async () => this.setCurrentDeviceAudio(this.userStatus.audio ? deviceNone.deviceId : this.currentDeviceAudio)
toggleEnableVideo = async () => this.setCurrentDeviceVideo(this.userStatus.video ? deviceNone.deviceId : this.currentDeviceVideo)

Expand Down Expand Up @@ -69,13 +64,10 @@ class WHIPContext extends Context {
setUserName: (name: string) => this.setUserName(name),
setSyncUserStatus: (callback: (userStatus: Stream) => void) => this.setSyncUserStatus(callback),

currentDeviceSpeaker: this.currentDeviceSpeaker,
currentDeviceAudio: this.currentDeviceAudio,
currentDeviceVideo: this.currentDeviceVideo,
setCurrentDeviceSpeaker: (current: string) => this.setCurrentDeviceSpeaker(current),
setCurrentDeviceAudio: (current: string) => this.setCurrentDeviceAudio(current),
setCurrentDeviceVideo: (current: string) => this.setCurrentDeviceVideo(current),
toggleEnableSpeaker: () => this.toggleEnableSpeaker(),
toggleEnableAudio: () => this.toggleEnableAudio(),
toggleEnableVideo: () => this.toggleEnableVideo(),
}
Expand Down Expand Up @@ -106,18 +98,6 @@ class WHIPContext extends Context {
}
}

async setCurrentDeviceSpeaker(current: string) {
const { userStatus, currentDeviceSpeaker } = this

if (current !== currentDeviceSpeaker || !userStatus.speaker) {
userStatus.speaker = current === deviceNone.deviceId ? false : true
this.currentDeviceSpeaker = current === deviceNone.deviceId ? this.currentDeviceSpeaker : current

this.sync()
this.syncUserStatus(userStatus)
}
}

onChangedDeviceAudio() {
const { pc, stream } = this
// If WebRTC is connected, switch track
Expand Down
3 changes: 3 additions & 0 deletions webapp/store/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ presentationStreamAtom.debugLabel = 'presentationStream'
const enabledPresentationAtom = atom(get => get(presentationStreamAtom).stream.getVideoTracks().length !== 0)
enabledPresentationAtom.debugLabel = 'enabledPresentation'

const deviceSpeakerAtom = atom<string>('default')

export {
locationAtom,
presentationStreamAtom,

meetingIdAtom,
meetingJoinedAtom,
enabledPresentationAtom,
deviceSpeakerAtom,
}

export type {
Expand Down

0 comments on commit fe9f09f

Please sign in to comment.