-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
video-plus-screen-recording.html
158 lines (133 loc) · 4.87 KB
/
video-plus-screen-recording.html
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
<style>
html, body {
margin: 0!important;
padding: 0!important;
text-align: center;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 1em;
}
video {
width: 30%;
border-radius: 5px;
border: 1px solid black;
}
</style>
<title>Video+Screen recording using RecordRTC</title>
<h1>
Video+Screen recording using RecordRTC
</h1>
<p>It will record 10-seconds video and automatically stop the recording.</p>
<video controls autoplay playsinline style="width: 40%;"></video>
<script src="/RecordRTC.js"></script>
<script>
if(!navigator.getDisplayMedia && !navigator.mediaDevices.getDisplayMedia) {
var error = 'Your browser does NOT supports getDisplayMedia API.';
document.querySelector('h1').innerHTML = error;
document.querySelector('video').style.display = 'none';
document.getElementById('btn-start-recording').style.display = 'none';
document.getElementById('btn-stop-recording').style.display = 'none';
throw new Error(error);
}
function invokeGetDisplayMedia(success, error) {
var displaymediastreamconstraints = {
video: {
displaySurface: 'monitor', // monitor, window, application, browser
logicalSurface: true,
cursor: 'always' // never, always, motion
}
};
// above constraints are NOT supported YET
// that's why overridnig them
displaymediastreamconstraints = {
video: true
};
if(navigator.mediaDevices.getDisplayMedia) {
navigator.mediaDevices.getDisplayMedia(displaymediastreamconstraints).then(success).catch(error);
}
else {
navigator.getDisplayMedia(displaymediastreamconstraints).then(success).catch(error);
}
}
function captureScreen(callback) {
invokeGetDisplayMedia(function(screen) {
addStreamStopListener(screen, function() {
if(window.stopCallback) {
window.stopCallback();
}
});
callback(screen);
}, function(error) {
console.error(error);
alert('Unable to capture your screen. Please check console logs.\n' + error);
});
}
function captureCamera(cb) {
navigator.mediaDevices.getUserMedia({audio: true, video: true}).then(cb);
}
function keepStreamActive(stream) {
var video = document.createElement('video');
video.muted = true;
video.srcObject = stream;
video.style.display = 'none';
(document.body || document.documentElement).appendChild(video);
}
captureScreen(function(screen) {
keepStreamActive(screen);
captureCamera(function(camera) {
keepStreamActive(camera);
screen.width = window.screen.width;
screen.height = window.screen.height;
screen.fullcanvas = true;
camera.width = 320;
camera.height = 240;
camera.top = screen.height - camera.height;
camera.left = screen.width - camera.width;
var recorder = RecordRTC([screen, camera], {
type: 'video',
mimeType: 'video/webm',
previewStream: function(s) {
document.querySelector('video').muted = true;
document.querySelector('video').srcObject = s;
}
});
recorder.startRecording();
window.stopCallback = function() {
window.stopCallback = null;
recorder.stopRecording(function() {
var blob = recorder.getBlob();
document.querySelector('video').srcObject = null;
document.querySelector('video').src = URL.createObjectURL(blob);
document.querySelector('video').muted = false;
[screen, camera].forEach(function(stream) {
stream.getTracks().forEach(function(track) {
track.stop();
});
});
});
};
window.timeout = setTimeout(window.stopCallback, 10 * 1000);
});
});
function addStreamStopListener(stream, callback) {
stream.addEventListener('ended', function() {
callback();
callback = function() {};
}, false);
stream.addEventListener('inactive', function() {
callback();
callback = function() {};
}, false);
stream.getTracks().forEach(function(track) {
track.addEventListener('ended', function() {
callback();
callback = function() {};
}, false);
track.addEventListener('inactive', function() {
callback();
callback = function() {};
}, false);
});
}
</script>
<footer style="margin-top: 20px; text-align: left;"><small id="send-message"></small></footer>
<script src="https://www.webrtc-experiment.com/common.js"></script>