Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
GLDuval committed May 7, 2023
1 parent 6bd0747 commit 06aeb38
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
2 changes: 1 addition & 1 deletion script/rtspServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ new Stream({
wsPort: parseInt(args[1]),
ffmpegOptions: {
// options ffmpeg flags
'-re': '', // read input at native frame rate
'-r': 30,
'-q': 0, // quality video in scale [0, 32]
},
});
47 changes: 20 additions & 27 deletions src/renderer/components/Feed/Feeds/RTSPFeed.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from 'react';
import React, { useEffect, useRef } from 'react';
import JSMpeg from '@cycjimmy/jsmpeg-player';
import { log } from '@/renderer/logger';

Expand All @@ -11,47 +11,40 @@ interface Props {
export const RTSPFeed = ({ url, flipped, rotated }: Props) => {
const videoRef = useRef<HTMLDivElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const [port, setPort] = useState<number | null>(null);
const [videoElement, setVideoElement] =
useState<JSMpeg.VideoElement | null>();

useEffect(() => {
const videoWrapper = videoRef.current;
const canvas = canvasRef.current;
let videoElement: JSMpeg.VideoElement | null = null;
let port: number | null = null;
const startServer = async () => {
const port = (await window.preloadApi.rtsp.start(url)) as number;
log.info(`RTSP server started on port ${port}`);
port = (await window.preloadApi.rtsp.start(url)) as number;

if (videoWrapper && canvas) {
setVideoElement(
new JSMpeg.VideoElement(
videoWrapper,
`ws://localhost:${port}`,
{
canvas,
autoplay: true,
audio: false,
},
{ videoBufferSize: 2048 * 2048 }
)
log.info(`RTSP server started on port ${port}`);
videoElement = new JSMpeg.VideoElement(
videoWrapper,
`ws://localhost:${port}`,
{
canvas,
autoplay: true,
audio: false,
},
{ videoBufferSize: 2048 * 2048 }
);
setPort(port);
}
};
if (!port && !videoElement) {
startServer().catch((e) => log.error(e));
}

startServer().catch((e) => log.error(e));

return () => {
if (port) {
if (videoElement && port) {
log.info(`Stopping RTSP server for ${url}`);
window.preloadApi.rtsp.stop(port);
setPort(null);
if (videoElement) {
videoElement.destroy();
}
videoElement.destroy();
}
};
}, [port, url, videoElement]);
}, [url]);

return (
<div
Expand Down

0 comments on commit 06aeb38

Please sign in to comment.