forked from indyblue/android_remote_control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminicap.js
226 lines (201 loc) · 5.57 KB
/
minicap.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
217
218
219
220
221
222
223
224
225
226
'option strict';
const { execSocket, execSync, bindir, libdir, info,
closeSocket, killProc, wsSendBin, wsSendObj } = require('./mini0'),
ptool = require('path'),
net = require('net');
const name = 'minicap';
const exp = module.exports = {
port: 1717,
adir: '/data/local/tmp/' + name,
debug: false,
superDebug: false
};
/**************************************************************************** */
// child process stuff
/**************************************************************************** */
exp.onExit = () => {
execSync(`adb shell -x killall ${name}`);
execSync(`adb shell rm -rf ${exp.adir}`);
execSync(`adb shell -x ls ${exp.adir}`);
};
exp.onExit();
exp.init = () => {
let out = execSync(`adb shell mkdir ${exp.adir}`);
// android 10 prebuilts available in comment here:
// https://github.com/openstf/minicap/pull/190
const bdir = 'minicap-prebuilt/prebuilt';
let dir = bindir(bdir);
out = execSync(`adb push ${dir}/${name} ${exp.adir}/`);
dir = libdir(bdir);
out = execSync(`adb push ${dir}/${name}.so ${exp.adir}/`);
out = execSync(`adb shell -x chmod +x ${exp.adir}/${name}`);
};
exp.init();
exp.proc = null;
exp.start = (force) => {
if(!force && exp.proc) return;
let w = info.w, h = info.h, r = info.rot,
w2 = Math.round(w / 2), h2 = Math.round(h / 2);
let args = `-P ${w}x${h}@${w2}x${h2}/${r} -S`;
cbDataInit();
exp.proc = execSocket(`adb shell -x LD_LIBRARY_PATH=${exp.adir}/ ${exp.adir}/${name} ${args}`,
exp.port, name, cbData);
exp.proc.cbSocketStatus = x => {
wsSendObj({ type: 'minicapStatus', state: x });
};
exp.sendLastFrame();
};
exp.stop = () => {
if (exp.proc) {
closeSocket(exp.proc.socket);
killProc(exp.proc);
exp.proc = null;
}
execSync(`adb shell -x killall ${name}`);
};
exp.restart = () => {
exp.stop();
setTimeout(exp.start, 100);
};
exp.toggle = on => {
if (typeof on === 'boolean') {
if (on) exp.start();
else exp.stop();
} else {
if (exp.proc) exp.stop();
else exp.start();
}
};
let readBannerBytes, bannerLength, readFrameBytes, frameBodyLength,
frameBody, banner;
function cbDataInit() {
readBannerBytes = 0,
bannerLength = 2,
readFrameBytes = 0,
frameBodyLength = 0,
frameBody = new Buffer(0),
banner = {
version: 0
, length: 0
, pid: 0
, realWidth: 0
, realHeight: 0
, virtualWidth: 0
, virtualHeight: 0
, orientation: 0
, quirks: 0
};
}
cbDataInit();
let lastFrame = null;
exp.sendLastFrame = () => {
if (lastFrame) {
console.log('sending initial frame', lastFrame.length);
wsSendBin(lastFrame);
}
};
function cbData(chunk) {
for (var cursor = 0, len = chunk.length; cursor < len;) {
if (readBannerBytes < bannerLength) {
switch (readBannerBytes) {
case 0:
// version
banner.version = chunk[cursor]
break
case 1:
// length
banner.length = bannerLength = chunk[cursor]
break
case 2:
case 3:
case 4:
case 5:
// pid
banner.pid +=
(chunk[cursor] << ((readBannerBytes - 2) * 8)) >>> 0
break
case 6:
case 7:
case 8:
case 9:
// real width
banner.realWidth +=
(chunk[cursor] << ((readBannerBytes - 6) * 8)) >>> 0
break
case 10:
case 11:
case 12:
case 13:
// real height
banner.realHeight +=
(chunk[cursor] << ((readBannerBytes - 10) * 8)) >>> 0
break
case 14:
case 15:
case 16:
case 17:
// virtual width
banner.virtualWidth +=
(chunk[cursor] << ((readBannerBytes - 14) * 8)) >>> 0
break
case 18:
case 19:
case 20:
case 21:
// virtual height
banner.virtualHeight +=
(chunk[cursor] << ((readBannerBytes - 18) * 8)) >>> 0
break
case 22:
// orientation
banner.orientation += chunk[cursor] * 90
break
case 23:
// quirks
banner.quirks = chunk[cursor]
break
}
cursor += 1
readBannerBytes += 1
if (readBannerBytes === bannerLength) {
if (exp.debug) console.log('banner', banner)
}
}
else if (readFrameBytes < 4) {
frameBodyLength += (chunk[cursor] << (readFrameBytes * 8)) >>> 0
cursor += 1
readFrameBytes += 1
if (exp.superDebug) console.info('headerbyte%d(val=%d)', readFrameBytes, frameBodyLength)
}
else {
if (len - cursor >= frameBodyLength) {
if (exp.debug) console.info('bodyfin(len=%d,cursor=%d)', frameBodyLength, cursor)
frameBody = Buffer.concat([
frameBody
, chunk.slice(cursor, cursor + frameBodyLength)
])
// Sanity check for JPG header, only here for debugging purposes.
if (frameBody[0] !== 0xFF || frameBody[1] !== 0xD8) {
console.error(
'Frame body does not start with JPG header', frameBody)
process.exit(1)
}
lastFrame = frameBody;
wsSendBin(frameBody);
cursor += frameBodyLength
frameBodyLength = readFrameBytes = 0
frameBody = new Buffer(0)
}
else {
if (exp.superDebug) console.info('body(len=%d)', len - cursor)
frameBody = Buffer.concat([
frameBody
, chunk.slice(cursor, len)
])
frameBodyLength -= len - cursor
readFrameBytes += len - cursor
cursor = len
}
}
}
}