-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
281 lines (234 loc) · 10.7 KB
/
index.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<title>SimpleWebRTC Demo</title>
</head>
<body>
<h1 id="title">Start a room</h1>
<button id="screenShareButton"></button>
<p id="subTitle"></p>
<form id="createRoom">
<input id="sessionInput"/>
<button type="submit">Create it!</button>
</form>
<p>
<button id="start">Share Screen</button>
<button id="recordBtn">Record</button>
<button id="stopRecordBtn">Stop</button>
</p>
<!-- <div class="videoContainer">
<video id="localVideo" style="height: 150px;" oncontextmenu="return false;" autoplay="true"></video>
<div id="remoteVideos"></div>
</div>
<div id="remotes"></div> -->
<div id="peers"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<!-- <script src="../dist/easy-mediasoup.bundle.js"></script> -->
<script src="../dist/easy-mediasoup.bundle.js"></script>
<script>
// grab the room from the URL
var user_uuid = "" + Math.floor((Math.random() * 1000 * 1000 * 1000));
var room = location.search && location.search.split('?')[1];
var config = {
roomId:'test123fvd',
media_server_wss:"wss://demo.mediasoup.org:3443",
peerName:user_uuid,
}
var em = new EasyMediasoup.Init(config);
this.emitter.on("joinRoom",(client)=>{
this.client = client
console.log("CLIENT", client)
});
peers = [];
var screenShareOn = false;
em.emitter.on('SET_ROOM_STATE', (state) => { //new/connecting/connected/disconnected/closed
console.log("SET_ROOM_STATE", state);
});
em.emitter.on('enableWebcam',(test)=>{
console.error("enableWebcam",test);
});
em.emitter.on("SET_PRODUCER_TRACK", (MSTrack) => {
console.warn("SET_PRODUCER_TRACK",MSTrack);
setProducerTrack(MSTrack);
});
em.emitter.on("ADD_PRODUCER", (MSTrack) => {
console.warn("ADD_PRODUCER",MSTrack);
setProducerTrack(MSTrack);
});
let setProducerTrack = (MSTrack) =>{
//Take current redux state to get mediastream
var state = em.store.getState();
// let stream_service_peer = this.get_peer(user_uuid);
// if (stream_service_peer.is_cam_enabled.value != false && stream_service_peer){
if(MSTrack.source === 'webcam'){
console.log("--Webcam track");
if (!state.me.stream) {
state.me.stream = new MediaStream();
}
//Find old track and remove it out
let oldTrack = state.me.stream.getTracks().find((track) => {
return track.kind == MSTrack.track.kind
})
if (oldTrack) state.me.stream.removeTrack(oldTrack)
console.warn("My tracks",state.me.stream.getTracks())
//Add new track instead old
state.me.stream.addTrack(MSTrack.track);
//render final stream
add_peer(user_uuid, state.me.stream)
}else if(MSTrack.source === 'screen'){
console.log("--Screen track");
if(!state.me.ss_stream){
state.me.ss_stream = new MediaStream();
}
let oldTrack = state.me.ss_stream.getTracks().find((track) => {
return track.kind == MSTrack.track.kind
})
if (oldTrack) state.me.ss_stream.removeTrack(oldTrack)
console.warn("My tracks",state.me.ss_stream.getTracks())
//Add new track instead old
state.me.ss_stream.addTrack(MSTrack.track);
//render final stream
screenShareOn = true;
add_peer_screen(user_uuid, state.me.ss_stream);
}
// }
}
em.emitter.on("SET_CONSUMER_EFFECTIVE_PROFILE",(newConsumer) => {
console.warn("SET_CONSUMER_EFFECTIVE_PROFILE", newConsumer);
})
em.emitter.on("SET_CONSUMER_TRACK", (consumerTrack) => {
//Take current redux
var state = em.store.getState()
//Find necessary peer
let peer = state.peers[consumerTrack.peerName]
// if (peer.stream) URL.revokeObjectURL(peer.stream);//a little optimizations :)
//Create new peer stream and add
if (!peer.stream){
peer.stream = new MediaStream;
}
// peer.consumers.forEach((key) => {
// let track = em.consumers[key].track
// console.log(key, track)
// if (track) peer.stream.addTrack(track)
// })
let oldTrack = peer.stream.getTracks().find((track) => {
return track.kind == consumerTrack.track.kind
})
if (oldTrack) peer.stream.removeTrack(oldTrack)
peer.stream.addTrack(consumerTrack.track)
//render final stream
if (peer.name != user_uuid){
add_peer(peer.name, peer.stream)
}
})
function get_peer(user_uuid){
let peer = peers.find(x => x.name == user_uuid)
if (!peer){
peers.push({
name:user_uuid
})
}
console.log( peers.find(x => x.name == user_uuid))
return peers.find(x => x.name == user_uuid)
}
function add_peer_screen(user_uuid, stream){
let peer = peers.find(x => x.name == user_uuid)
peer.screen_stream_url = stream;
render_peers();
}
function add_peer(user_uuid, stream) {
const peer = get_peer(user_uuid);
// console.log("STREAM", stream)
// if (stream) {
// stream.addEventListener('addtrack', function () {
// URL.revokeObjectURL(stream);
// });
// stream.addEventListener('removetrack', function () {
// URL.revokeObjectURL(stream);
// });
// const stream_src = window.URL.createObjectURL(stream);
peer.stream_url = stream;
render_peers()
}
function render_peers(){
$('#peers').empty()
peers.forEach((peer) => {
if (peer.stream_url){
remoteVideo = document.createElement("video");
remoteVideo.id = peer.name + '-webcam'
remoteVideo.srcObject = peer.stream_url
remoteVideo.play();
remoteVideo.width = 600;
remoteVideo.muted = true;
$('#peers').append(remoteVideo)
}
if(peer.screen_stream_url){
remoteVideo = document.createElement("video");
remoteVideo.id = peer.name + '-screen'
remoteVideo.srcObject = peer.screen_stream_url
remoteVideo.play();
remoteVideo.width = 400;
remoteVideo.muted = true;
$('#peers').append(remoteVideo)
}
})
}
var extensionInstalled = false;
var screenShareActive = false;
document.getElementById('start').addEventListener('click', function() {
if(!screenShareActive){
if (!extensionInstalled){
var message = 'Please install the extension:\n' +
'1. Go to chrome://extensions\n' +
'2. Check: "Enable Developer mode"\n' +
'3. Click: "Load the unpacked extension..."\n' +
'4. Choose "extension" folder from the repository\n' +
'5. Reload this page';
alert(message);
}
window.postMessage({ type: 'SS_UI_REQUEST', text: 'start' }, '*');
screenShareActive = true;
$('#start').html('Stop Share');
} else {
if(em.client.deactivateScreenShare()){
screenShareActive = false;
$('#start').html('Screen Share');
}
}
// send screen-sharer request to content-script
});
//Recording
document.getElementById('recordBtn').addEventListener('click', () => {
document.getElementById('recordBtn').disabled = true;
em.client.record(5000);
});
document.getElementById('stopRecordBtn').addEventListener('click', () => {
em.client.stopRecord();
});
window.onbeforeunload = confirmExit;
function confirmExit(){
if(em.client._recordState !== 'inactive'){
return em.client.finishRecord();
}
}
// listen for messages from the content-script
window.addEventListener('message', function (event) {
if (event.origin != window.location.origin) return;
// content-script will send a 'SS_PING' msg if extension is installed
if (event.data.type && (event.data.type === 'SS_PING')) {
extensionInstalled = true;
}
// user chose a stream
if (event.data.type && (event.data.type === 'SS_DIALOG_SUCCESS')) {
//em.client.record();
em.client.setScreenShare(event.data.streamId);
}
// user clicked on 'cancel' in choose media dialog
if (event.data.type && (event.data.type === 'SS_DIALOG_CANCEL')) {
console.log('User cancelled!');
}
});
</script>
</body>
</html>