-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.html
288 lines (255 loc) · 9.01 KB
/
send.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
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>webRTC</title>
</head>
<body>
<style>
*{
margin:0;
padding:0;
}
html{
width:100%;
height:100%;
}
body{
width:100%;
height:100%;
}
#video{
width:640px;
height:360px;
display:none;
}
#statusConnection{
}
#btn_start{
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
<b id="statusConnection">未接続</b>
<input type="button" value="デバイス検出" onclick="deviceGetList()">
<div><label>video:<select id="selectVideo"></select></label></div>
<div><label>audio:<select id="selectAudio"></select></label></div>
<input type="button" onclick="startCamera()" value="カメラ接続" id="btn_camera" disabled>
<video id="video" controls></video>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script src="https://stream.ikasekai.com/js/simplepeer.min.js"></script>
<script>
var initiator = true;
var socketReady = false;
var socket = io.connect();
var peerClient = null;
var broadcastStream = null;
var intervalConnection = null;
var selectAudio = document.querySelector("#selectAudio");
var selectVideo = document.querySelector("#selectVideo");
var buttonCameraConnect = document.querySelector("#btn_camera");
const listPeerConnection = {};
var url = new URL(document.location.href)
socket.emit("client_to_server_join", {room : getRoomName() });
socket.emit("client_to_server_personal", {value : name});
// socket: channel connected
socket.on('connect', onOpened)
.on('server_to_client', onMessage);
function getRoomName() { // たとえば、 URLに ?roomname とする
var args = url.pathname.split('/');
console.log(args)
if (args.length > 1) {
var room = args[2];
if (room != "") {
return 'room' + room;
}
}
return "_defaultroom";
}
function onOpened(evt) {
console.log('socket opened.');
socketReady = true;
var roomname = getRoomName(); // 会議室名を取得する
socket.emit('enter', roomname);
}
function onMessage(evt) {
console.log(evt);
//document.querySelector('#outgoing').textContent = JSON.stringify(evt)
if(initiator){
if(evt.data.type=="call"){
if(broadcastStream){
socket.emit("client_to_server_to", evt.clientId, {type:'received'});
listPeerConnection[evt.clientId] = createPeer(evt.clientId);
}
}else if(evt.data.type=="answer"){
listPeerConnection[evt.clientId].signal(evt.data)
}else if(evt.data.type=="leave"){
console.log('disconnect:' + evt.clientId);
delete listPeerConnection[evt.clientId]
}
}else{
if(evt.data.type=="received"){
clearInterval(intervalConnection);
}else if(evt.data.type=="offer"){
peerClient.signal(evt.data)
}
}
}
function startCallConnection(){
setStatus('-接続処理開始...');
document.querySelector('#btn_start').style.display = 'none'
let text = '▲接続中...'
intervalConnection = setInterval(function(){
if(socketReady){
console.log('try connect');
setStatus(text + '.')
callConnection();
}
},3000
);
}
function callConnection(){
if(peerClient == null){
peerClient = createPeer();
}
socket.emit("client_to_server_broadcast", {type:'call',clientId:socket.id});
}
function gotMedia(stream){
broadcastStream = stream;
// buttonCameraConnect.disabled = true
var video = document.querySelector('#video');
video.muted = true;
video.style.display = 'block';
if ('srcObject' in video) {
video.srcObject = stream
} else {
video.src = window.URL.createObjectURL(stream) // for older browsers
}
video.play()
for (let key in listPeerConnection) {
if(listPeerConnection[key]){
listPeerConnection[key].addStream(broadcastStream)
}else{
delete listPeerConnection[key]
}
}
}
function startCamera(){
navigator.mediaDevices.getUserMedia({
video: {
deviceId:selectVideo.value,
width:1280,
height:720
},
audio: {
deviceId:selectAudio.value
}
}).then(gotMedia).catch(() => {})
}
function createPeer(socketSendTo){
let param = {
initiator: initiator,
trickle: false,
config:{iceServers: [{ urls: 'stun:stream.ikasekai.com:3478' },]}
}
if(broadcastStream){
param.stream = broadcastStream;
}
let peerClient = new SimplePeer(param)
peerClient.on('error', err => console.log('error', err))
peerClient.on('signal', data => {
console.log('SIGNAL', JSON.stringify(data));
if(socketSendTo){
// 特定socketに送信
socket.emit("client_to_server_to", socketSendTo, data);
}else{
// Room全体に送信
socket.emit("client_to_server_broadcast", data);
}
//socket.to(clientId).emit('client_to_server_broadcast', data);
//document.querySelector('#outgoing').textContent = JSON.stringify(data)
})
peerClient.on('connect', () => {
console.log('CONNECT')
setStatusConnect();
peerClient.send('whatever' + Math.random())
})
peerClient.on('data', data => {
console.log('data: ' + data)
})
if(!initiator){
peerClient.on('stream', stream => {
// got remote video stream, now let's show it in a video tag
var video = document.querySelector('#video')
if ('srcObject' in video) {
video.srcObject = stream
} else {
video.src = window.URL.createObjectURL(stream) // for older browsers
}
video.addEventListener('canplay', function() {
video.play()
});
})
}
return peerClient;
}
function setStatusConnect(){
setStatus('●接続済み');
}
function setStatus(text){
document.querySelector('#statusConnection').innerHTML = text;
}
function deviceGetList() {
deviceClearList();
navigator.mediaDevices.enumerateDevices().then(
function(devices) {
devices.forEach(
function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId);
deviceAdd(device);
}
);
}
).catch(
function(err) {
console.error('enumerateDevide ERROR:', err);
}
);
buttonCameraConnect.disabled = false
}
function deviceClearList(){
selectAudio.innerHTML = '';
selectVideo.innerHTML = '';
}
function deviceAdd(device) {
if (device.kind === 'audioinput') {
var id = device.deviceId;
var label = device.label || 'microphone';
var option = document.createElement('option');
option.setAttribute('value', id);
option.innerHTML = label;
selectAudio.appendChild(option);
console.log(label, id);
}
else if (device.kind === 'videoinput') {
var id = device.deviceId;
var label = device.label || 'camera';
var option = document.createElement('option');
option.setAttribute('value', id);
option.innerHTML = label;
selectVideo.appendChild(option);
console.log(label, id);
}else if(device.kind === 'audiooutput') {
}
else {
console.error('UNKNOWN Device kind:' + device.kind);
}
}
deviceGetList()
</script>
</body>
</html>