We are showing a mjpeg video stream using the html img tag. For this we have multiple implementations depending on the provider of the stream.
Streaming video is working fine. Switching streams is fine. But switching to a stream of a different type is not fine. At this point the current stream is not closed and a second stream is opened. After several such switches no more http requests can be made and the browser tab freezes.
I am able to reproduce it with this code:
interface StreamProps {
mjpegUrl: string;
}
export const StreamType1: React.FC<StreamProps> = ({ mjpegUrl }) => {
return <img src={mjpegUrl} />;
};
export const StreamType2: React.FC<StreamProps> = ({ mjpegUrl }) => {
return <img src={mjpegUrl} />;
};
interface Props {
}
export const VideoStream: React.FC<Props> = () => {
const [toggle, setToggle] = useState(true);
return (
<div>
<button onClick={() => setToggle((current) => !current)}>
Switch!
</button>
{toggle
? <StreamType1 mjpegUrl="http://localhost/stream1" />
: <StreamType2 mjpegUrl="http://localhost/stream2" />}
</div>
);
};
One thing to note is that if I were to use the same stream type for both this problem does not occur. Similar as to updating the mjpegUrl
property.
{toggle
? <StreamType1 mjpegUrl="http://localhost/stream1" />
: <StreamType1 mjpegUrl="http://localhost/stream2" />}
Why is this happening and how can it be fixed?