Skip to content

Commit

Permalink
finish speaker button
Browse files Browse the repository at this point in the history
  • Loading branch information
xdzqyyds committed Nov 20, 2024
1 parent 04610f7 commit 4cde076
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 32 deletions.
3 changes: 1 addition & 2 deletions webapp/components/device.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ export default function DeviceBar(props: { streamId: string }) {
}}>
<center>{ loadingSpeaker ? <Loading/> : <SvgSpeaker/> }</center>
</button>
<div className="flex flex-col justify-between w-1 pointer-events-none">
</div>
<div className="flex flex-col justify-between w-1 pointer-events-none"></div>
<select
className="w-3.5 h-8 rounded-sm rotate-180"
value={currentDeviceSpeaker}
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" />
? <Player stream={presentationStream.stream} muted={true} width="auto" currentDeviceSpeaker={'none'} />
: null
}

Expand Down
8 changes: 6 additions & 2 deletions webapp/components/player/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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 }) {
export default function Player(props: { stream: MediaStream, muted: boolean, audio?: boolean, video?: boolean, width: string, currentDeviceSpeaker: string}) {
const refVideo = useRef<HTMLVideoElement>(null)
const [showAudio, setShowAudio] = useState(false)
const audioTrack = props.stream.getAudioTracks()[0]
Expand All @@ -44,6 +44,10 @@ export default function Player(props: { stream: MediaStream, muted: boolean, aud
if (audioTrack && props.audio) {
const el = document.createElement('audio')
el.srcObject = new MediaStream([audioTrack])

if (el.setSinkId) {
el.setSinkId(props.currentDeviceSpeaker)
}
el.play()

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

useEffect(() => {
if (refVideo.current && videoTrack) {
Expand Down
6 changes: 5 additions & 1 deletion webapp/components/player/whep-player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ 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 @@ -31,7 +35,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} />
<Player stream={stream} muted={false} width={props.width} audio={true} video={props.userStatus.video} currentDeviceSpeaker={currentDeviceSpeaker}/>
<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} />
<Player stream={stream} muted={true} width={props.width} audio={false} video={userStatus.video} currentDeviceSpeaker={'none'}/>
<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, setUserName, setSyncUserStatus, start } = useWhipClient(localStreamId)
const { id, stream, currentDeviceSpeaker,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} />
<Player stream={stream} muted={true} width="320px" audio={true} video={true} currentDeviceSpeaker={currentDeviceSpeaker}/>
</center>

<center className="mb-xs">
Expand Down
23 changes: 0 additions & 23 deletions webapp/components/use/whip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ interface WHIPData extends Data {
}

class WHIPContext extends Context {
private audioElement: HTMLAudioElement
client: WHIPClient = new WHIPClient()
cache: WHIPData

Expand All @@ -38,7 +37,6 @@ class WHIPContext extends Context {

constructor(id: string) {
super(id)
this.audioElement = document.createElement('audio')
this.cache = this.clone()
}

Expand Down Expand Up @@ -111,31 +109,10 @@ class WHIPContext extends Context {
async setCurrentDeviceSpeaker(current: string) {
const { userStatus, currentDeviceSpeaker } = this

// 检查是否需要切换设备
if (current !== currentDeviceSpeaker || !userStatus.speaker) {
if (!this.audioElement) {
// 使用全局或共享的 audioElement
this.audioElement = document.createElement('audio')
document.body.appendChild(this.audioElement)
}

// 设置新的扬声器设备
if (this.audioElement.setSinkId) {
try {
await this.audioElement.setSinkId(current)
console.log(`扬声器切换到设备: ${current}`)
} catch (error) {
console.error('切换音频输出设备失败:', error)
}
} else {
console.error('当前浏览器不支持 setSinkId')
}

// 更新状态
userStatus.speaker = current === deviceNone.deviceId ? false : true
this.currentDeviceSpeaker = current === deviceNone.deviceId ? this.currentDeviceSpeaker : current

// 同步状态并触发相关回调
this.sync()
this.syncUserStatus(userStatus)
}
Expand Down
1 change: 1 addition & 0 deletions webapp/store/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface UserStatus {
*/
name: string
state: StreamState
speaker: boolean
audio: boolean
video: boolean
screen: boolean
Expand Down

0 comments on commit 4cde076

Please sign in to comment.