2023-06-22 16:43:33 +10:00
|
|
|
import { MouseEventHandler, useEffect, useRef } from 'react'
|
2023-03-06 20:13:34 +11:00
|
|
|
import { PanelHeader } from '../components/PanelHeader'
|
2023-06-21 09:15:02 +10:00
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
2023-06-22 16:43:33 +10:00
|
|
|
import { useStore } from '../useStore'
|
|
|
|
import { throttle } from '../lib/utils'
|
2023-03-06 20:13:34 +11:00
|
|
|
|
|
|
|
export const Stream = () => {
|
|
|
|
const videoRef = useRef<HTMLVideoElement>(null)
|
2023-06-22 16:43:33 +10:00
|
|
|
const cmdId = useRef('')
|
|
|
|
const { mediaStream, engineCommandManager } = useStore((s) => ({
|
|
|
|
mediaStream: s.mediaStream,
|
|
|
|
engineCommandManager: s.engineCommandManager,
|
|
|
|
}))
|
2023-03-06 20:13:34 +11:00
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-07 15:45:59 +11:00
|
|
|
if (
|
|
|
|
typeof window === 'undefined' ||
|
|
|
|
typeof RTCPeerConnection === 'undefined'
|
|
|
|
)
|
|
|
|
return
|
2023-06-22 16:43:33 +10:00
|
|
|
if (!videoRef.current) return
|
|
|
|
if (!mediaStream) return
|
|
|
|
videoRef.current.srcObject = mediaStream
|
|
|
|
}, [mediaStream, engineCommandManager])
|
|
|
|
|
|
|
|
const file_id = uuidv4()
|
|
|
|
|
|
|
|
const debounceSocketSend = throttle((message) => {
|
|
|
|
engineCommandManager?.sendSceneCommand(message)
|
|
|
|
}, 100)
|
|
|
|
const handleMouseMove: MouseEventHandler<HTMLVideoElement> = ({
|
|
|
|
clientX,
|
|
|
|
clientY,
|
|
|
|
}) => {
|
|
|
|
if (!videoRef.current) return
|
|
|
|
if (!cmdId.current) return
|
|
|
|
const { left, top } = videoRef.current.getBoundingClientRect()
|
|
|
|
const x = clientX - left
|
|
|
|
const y = clientY - top
|
|
|
|
debounceSocketSend({
|
|
|
|
type: 'ModelingCmdReq',
|
|
|
|
cmd: {
|
|
|
|
CameraDragMove: {
|
|
|
|
interaction: 'rotate',
|
|
|
|
window: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
file_id: file_id,
|
2023-03-06 20:13:34 +11:00
|
|
|
})
|
2023-06-22 16:43:33 +10:00
|
|
|
}
|
2023-03-06 20:13:34 +11:00
|
|
|
|
2023-06-22 16:43:33 +10:00
|
|
|
const handleMouseDown: MouseEventHandler<HTMLVideoElement> = ({
|
|
|
|
clientX,
|
|
|
|
clientY,
|
|
|
|
}) => {
|
|
|
|
if (!videoRef.current) return
|
|
|
|
const { left, top } = videoRef.current.getBoundingClientRect()
|
|
|
|
const x = clientX - left
|
|
|
|
const y = clientY - top
|
|
|
|
console.log('click', x, y)
|
|
|
|
|
|
|
|
const newId = uuidv4()
|
|
|
|
cmdId.current = newId
|
|
|
|
|
|
|
|
engineCommandManager?.sendSceneCommand({
|
|
|
|
type: 'ModelingCmdReq',
|
|
|
|
cmd: {
|
|
|
|
CameraDragStart: {
|
|
|
|
interaction: 'rotate',
|
|
|
|
window: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
2023-06-21 09:15:02 +10:00
|
|
|
},
|
2023-06-22 16:43:33 +10:00
|
|
|
},
|
|
|
|
},
|
|
|
|
cmd_id: newId,
|
|
|
|
file_id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const handleMouseUp: MouseEventHandler<HTMLVideoElement> = ({
|
|
|
|
clientX,
|
|
|
|
clientY,
|
|
|
|
}) => {
|
|
|
|
if (!videoRef.current) return
|
|
|
|
const { left, top } = videoRef.current.getBoundingClientRect()
|
|
|
|
const x = clientX - left
|
|
|
|
const y = clientY - top
|
|
|
|
|
|
|
|
if (cmdId.current == null) {
|
|
|
|
return
|
2023-06-21 09:15:02 +10:00
|
|
|
}
|
|
|
|
|
2023-06-22 16:43:33 +10:00
|
|
|
engineCommandManager?.sendSceneCommand({
|
|
|
|
type: 'ModelingCmdReq',
|
|
|
|
cmd: {
|
|
|
|
CameraDragEnd: {
|
|
|
|
interaction: 'rotate',
|
|
|
|
window: {
|
|
|
|
x: x,
|
|
|
|
y: y,
|
2023-06-21 09:15:02 +10:00
|
|
|
},
|
|
|
|
},
|
2023-06-22 16:43:33 +10:00
|
|
|
},
|
|
|
|
cmd_id: uuidv4(),
|
|
|
|
file_id: file_id,
|
|
|
|
})
|
|
|
|
cmdId.current = ''
|
|
|
|
}
|
2023-03-06 20:13:34 +11:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<PanelHeader title="Stream" />
|
2023-06-22 16:43:33 +10:00
|
|
|
<video
|
|
|
|
ref={videoRef}
|
|
|
|
muted
|
|
|
|
autoPlay
|
|
|
|
controls={false}
|
|
|
|
onMouseMove={handleMouseMove}
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
onMouseUp={handleMouseUp}
|
|
|
|
/>
|
2023-03-06 20:13:34 +11:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|