-
Notifications
You must be signed in to change notification settings - Fork 134
/
background.js
147 lines (125 loc) · 4.58 KB
/
background.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
/* global chrome, MediaRecorder, FileReader */
let recorder = null;
let filename = null;
let ws;
let liveSteam = false;
let ffmpegServer;
let doDownload = true;
chrome.runtime.onConnect.addListener(port => {
port.onMessage.addListener(msg => {
console.log(msg);
switch (msg.type) {
case 'SET_EXPORT_PATH':
filename = msg.filename
break
case 'FFMPEG_SERVER':
ffmpegServer = msg.ffmpegServer
startWebsock();
break
case 'REC_STOP':
doDownload = true;
recorder.stop()
break
case 'REC_START':
if (liveSteam) {
recorder.start(1000);
} else {
recorder.start();
}
break
case 'REC_CLIENT_PLAY':
if (recorder) {
return
}
const tab = port.sender.tab
tab.url = msg.data.url
chrome.desktopCapture.chooseDesktopMedia(['tab', 'audio'], streamId => {
// Get the stream
navigator.webkitGetUserMedia({
audio: {
mandatory: {
chromeMediaSource: 'system'
}
},
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: streamId,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720,
minFrameRate: 60,
}
}
}, stream => {
var chunks = [];
recorder = new MediaRecorder(stream, {
videoBitsPerSecond: 2500000,
ignoreMutedMedia: true,
mimeType: 'video/webm;codecs=h264'
});
recorder.ondataavailable = function (event) {
if (event.data.size > 0) {
chunks.push(event.data);
if (liveSteam) {
ws.send(event.data);
}
}
};
recorder.onstop = function () {
if (liveSteam) {
ws.close();
}
if(!doDownload){
chunks = [];
return;
}
var superBuffer = new Blob(chunks, {
type: 'video/webm'
});
var url = URL.createObjectURL(superBuffer);
// var a = document.createElement('a');
// document.body.appendChild(a);
// a.style = 'display: none';
// a.href = url;
// a.download = 'test.webm';
// a.click();
chrome.downloads.download({
url: url,
filename: filename
}, () => {
});
}
}, error => console.log('Unable to get user media', error))
})
break
default:
console.log('Unrecognized message', msg)
}
})
chrome.downloads.onChanged.addListener(function (delta) {
if (!delta.state || (delta.state.current != 'complete')) {
return;
}
try {
port.postMessage({ downloadComplete: true })
}
catch (e) { }
});
})
function startWebsock() {
ws = new WebSocket(ffmpegServer);
liveSteam = true;
ws.onmessage = function (e) {
console.log(e.data);
if (e.data == "ffmpegClosed") {
doDownload = false;
recorder.stop();
setTimeout(function () {
startWebsock();
recorder.start(1000);
}, 500)
}
}
}