-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtcpeers.js
116 lines (107 loc) · 3.64 KB
/
rtcpeers.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
var code = `
window.srPeerConnections = [];
(function(original) {
RTCPeerConnection = function() {
var connection = new original(arguments);
window.srPeerConnections.push(connection);
return connection;
};
RTCPeerConnection.prototype = original.prototype;
})(RTCPeerConnection);
function createRemoteStreamWindowObject() {
window.srPeerConnections.forEach((pc) => {
var streams = pc.getRemoteStreams();
streams.forEach((stream) => {
var tracks = stream.getTracks();
if (tracks.length === 2) {
window.srRemoteStream = stream;
}
});
});
}
function downloadStream(chunks) {
var blob = new Blob(chunks, {
type: chunks[0].type
});
var url = URL.createObjectURL(blob);
window.dispatchEvent(new CustomEvent('stadiaRecorder', {
detail: {
fn: 'downloadUrl',
fnArgs: [
url
]
}
}));
}
function startStream() {
if (!window.srRemoteStream && !window.srRecorder && !window.srChunks) {
createRemoteStreamWindowObject();
if (window.srRemoteStream) {
window.srRecorder = new MediaRecorder(window.srRemoteStream, getBitrate(window.srRemoteStream));
window.srChunks = [];
window.srRecorder.ondataavailable = (e) => {
window.srChunks.push(e.data);
if (window.srRecorder.state === "inactive") {
downloadStream(window.srChunks);
window.srRemoteStream = undefined;
window.srRecorder = undefined;
}
};
window.srRecorder.start();
showNotification('Recording started');
} else {
showNotification('No active stream found');
}
} else {
showNotification('Ongoing stream exists');
}
}
function pauseResumeStream() {
if (window.srRecorder) {
var state = window.srRecorder.state;
if (state === "recording") {
window.srRecorder.pause();
showNotification('Recording paused');
}
else if (state === "paused") {
window.srRecorder.resume();
showNotification('Recording resumed');
}
} else {
showNotification('Did you start recording?');
}
}
function stopStream() {
if (window.srRecorder) {
window.srRecorder.stop();
showNotification('Recording stopped');
} else {
showNotification('No ongoing stream to stop');
}
}
function showNotification(message) {
console.log('Stadia Recorder:', message);
window.dispatchEvent(new CustomEvent('stadiaRecorder', {
detail: {
fn: 'showNotification',
fnArgs: [
message
]
}
}));
}
function getBitrate(ms) {
var bitrate = 10000000;
var height = ms.getVideoTracks()[0].getSettings().height;
if (height <= 720) bitrate *= 1;
else if (height <= 1080) bitrate *= 2.5;
else if (height <= 2160) bitrate *= 3.5;
return {
bitsPerSecond: bitrate
};
}
`;
var script = document.createElement('script');
script.textContent = code;
(document.head||document.documentElement).appendChild(script);
script.remove();