You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
3.8 KiB
157 lines
3.8 KiB
// electron/electron.js
|
|
const path = require('path');
|
|
const { app, BrowserWindow, ipcMain } = require('electron');
|
|
|
|
const isDev = process.env.IS_DEV == "true" ? true : false;
|
|
|
|
// rtsp相关,ffmpeg-static会自动下载对应平台windos,linux,mac(intle),mac(M1版本)的ffmpeg二进制文件
|
|
const ffmpegPath = require('ffmpeg-static')
|
|
/**
|
|
stream相关配置及方法
|
|
name = options.name
|
|
streamUrl = options.streamUrl
|
|
width = options.width
|
|
height = options.height
|
|
wsPort = options.wsPort
|
|
nputStreamStarted = false
|
|
stream = undefined
|
|
ffmpegPath = options?.ffmpegPath ?? ffmpeg
|
|
stop()
|
|
*/
|
|
const Stream = require('node-rtsp-stream')
|
|
/**
|
|
* rtsp列表
|
|
* interface {
|
|
* rtspUrl: {
|
|
* ws: websocket地址
|
|
* stream: stream实例
|
|
* }
|
|
* }
|
|
*/
|
|
const rtspOpenders = {}
|
|
let addPort = 9000
|
|
|
|
/**
|
|
* 开启rtsp
|
|
* @param rtsp {String} rtsp流地址
|
|
*/
|
|
|
|
|
|
ipcMain.on('openRtsp', (event, rtsp) => {
|
|
/** 判断是否已开启,若已开启,直接返回ws地址, 未开启则先开启再返回 */
|
|
if (rtspOpenders[rtsp]) {
|
|
event.returnValue = {
|
|
code: 200,
|
|
msg: '开启成功',
|
|
ws: rtspOpenders[rtsp].ws
|
|
}
|
|
} else {
|
|
addPort++
|
|
const stream = new Stream({
|
|
name: `socket-${addPort}`,
|
|
streamUrl: rtsp,
|
|
wsPort: addPort,
|
|
ffmpegPath: app.isPackaged ? ffmpegPath.replace('app.asar', 'app.asar.unpacked') : ffmpegPath,
|
|
ffmpegOptions: {
|
|
'-stats': '',
|
|
'-r': 30
|
|
}
|
|
}).on('exitWithError', () => {
|
|
stream.stop()
|
|
delete rtspOpenders[rtsp]
|
|
event.returnValue = {
|
|
code: 400,
|
|
msg: '开启失败'
|
|
}
|
|
})
|
|
rtspOpenders[rtsp] = {
|
|
ws: `ws://localhost:${addPort}`,
|
|
stream: stream
|
|
}
|
|
event.returnValue = {
|
|
code: 200,
|
|
msg: '开启成功',
|
|
ws: rtspOpenders[rtsp].ws
|
|
}
|
|
}
|
|
})
|
|
|
|
/**
|
|
* 关闭rtsp
|
|
*/
|
|
ipcMain.on('closeRtsp', (event, rtsp) => {
|
|
if (rtspOpenders[rtsp]) {
|
|
// 停止解析
|
|
rtspOpenders[rtsp].stream.stop()
|
|
// 删除该项
|
|
delete rtspOpenders[rtsp]
|
|
// 返回结果
|
|
event.returnValue = {
|
|
code: 200,
|
|
msg: '关闭成功'
|
|
}
|
|
} else {
|
|
event.returnValue = {
|
|
code: 400,
|
|
msg: '未找到该rtsp'
|
|
}
|
|
}
|
|
})
|
|
|
|
|
|
///////////////////////////////////////////
|
|
|
|
function createWindow() {
|
|
// Create the browser window.
|
|
const mainWindow = new BrowserWindow({
|
|
width: 800,
|
|
height: 600,
|
|
webPreferences: {
|
|
//preload: path.join(__dirname, 'preload.js'),
|
|
javascript: true,
|
|
plugins: true,
|
|
allowRunningInsecureContent: true,
|
|
// 允许web断使用node
|
|
nodeIntegration: true,
|
|
contextIsolation: false,
|
|
// 同源策略关闭
|
|
webSecurity: false,
|
|
},
|
|
});
|
|
|
|
// and load the index.html of the app.
|
|
// win.loadFile("index.html");
|
|
|
|
mainWindow.loadURL(
|
|
isDev
|
|
? 'http://localhost:3200'
|
|
: `file://${path.join(__dirname, '../dist/index.html')}`
|
|
);
|
|
|
|
// mainWindow.loadURL('http://127.0.0.1:5500');
|
|
// Open the DevTools.
|
|
// if (isDev) {
|
|
mainWindow.webContents.openDevTools();
|
|
// }
|
|
}
|
|
|
|
// This method will be called when Electron has finished
|
|
// initialization and is ready to create browser windows.
|
|
// Some APIs can only be used after this event occurs.
|
|
app.whenReady().then(() => {
|
|
createWindow()
|
|
app.on('activate', function () {
|
|
// On macOS it's common to re-create a window in the app when the
|
|
// dock icon is clicked and there are no other windows open.
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
})
|
|
});
|
|
|
|
// Quit when all windows are closed, except on macOS. There, it's common
|
|
// for applications and their menu bar to stay active until the user quits
|
|
// explicitly with Cmd + Q.
|
|
app.on('window-all-closed', () => {
|
|
if (process.platform !== 'darwin') {
|
|
app.quit();
|
|
}
|
|
});
|
|
|