-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TMP-Argus]stream switch via dropdown or monitors
- Loading branch information
1 parent
830012c
commit 3e5f07a
Showing
2 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { JSMpeg } from '../SampleView/jsmpeg.min.js'; | ||
|
||
export function StreamSwitch(props) { | ||
const { handleSourceSwitch } = props; | ||
const cameras = useSelector( | ||
(state) => state.beamline.hardwareObjects.argus?.attributes?.camera_streams, | ||
); | ||
const [players, setPlayers] = useState({}); | ||
const [streammode, setStreammode] = useState('monitors'); | ||
|
||
useEffect(() => { | ||
setPlayers((prevPlayers) => { | ||
if (streammode === 'monitors') { | ||
let new_players = {}; | ||
// remove all JSMpeg players from streams that have been removed | ||
if (prevPlayers) { | ||
new_players = Object.fromEntries( | ||
Object.entries(prevPlayers).filter(([key]) => { | ||
if (!Object.keys(cameras).includes(key)) { | ||
prevPlayers[key].destroy(); | ||
return false; | ||
} | ||
return true; | ||
}), | ||
); | ||
} | ||
|
||
// add players for new streams | ||
Object.keys(cameras).forEach((key) => { | ||
if (prevPlayers && !prevPlayers[key]) { | ||
const curr_canv = document.querySelector(`#${key}-canv`); | ||
const url = `ws://localhost:7000/ws/${key}`; | ||
new_players[key] = new JSMpeg.Player(url, { | ||
canvas: curr_canv, | ||
decodeFirstFrame: true, | ||
preserveDrawingBuffer: false, | ||
protocols: [], | ||
}); | ||
new_players[key].stop(); | ||
curr_canv.src = url; | ||
} | ||
}); | ||
|
||
return new_players; | ||
} | ||
return {}; | ||
}); | ||
}, [cameras, streammode]); | ||
|
||
let result = null; | ||
// in dropdown mode, we only create a dropdown where each option is one stream, | ||
// upon changing the value, we call the handleSourceSwitch function from props | ||
if (streammode === 'dropdown') { | ||
result = ( | ||
<select | ||
title="Camera Streams" | ||
id="streams-dropdown" | ||
onChange={(event) => { | ||
handleSourceSwitch(event.target.value); | ||
}} | ||
> | ||
<option value="">-- Streams --</option> | ||
{Object.entries(cameras).map((stream) => { | ||
const key = stream.slice(0, 1); | ||
const url = `ws://localhost:7000/ws/${stream.slice(0, 1)}`; | ||
return ( | ||
<option key={key} value={url}> | ||
{' '} | ||
{key}{' '} | ||
</option> | ||
); | ||
})} | ||
</select> | ||
); | ||
} // in monitors mode, one small monitor is created per stream. | ||
// clicking changes the source, while hovering plays/stops the monitor stream | ||
else if (streammode === 'monitors') { | ||
result = Object.entries(cameras).map((stream) => { | ||
const key = stream.slice(0, -1)[0]; | ||
const url = `ws://localhost:7000/ws/${stream.slice(0, -1)}`; | ||
return ( | ||
<button | ||
key={`${key}-button`} | ||
onClick={() => handleSourceSwitch(url)} | ||
onMouseEnter={() => { | ||
if (players && players[key]) { | ||
players[key].play(); | ||
} | ||
}} | ||
onMouseLeave={() => { | ||
if (players && players[key]) { | ||
players[key].stop(); | ||
} | ||
}} | ||
type="button" | ||
style={{ | ||
width: '33%', | ||
}} | ||
> | ||
<canvas | ||
id={`${key}-canv`} | ||
aria-label={`Canvas for ${key}`} | ||
style={{ | ||
width: '100%', | ||
height: '180px', | ||
overflow: 'hidden', | ||
}} | ||
/> | ||
{key} | ||
</button> | ||
); | ||
}); | ||
} | ||
return ( | ||
<div style={{ textAlign: 'center' }}> | ||
{result} | ||
<button | ||
type="button" | ||
onClick={() => { | ||
setStreammode(streammode === 'monitors' ? 'dropdown' : 'monitors'); | ||
}} | ||
style={{ display: 'block', margin: '10px auto' }} | ||
> | ||
Switch Stream Mode | ||
</button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters