-
Notifications
You must be signed in to change notification settings - Fork 1
/
launchMeetingScribe.js
126 lines (109 loc) · 3.9 KB
/
launchMeetingScribe.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
import puppeteer from 'puppeteer-extra';
import UserPreferencesPlugin from 'puppeteer-extra-plugin-user-preferences';
import { removeSpecialCharacter, wait, createDirIfNotExists } from './common.js';
import { concatenateFilesInDirectory } from './AVFileCreator.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function downloadUserLocation(downloadsDir) {
return UserPreferencesPlugin({
userPrefs: {
download: {
prompt_for_download: false,
default_directory: downloadsDir,
},
},
});
}
export const launchMeetingScribe = async (entryPoint, options) => {
const manageAVFilesName = {};
const meetingLink = options.url;
const outputDir = options.outputDir;
const downloadsDir = options.downloadsDir;
const interactiveModeEnabled = options.interactive;
const headless = interactiveModeEnabled ? false : 'new';
// create tmp directories
createDirIfNotExists(outputDir);
createDirIfNotExists(downloadsDir);
puppeteer.use(downloadUserLocation(downloadsDir));
const extensionPath = path.join(__dirname, 'ext');
const launchOptions = {
headless,
defaultViewport: null,
ignoreDefaultArgs: ['--disable-extensions', '--enable-automation'],
args: [
'--no-first-run',
'--start-maximized',
'--no-default-browser-check',
'--auto-accept-camera-and-microphone-capture',
`--load-extension=${extensionPath}`,
],
};
// Currently it is necassay to have chrome installed on system.
launchOptions.channel = 'chrome';
let browser;
try {
browser = await puppeteer.launch(launchOptions);
} catch (e) {
// This error should ideally come if chrome is not installed on M1 mac
// For x64 arch, puppeteer's chrome-for-testing should work
console.error(
'Could not launch chrome on your machine. Please check if chrome is properly installed on your system.',
);
process.exit(1);
}
// wait for browser to load
await wait(2000);
// Find the extension
const targets = browser.targets();
const extensionTarget = targets.find(target => {
return target.type() === 'service_worker';
});
// Extract the URL
const extensionURL = extensionTarget.url();
const urlSplit = extensionURL.split('/');
const extensionID = urlSplit[2];
// Define the extension page
const extensionEndURL = 'index.html';
// Select existing page when browser opens instead of creating a new page
const extPage = await browser.pages().then(e => e[0]);
//Navigate to the page
await extPage.goto(`chrome-extension://${extensionID}/${extensionEndURL}`);
// load 100ms url
const page = await browser.newPage();
await page.bringToFront();
await page.exposeFunction('__100ms_getExtensionId', () => {
return extensionID;
});
await page.goto(meetingLink);
if (entryPoint) {
entryPoint.setPageAccess(page);
await entryPoint.onLoad();
}
await page.exposeFunction('__100ms_onMessageReceivedEvent', data => {
try {
data = JSON.parse(data);
} catch (e) {}
if (!data.streamId) {
// if no streamId sent by the vendor
data.streamId = 'data';
}
if (!manageAVFilesName[data.trackId] && data.action === 'AVTracksAdded') {
manageAVFilesName[data.trackId] = { kind: data.kind, streamId: data.streamId };
const downloadFileDirPath = path.join(outputDir, removeSpecialCharacter(data.trackId));
if (!fs.existsSync(downloadFileDirPath)) {
fs.mkdirSync(downloadFileDirPath);
}
const downloadFilePath = path.join(downloadFileDirPath, `${data.kind}.webm`);
const trackFilePath = path.join(
downloadsDir,
`stream_${removeSpecialCharacter(data.streamId)}`,
`track_${removeSpecialCharacter(data.trackId)}`,
data.kind,
);
concatenateFilesInDirectory(trackFilePath, downloadFilePath);
}
});
};