-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.ts
214 lines (181 loc) · 5.63 KB
/
stream.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
import {
launch as puppeteerLaunch,
LaunchOptions,
Browser,
Page,
BrowserLaunchArgumentOptions,
BrowserConnectOptions,
} from "puppeteer";
import * as path from "path";
import * as url from "url";
type PageWithExtension = Omit<Page, "browser"> & {
browser(): BrowserWithExtension;
};
type StreamLaunchOptions = LaunchOptions &
BrowserLaunchArgumentOptions &
BrowserConnectOptions & {
allowIncognito?: boolean;
};
let currentIndex = 0;
declare module "puppeteer" {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Page {
index: number;
startStream(): Promise<string>;
}
}
type BrowserWithExtension = Browser & { videoCaptureExtension?: Page };
const extensionPath = path.join(
url.fileURLToPath(new URL(".", import.meta.url)),
"..",
"extension"
);
const extensionId = "ohjhakbncjlkfjihjcoibikidbgccmoc";
export async function launch(
arg1:
| (StreamLaunchOptions &
BrowserLaunchArgumentOptions &
BrowserConnectOptions)
| any,
opts?: StreamLaunchOptions &
BrowserLaunchArgumentOptions &
BrowserConnectOptions
): Promise<Browser> {
// if puppeteer library is not passed as first argument, then first argument is options
if (typeof arg1.launch !== "function") {
opts = arg1;
}
if (!opts) opts = {};
if (!opts.args) opts.args = [];
function addToArgs(arg: string, value?: string) {
if (!value) {
if (opts.args.includes(arg)) return;
opts.args.push(arg);
return;
}
let found = false;
opts.args = opts.args.map((x) => {
if (x.includes(arg)) {
found = true;
return `${x},${value}`;
}
return x;
});
if (!found) opts.args.push(arg + value);
}
addToArgs("--load-extension=", extensionPath);
addToArgs("--disable-extensions-except=", extensionPath);
addToArgs("--allowlisted-extension-id=", extensionId);
addToArgs("--autoplay-policy=no-user-gesture-required");
if (opts.defaultViewport?.width && opts.defaultViewport?.height)
opts.args.push(
`--window-size=${opts.defaultViewport.width}x${opts.defaultViewport.height}`
);
opts.headless = false;
/*
let loadExtension = false;
let loadExtensionExcept = false;
let whitelisted = false;
opts.args = opts.args.map((x) => {
if (x.includes("--load-extension=")) {
loadExtension = true;
return `${x},${extensionPath}`;
}
if (x.includes("--disable-extensions-except=")) {
loadExtensionExcept = true;
return `--disable-extensions-except=${extensionPath},${x.split("=")[1]}`;
}
if (x.includes("--whitelisted-extension-id")) {
whitelisted = true;
return `${x},${extensionId}`;
}
return x;
});
if (!loadExtension) opts.args.push(`--load-extension=${extensionPath}`);
if (!loadExtensionExcept)
opts.args.push(`--disable-extensions-except=${extensionPath}`);
if (!whitelisted) opts.args.push(`--whitelisted-extension-id=${extensionId}`);
if (opts.defaultViewport?.width && opts.defaultViewport?.height)
opts.args.push(
`--window-size=${opts.defaultViewport?.width}x${opts.defaultViewport?.height}`
);
/* if (opts.defaultViewport?.deviceScaleFactor) {
// opts.args.push(`--device-scale-factor=${opts.defaultViewport?.deviceScaleFactor}`);
opts.args.push(
// --device-scale-factor=${opts.defaultViewport?.deviceScaleFactor}
`--force-device-scale-factor=${opts.defaultViewport?.deviceScaleFactor}`
);
} */
// opts.args.push('--enable-use-zoom-for-dsf');
// opts.headless = false;
let browser: BrowserWithExtension;
if (typeof arg1.launch === "function") {
browser = await arg1.launch(opts);
} else {
browser = await puppeteerLaunch(opts);
}
if (opts.allowIncognito) {
const settings = await browser.newPage();
await settings.goto(`chrome://extensions/?id=${extensionId}`);
await settings.evaluate(() => {
(document as any)
.querySelector("extensions-manager")
.shadowRoot.querySelector(
"#viewManager > extensions-detail-view.active"
)
.shadowRoot.querySelector(
"div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito"
)
.shadowRoot.querySelector("label#label input")
.click();
});
await settings.close();
}
const extensionTarget = await browser.waitForTarget(
(t) =>
t.url() === `chrome-extension://${extensionId}/options.html` &&
t.type() === "page"
);
if (!extensionTarget) {
throw new Error("cannot load extension");
}
const videoCaptureExtension = await extensionTarget.page();
if (!videoCaptureExtension) {
throw new Error("cannot get page of extension");
}
browser.videoCaptureExtension = videoCaptureExtension;
/*await browser.videoCaptureExtension.exposeFunction(
"log",
(...parameters: any) => {
console.log("videoCaptureExtension", ...parameters);
}
);*/
return browser;
}
export async function startStream(
page: PageWithExtension,
room?: string,
zoom?: number
) {
await page.bringToFront();
if (page.index === undefined) {
page.index = currentIndex++;
}
console.log('start recording', page.index, room, zoom);
await page.bringToFront();
await page.browser().videoCaptureExtension?.evaluate(
(settings) => {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return START_RECORDING(settings);
} catch (err) {
console.error('err', err);
return err;
}
},
{ index: page.index, room, zoom }
);
// page.browser().encoders?.set(page.index, encoder);
return room || `room${page.index}`;
}