-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start rtsp viewer * Working rtsp stream viewer draft * Remove debug change * Improve quality and add flipped and rotated configs * Update readme * Small fixes * Cleanup code * Fix build
- Loading branch information
Showing
13 changed files
with
236 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,25 @@ | ||
const Stream = require('node-rtsp-stream'); | ||
|
||
// Get node args | ||
const args = process.argv.slice(2); | ||
|
||
// Check if required arguments are present | ||
if (args.length < 2) { | ||
const filename = __filename.split('/').pop(); | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
`Missing Arguments! Usage: node ${filename} <rtsp-url> <websocket-port>` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
new Stream({ | ||
name: 'RTSP-Stream', | ||
streamUrl: args[0], | ||
wsPort: parseInt(args[1]), | ||
ffmpegOptions: { | ||
// options ffmpeg flags | ||
'-r': 30, | ||
'-q': 0, // quality video in scale [0, 32] | ||
}, | ||
}); |
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 |
---|---|---|
|
@@ -12,4 +12,4 @@ module.exports = { | |
packageOptions: { | ||
knownEntrypoints: ['date-fns/fp/format', 'react-is'], | ||
}, | ||
} | ||
}; |
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
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
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
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,45 @@ | ||
import { log } from '@/main/logger'; | ||
import { app, ipcMain } from 'electron'; | ||
import { ExecaChildProcess, execa } from 'execa'; | ||
import path from 'path'; | ||
import { RTSP_START, RTSP_STOP } from './preload'; | ||
import process from 'process'; | ||
|
||
interface RtspProcess { | ||
process: ExecaChildProcess; | ||
wsPort: number; | ||
} | ||
|
||
const rtspServers: Map<number, RtspProcess> = new Map(); | ||
|
||
// Stack array of ports from (9000 to 9060) to use for rtsp servers | ||
const ports = Array.from({ length: 61 }, (_, i) => i + 9000); | ||
|
||
ipcMain.handle(RTSP_START, (_, url: string) => { | ||
const nextPort = ports.shift() ?? 9000; | ||
const rtspProcess = execa('node', [ | ||
!app.isPackaged | ||
? './script/rtspServer.js' | ||
: path.resolve(`${process.resourcesPath}/../script/rtspServer.js`), | ||
url, | ||
nextPort.toString(), | ||
]); | ||
rtspServers.set(nextPort, { | ||
process: rtspProcess, | ||
wsPort: nextPort, | ||
}); | ||
|
||
return nextPort; | ||
}); | ||
|
||
ipcMain.on(RTSP_STOP, (_, port: number) => { | ||
const rtspProcess = rtspServers.get(port); | ||
|
||
if (!rtspProcess) { | ||
return; | ||
} | ||
log.info('stopping rtsp process'); | ||
ports.push(rtspProcess.wsPort); | ||
rtspProcess.process.kill(); | ||
rtspServers.delete(port); | ||
}); |
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
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,71 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import JSMpeg from '@cycjimmy/jsmpeg-player'; | ||
import { log } from '@/renderer/logger'; | ||
|
||
interface Props { | ||
url: string; | ||
flipped: boolean; | ||
rotated: boolean; | ||
} | ||
|
||
export const RTSPFeed = ({ url, flipped, rotated }: Props) => { | ||
const videoRef = useRef<HTMLDivElement>(null); | ||
const canvasRef = useRef<HTMLCanvasElement>(null); | ||
|
||
useEffect(() => { | ||
const videoWrapper = videoRef.current; | ||
const canvas = canvasRef.current; | ||
let videoElement: JSMpeg.VideoElement | null = null; | ||
let port: number | null = null; | ||
const startServer = async () => { | ||
port = (await window.preloadApi.rtsp.start(url)) as number; | ||
|
||
if (videoWrapper && canvas) { | ||
log.info(`RTSP server started on port ${port}`); | ||
videoElement = new JSMpeg.VideoElement( | ||
videoWrapper, | ||
`ws://localhost:${port}`, | ||
{ | ||
canvas, | ||
autoplay: true, | ||
audio: false, | ||
}, | ||
{ videoBufferSize: 2048 * 2048 } | ||
); | ||
} | ||
}; | ||
|
||
startServer().catch((e) => log.error(e)); | ||
|
||
return () => { | ||
if (videoElement && port) { | ||
log.info(`Stopping RTSP server for ${url}`); | ||
window.preloadApi.rtsp.stop(port); | ||
videoElement.destroy(); | ||
} | ||
}; | ||
}, [url]); | ||
|
||
return ( | ||
<div | ||
ref={videoRef} | ||
style={{ | ||
height: '100%', | ||
objectFit: 'contain', | ||
overflow: 'hidden', | ||
transform: `${flipped ? 'scaleX(-1)' : ''} ${ | ||
rotated ? 'rotate(180deg)' : '' | ||
}`, | ||
}} | ||
> | ||
<canvas | ||
ref={canvasRef} | ||
style={{ | ||
height: '100%', | ||
objectFit: 'contain', | ||
overflow: 'hidden', | ||
}} | ||
/> | ||
</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
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,19 @@ | ||
declare module '@cycjimmy/jsmpeg-player' { | ||
class VideoElement { | ||
constructor( | ||
videoWrapper: HTMLDivElement, | ||
url: string, | ||
options: { | ||
canvas?: HTMLCanvasElement; | ||
autoplay?: boolean; | ||
audio?: boolean; | ||
autoSetWrapperSize?: boolean; | ||
}, | ||
overlayOptions?: { | ||
videoBufferSize?: number; | ||
} | ||
); | ||
paused: boolean; | ||
destroy(): void; | ||
} | ||
} |