전체 과정
node-rtsp-stream 삽질 - 1부 |
node-rtsp-stream 삽질 - 2부(개선) |
어제까지도 이걸로 삽질을 많이 했다.
아래 코드의 내용을 설명 하자면..
1. rtspList에 있는것을 반복문 돌면서 각각의 stream을 생성하면서 안에 stream에 저장을 시킴
2. 각 stream 마다 ffmpegStderr을 체크하면서 값이 새로 들어오면 시간을 갱신
3. 각 stream을 1초마다 체크하는 타이머를 만들었으며, 현재시간, stream의 마지막 시간의 초를 비교
4. 5초 이상 차이가 나면 해당 스트림 정지 밎 재시작
오히려 어제보다 코드 자체는 더 깔끔해진듯 하다.
이 방식이 효율적인지 아닌지는 모르겠다. 일단 node, rtsp에 대해서 잘 모르는 상태에서 처리를 하려다 보니깐 , node-rtsp-stream을 쓸 수 밖에 없었고 , 문제가 발생한것을 해결하기 위해 약간 편법과 같은 방법으로 처리가 된듯하다.
더 좋은 방법이 있다면 답글 부탁 드려요~
Stream = require('node-rtsp-stream')
var rtspList = [
{"url":'rtsp url',"port":포트번호, "stream":null,"lastData":null},
{"url":'rtsp url',"port":포트번호, "stream":null,"lastData":null}
];
var rtspListLength = rtspList.length;
for(var i=0; i<rtspListLength; i++){
openStream(rtspList[i]);
var timer = setInterval(function(obj){
var today = new Date();
if(obj.lastData !== undefined){
var stream_date = new Date(obj.lastData);
var gap = (today.getTime() - stream_date.getTime())/1000;
console.log(gap);
if(gap >= 5){//check gap of second
obj.stream.stop();
openStream(obj);
}
}
},1000,rtspList[i]);
}
function openStream(obj){
var stream = new Stream({
name: 'name',
streamUrl : obj.url,
wsPort: obj.port,
ffmpegOptions: { // options ffmpeg flags
'-stats': '', // an option with no neccessary value uses a blank string
'-r': 30, // options with required values specify the value after the key
}
});
obj.stream = stream;
stream.mpeg1Muxer.on('ffmpegStderr', (data)=>{
var today = new Date();
obj.lastData = today
});
}
//2021-07-13 추가 글
위의 소스를 사용하던 중 마음에 들지 않는 부분이 있었고 그중 하나가 소켓을 끊는다는 것이었다. 그래서 아래처럼 소스를 조금 변경했다.
var rtspList = [
{"url":'rtsp url',"port":6055, "stream":null,"lastData":null},
];
//var sendSecond = "";
var rtspListLength = rtspList.length;
for(var i=0; i<rtspListLength; i++){
openStream(rtspList[i]);
var timer = setInterval(function(obj){
var today = new Date();
if(obj.lastData !== undefined){
var stream_date = new Date(obj.lastData);
var gap = (today.getTime() - stream_date.getTime())/1000;
console.log(gap);
if(gap >= 5){//check gap of second
//obj.stream.stop();
//openStream(obj);
obj.lastData = today;
obj.stream = obj.stream.restartStream();
obj.stream.mpeg1Muxer.on('ffmpegStderr', (data)=>{
var today = new Date();
obj.lastData = today;
});
}
}
},1000,rtspList[i]);
}
function openStream(obj){
var stream = new Stream({
name: 'name',
streamUrl : obj.url,
wsPort: obj.port,
ffmpegOptions: { // options ffmpeg flags
'-stats': '', // an option with no neccessary value uses a blank string
'-r': 30, // options with required values specify the value after the key
}
});
obj.stream = stream;
stream.mpeg1Muxer.on('ffmpegStderr', (data)=>{
var today = new Date();
obj.lastData = today;
});
}
stream에서 restartStream이라는 메소드를 추가 생성했다. 위의 함수는 기존 stop메소드와 비슷한 형태를 가지지만 몇가지 기능을 제외 시켰다.
./node_modules/node-rtsp-streap/videoStream.js 에 파일 존재함
//기존 stop
VideoStream.prototype.stop = function() {
this.wsServer.close()
this.stream.kill()
this.inputStreamStarted = false
return this
}
//추가된 메소드
VideoStream.prototype.restartStream = function(){
this.stream.kill()
return this.startMpeg1Stream()
}
기존 stop메소드와 비교하면 wsServer(웹소켓 서버)를 닫는거만 제외를 시켰다고 보면 된다.
현재 ip카메라 한대를 연결해서 사용중인데 5일째 문제 없이 정상 동작 중이다.
'Javascript > Node.js' 카테고리의 다른 글
[Node.js] 버전 관리하기 with nvm (0) | 2023.02.28 |
---|---|
[node.js]puppeteer 설치 오류 (0) | 2023.01.30 |
npm 패키지 추가 중 에러 (0) | 2022.06.20 |
nvm 설치 오류 해결 with scoop (0) | 2022.01.01 |
npm node-rtsp-stream (3) | 2021.07.06 |
댓글