forked from kedacomresearch/webstreamer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtspanalyzer.js
216 lines (187 loc) · 8.15 KB
/
rtspanalyzer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const IElementWatcher = require('./elementwatcher').IElementWatcher;
const CodecMap = require('./codecmap').TABLE;
let rmdirSync = require('./utils').rmdirSync;
let parseTime = require('./utils').parseTime;
const tesseract = require('node-tesseract');
const Promise = require('bluebird');
const uuid = require('uuid');
const fs = require('fs');
// var WebRTC = require('./webrtc_socket.io.js').WebRTC;
var WebRTC = require('./webrtc.js').WebRTC;
const Tesseract = require('./tesseract.js').Tesseract;
class Analyzer extends IElementWatcher {
constructor(webstreamer, name, url, option) {
super(webstreamer, name);
this.option = option;
this.url = url;
this.audio_passed = 0;
this.video_frame = 10;
this.image_base_number = 5; // invalid image number
this.image_number = 4; // valid number
this.images = [];
this.out_dir = 'img@' + uuid.v4();
this.option.image.location = `${this.out_dir}/%05d.jpg`;
// fs.mkdirSync(this.out_dir);
this.launch = null;
this.tesseract_plugin = new Tesseract();
let self = this;
this.tesseract_plugin.on('image_data', async function (data) {
let obj = JSON.parse(data.toString('utf8'));
let ocr_time = parseTime(obj.ocr);
let ms = obj["stream-time"] / 1000000;
self.images.push({ "time": ocr_time, "ms": ms });
// console.log({ "time": ocr_time, "ms": ms });
});
}
async startup() {
await this.tesseract_plugin.initialize();
return this.call('startup', {
frame: this.video_frame,
launch: this.launch
});
}
async stop() {
await this.tesseract_plugin.terminate();
this.tesseract_plugin = null;
await this.call('stop');
}
clean() {
if (this.out_dir) {
if (fs.existsSync(this.out_dir)) {
rmdirSync(this.out_dir);
}
this.out_dir = null;
}
}
calc_band_number(magnitude) {
let freq = this.option.audio.freq;
let bands = this.option.audio.bands;
let rate = this.option.audio.rate;
let bandwith = (rate / bands) / 2;
let pos = freq / bandwith;
pos = Math.max(pos, 0);
pos = Math.round(pos);
let max = Math.max.apply(null, magnitude);
let mag = magnitude[pos];
if (Math.abs(mag - max) < 5) {
this.audio_passed++;
}
}
store_image(data) {
// if (this.images.length > (this.image_number + this.image_base_number))
// return;
let j = JSON.parse(data.toString('utf8'));
let filename = j.filename;
let time = j["stream-time"] / 1000000;
this.images.push({ "filename": filename, "time": time });
}
async analyze_image() {
// console.log('start analyze_image')
let res = [];
let pre1, pre2;
for (let i = 0; i < this.image_base_number + this.image_number; i++) {
if (i < this.image_base_number)
continue;
// let filename = this.images[i].filename;
let time = this.images[i].time;
let ms = this.images[i].ms;
if (i == this.image_base_number) {
pre1 = time;
pre2 = ms;
} else {
res.push({ 'time': time - pre1, 'ms': ms - pre2 });
pre1 = time;
pre2 = ms;
}
}
console.log(res);
// console.log('end analyze_image')
return res;
}
video_analyze(data, meta) {
return this.tesseract_plugin.call(data, meta);
}
}
class RTSPAnalyzer extends Analyzer {
constructor(webstreamer, name, url, option) {
super(webstreamer, name, url, option);
let video = this.option.video;
let audio = this.option.audio;
let video_codec = CodecMap[video.codec];
let audio_codec = CodecMap[audio.codec];
// this.launch =
// `( rtspsrc location=${this.url} name=r r. `
// + `! queue ! capsfilter caps="application/x-rtp,media=video" `
// // + `! ${video_codec.depay} ! ${video_codec.dec} ! autovideosink sync=false r. `
// + `! ${video_codec.depay} ! ${video_codec.dec} `
// + `! jpegenc ! multifilesink name=image post-messages=TRUE location=${this.option.image.location} r. `
// + `! queue ! capsfilter caps="application/x-rtp,media=audio" ` // audio
// + `! ${audio_codec.depay} ! ${audio_codec.dec} ! audioconvert `
// + `! spectrum name=audio bands=${audio.bands} threshold=${audio.threshold} post-messages=TRUE message-phase=TRUE message-magnitude=TRUE `
// + `! fakesink )`;
// // + `! spectrascope ! autovideosink sync=false )`;
// // this.launch=`rtspsrc location=rtsp://127.0.0.1/test_server ! rtph264depay ! avdec_h264 ! autovideosink sync=false`;
this.launch =
`( rtspsrc location=${this.url} name=r r. `
+ `! queue ! capsfilter caps="application/x-rtp,media=video" `
+ `! ${video_codec.depay} ! ${video_codec.dec} ! fakesink name=sink r. `
+ `! queue ! capsfilter caps="application/x-rtp,media=audio" ` // audio
+ `! ${audio_codec.depay} ! ${audio_codec.dec} ! audioconvert `
+ `! spectrum name=audio bands=${audio.bands} threshold=${audio.threshold} post-messages=TRUE message-phase=TRUE message-magnitude=TRUE `
+ `! fakesink )`;
}
}
class WebRTCAnalyzer extends Analyzer {
constructor(webstreamer, name, signal_bridge, id, option, role = 'answer') {
super(webstreamer, name, null, option);
this.id = id;
this.signal_bridge = signal_bridge;
this.type = 'WebRTCTestClient';
this.role = role;
let video = this.option.video;
let audio = this.option.audio;
let video_codec = CodecMap[video.codec];
let audio_codec = CodecMap[audio.codec];
this.launch =
`( webrtcbin name=webrtc `
+ ` webrtc.! ${video_codec.depay} name=video_payloader ! ${video_codec.dec} ! fakesink name=sink`
// + `! jpegenc ! multifilesink name=image post-messages=TRUE location=${this.option.image.location}`
+ ` webrtc. ! ${audio_codec.depay} ! ${audio_codec.dec} ! audioconvert ! audioresample `
+ `! spectrum name=audio bands=${audio.bands} threshold=${audio.threshold} post-messages=TRUE message-phase=TRUE message-magnitude=TRUE `
+ `! fakesink )`;
// this.launch =
// `( webrtcbin name=webrtc `
// + ` webrtc.! ${video_codec.depay} name=video_payloader ! ${video_codec.dec} `
// + `! autovideosink sync=false`
// + ` webrtc. ! ${audio_codec.depay} ! ${audio_codec.dec} ! audioconvert ! audioresample `
// // + `! autoaudiosink sync=false )`;
// + `! spectrascope ! autovideosink sync=false )`;
// this.launch =
// `( webrtcbin name=webrtc `
// + ` videotestsrc pattern=ball ! x264enc ! rtph264pay ! queue ! application/x-rtp,media=video,payload=96,encoding-name=H264 ! webrtc. `
// + ` audiotestsrc wave=saw ! alawenc ! rtppcmapay ! queue ! application/x-rtp,media=audio,payload=8,encoding-name=PCMA ! webrtc. `
// + ` )`;
}
async startup() {
if (this.role == 'answer') {
this.webrtc = new WebRTC(this.signal_bridge, null, null, this, 'answer', this.id);
} else {
this.webrtc = new WebRTC(this.signal_bridge, this.id, null, this, 'offer', null);
}
this.webrtc.connect();
await this.tesseract_plugin.initialize();
return this.call('startup', {
role: this.role,
launch: this.launch
});
}
async stop() {
this.webrtc.close();
this.webrtc = null;
await super.stop();
}
}
module.exports = {
RTSPAnalyzer,
WebRTCAnalyzer
};