-
Notifications
You must be signed in to change notification settings - Fork 340
/
GoogMoreBox.ts
317 lines (290 loc) · 13.9 KB
/
GoogMoreBox.ts
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import '../../../style/morebox.css';
import { BasePlayer } from '../../player/BasePlayer';
import { TextControlMessage } from '../../controlMessage/TextControlMessage';
import { CommandControlMessage } from '../../controlMessage/CommandControlMessage';
import { ControlMessage } from '../../controlMessage/ControlMessage';
import Size from '../../Size';
import DeviceMessage from '../DeviceMessage';
import VideoSettings from '../../VideoSettings';
import { StreamClientScrcpy } from '../client/StreamClientScrcpy';
const TAG = '[GoogMoreBox]';
export class GoogMoreBox {
private static defaultSize = new Size(480, 480);
private onStop?: () => void;
private readonly holder: HTMLElement;
private readonly input: HTMLTextAreaElement;
private readonly bitrateInput?: HTMLInputElement;
private readonly maxFpsInput?: HTMLInputElement;
private readonly iFrameIntervalInput?: HTMLInputElement;
private readonly maxWidthInput?: HTMLInputElement;
private readonly maxHeightInput?: HTMLInputElement;
constructor(udid: string, private player: BasePlayer, private client: StreamClientScrcpy) {
const playerName = player.getName();
const videoSettings = player.getVideoSettings();
const { displayId } = videoSettings;
const preferredSettings = player.getPreferredVideoSetting();
const moreBox = document.createElement('div');
moreBox.className = 'more-box';
const nameBox = document.createElement('p');
nameBox.innerText = `${udid} (${playerName})`;
nameBox.className = 'text-with-shadow';
moreBox.appendChild(nameBox);
const input = (this.input = document.createElement('textarea'));
input.classList.add('text-area');
const sendButton = document.createElement('button');
sendButton.innerText = 'Send as keys';
const inputWrapper = GoogMoreBox.wrap('p', [input, sendButton], moreBox);
sendButton.onclick = () => {
if (input.value) {
client.sendMessage(new TextControlMessage(input.value));
}
};
const commands: HTMLElement[] = [];
const codes = CommandControlMessage.Commands;
for (const [action, command] of codes.entries()) {
const btn = document.createElement('button');
let bitrateInput: HTMLInputElement;
let maxFpsInput: HTMLInputElement;
let iFrameIntervalInput: HTMLInputElement;
let maxWidthInput: HTMLInputElement;
let maxHeightInput: HTMLInputElement;
if (action === ControlMessage.TYPE_CHANGE_STREAM_PARAMETERS) {
const spoiler = document.createElement('div');
const spoilerLabel = document.createElement('label');
const spoilerCheck = document.createElement('input');
const innerDiv = document.createElement('div');
const id = `spoiler_video_${udid}_${playerName}_${displayId}_${action}`;
spoiler.className = 'spoiler';
spoilerCheck.type = 'checkbox';
spoilerCheck.id = id;
spoilerLabel.htmlFor = id;
spoilerLabel.innerText = command;
innerDiv.className = 'box';
spoiler.appendChild(spoilerCheck);
spoiler.appendChild(spoilerLabel);
spoiler.appendChild(innerDiv);
const bitrateLabel = document.createElement('label');
bitrateLabel.innerText = 'Bitrate:';
bitrateInput = document.createElement('input');
bitrateInput.placeholder = `${preferredSettings.bitrate} bps`;
bitrateInput.value = videoSettings.bitrate.toString();
GoogMoreBox.wrap('div', [bitrateLabel, bitrateInput], innerDiv);
this.bitrateInput = bitrateInput;
const maxFpsLabel = document.createElement('label');
maxFpsLabel.innerText = 'Max fps:';
maxFpsInput = document.createElement('input');
maxFpsInput.placeholder = `${preferredSettings.maxFps} fps`;
maxFpsInput.value = videoSettings.maxFps.toString();
GoogMoreBox.wrap('div', [maxFpsLabel, maxFpsInput], innerDiv);
this.maxFpsInput = maxFpsInput;
const iFrameIntervalLabel = document.createElement('label');
iFrameIntervalLabel.innerText = 'I-Frame Interval:';
iFrameIntervalInput = document.createElement('input');
iFrameIntervalInput.placeholder = `${preferredSettings.iFrameInterval} seconds`;
iFrameIntervalInput.value = videoSettings.iFrameInterval.toString();
GoogMoreBox.wrap('div', [iFrameIntervalLabel, iFrameIntervalInput], innerDiv);
this.iFrameIntervalInput = iFrameIntervalInput;
const { width, height } = videoSettings.bounds || client.getMaxSize() || GoogMoreBox.defaultSize;
const pWidth = preferredSettings.bounds?.width || width;
const pHeight = preferredSettings.bounds?.height || height;
const maxWidthLabel = document.createElement('label');
maxWidthLabel.innerText = 'Max width:';
maxWidthInput = document.createElement('input');
maxWidthInput.placeholder = `${pWidth} px`;
maxWidthInput.value = width.toString();
GoogMoreBox.wrap('div', [maxWidthLabel, maxWidthInput], innerDiv);
this.maxWidthInput = maxWidthInput;
const maxHeightLabel = document.createElement('label');
maxHeightLabel.innerText = 'Max height:';
maxHeightInput = document.createElement('input');
maxHeightInput.placeholder = `${pHeight} px`;
maxHeightInput.value = height.toString();
GoogMoreBox.wrap('div', [maxHeightLabel, maxHeightInput], innerDiv);
this.maxHeightInput = maxHeightInput;
innerDiv.appendChild(btn);
const fitButton = document.createElement('button');
fitButton.innerText = 'Fit';
fitButton.onclick = this.fit;
innerDiv.insertBefore(fitButton, innerDiv.firstChild);
const resetButton = document.createElement('button');
resetButton.innerText = 'Reset';
resetButton.onclick = this.reset;
innerDiv.insertBefore(resetButton, innerDiv.firstChild);
commands.push(spoiler);
} else {
if (
action === CommandControlMessage.TYPE_SET_CLIPBOARD ||
action === CommandControlMessage.TYPE_GET_CLIPBOARD
) {
inputWrapper.appendChild(btn);
} else {
commands.push(btn);
}
}
btn.innerText = command;
if (action === ControlMessage.TYPE_CHANGE_STREAM_PARAMETERS) {
btn.onclick = () => {
const bitrate = parseInt(bitrateInput.value, 10);
const maxFps = parseInt(maxFpsInput.value, 10);
const iFrameInterval = parseInt(iFrameIntervalInput.value, 10);
if (isNaN(bitrate) || isNaN(maxFps)) {
return;
}
const width = parseInt(maxWidthInput.value, 10) & ~15;
const height = parseInt(maxHeightInput.value, 10) & ~15;
const bounds = new Size(width, height);
const current = player.getVideoSettings();
const { lockedVideoOrientation, sendFrameMeta, displayId, codecOptions, encoderName } = current;
const videoSettings = new VideoSettings({
bounds,
bitrate,
maxFps,
iFrameInterval,
lockedVideoOrientation,
sendFrameMeta,
displayId,
codecOptions,
encoderName,
});
client.sendNewVideoSetting(videoSettings);
};
} else if (action === CommandControlMessage.TYPE_SET_CLIPBOARD) {
btn.onclick = () => {
const text = input.value;
if (text) {
client.sendMessage(CommandControlMessage.createSetClipboardCommand(text));
}
};
} else {
btn.onclick = () => {
client.sendMessage(new CommandControlMessage(action));
};
}
}
GoogMoreBox.wrap('p', commands, moreBox);
const screenPowerModeId = `screen_power_mode_${udid}_${playerName}_${displayId}`;
const screenPowerModeLabel = document.createElement('label');
screenPowerModeLabel.style.display = 'none';
const labelTextPrefix = 'Mode';
const buttonTextPrefix = 'Set screen power mode';
const screenPowerModeCheck = document.createElement('input');
screenPowerModeCheck.type = 'checkbox';
let mode = (screenPowerModeCheck.checked = false) ? 'ON' : 'OFF';
screenPowerModeCheck.id = screenPowerModeLabel.htmlFor = screenPowerModeId;
screenPowerModeLabel.innerText = `${labelTextPrefix} ${mode}`;
screenPowerModeCheck.onchange = () => {
mode = screenPowerModeCheck.checked ? 'ON' : 'OFF';
screenPowerModeLabel.innerText = `${labelTextPrefix} ${mode}`;
sendScreenPowerModeButton.innerText = `${buttonTextPrefix} ${mode}`;
};
const sendScreenPowerModeButton = document.createElement('button');
sendScreenPowerModeButton.innerText = `${buttonTextPrefix} ${mode}`;
sendScreenPowerModeButton.onclick = () => {
const message = CommandControlMessage.createSetScreenPowerModeCommand(screenPowerModeCheck.checked);
client.sendMessage(message);
};
GoogMoreBox.wrap('p', [screenPowerModeCheck, screenPowerModeLabel, sendScreenPowerModeButton], moreBox, [
'flex-center',
]);
const qualityId = `show_video_quality_${udid}_${playerName}_${displayId}`;
const qualityLabel = document.createElement('label');
const qualityCheck = document.createElement('input');
qualityCheck.type = 'checkbox';
qualityCheck.checked = BasePlayer.DEFAULT_SHOW_QUALITY_STATS;
qualityCheck.id = qualityId;
qualityLabel.htmlFor = qualityId;
qualityLabel.innerText = 'Show quality stats';
GoogMoreBox.wrap('p', [qualityCheck, qualityLabel], moreBox, ['flex-center']);
qualityCheck.onchange = () => {
player.setShowQualityStats(qualityCheck.checked);
};
const stop = (ev?: string | Event) => {
if (ev && ev instanceof Event && ev.type === 'error') {
console.error(TAG, ev);
}
const parent = moreBox.parentElement;
if (parent) {
parent.removeChild(moreBox);
}
player.off('video-view-resize', this.onViewVideoResize);
if (this.onStop) {
this.onStop();
delete this.onStop;
}
};
const stopBtn = document.createElement('button') as HTMLButtonElement;
stopBtn.innerText = `Disconnect`;
stopBtn.onclick = stop;
GoogMoreBox.wrap('p', [stopBtn], moreBox);
player.on('video-view-resize', this.onViewVideoResize);
player.on('video-settings', this.onVideoSettings);
this.holder = moreBox;
}
private onViewVideoResize = (size: Size): void => {
// padding: 10px
this.holder.style.width = `${size.width - 2 * 10}px`;
};
private onVideoSettings = (videoSettings: VideoSettings): void => {
if (this.bitrateInput) {
this.bitrateInput.value = videoSettings.bitrate.toString();
}
if (this.maxFpsInput) {
this.maxFpsInput.value = videoSettings.maxFps.toString();
}
if (this.iFrameIntervalInput) {
this.iFrameIntervalInput.value = videoSettings.iFrameInterval.toString();
}
if (videoSettings.bounds) {
const { width, height } = videoSettings.bounds;
if (this.maxWidthInput) {
this.maxWidthInput.value = width.toString();
}
if (this.maxHeightInput) {
this.maxHeightInput.value = height.toString();
}
}
};
private fit = (): void => {
const { width, height } = this.client.getMaxSize() || GoogMoreBox.defaultSize;
if (this.maxWidthInput) {
this.maxWidthInput.value = width.toString();
}
if (this.maxHeightInput) {
this.maxHeightInput.value = height.toString();
}
};
private reset = (): void => {
const preferredSettings = this.player.getPreferredVideoSetting();
this.onVideoSettings(preferredSettings);
};
public OnDeviceMessage(ev: DeviceMessage): void {
if (ev.type !== DeviceMessage.TYPE_CLIPBOARD) {
return;
}
this.input.value = ev.getText();
this.input.select();
document.execCommand('copy');
}
private static wrap(
tagName: string,
elements: HTMLElement[],
parent: HTMLElement,
opt_classes?: string[],
): HTMLElement {
const wrap = document.createElement(tagName);
if (opt_classes) {
wrap.classList.add(...opt_classes);
}
elements.forEach((e) => {
wrap.appendChild(e);
});
parent.appendChild(wrap);
return wrap;
}
public getHolderElement(): HTMLElement {
return this.holder;
}
public setOnStop(listener: () => void): void {
this.onStop = listener;
}
}