-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
executable file
·184 lines (158 loc) · 5.48 KB
/
script.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
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
/* eslint-disable require-jsdoc */
'use strict'
$(function() {
// Peer object
const peer = new Peer({
key: "e91a15d8-c4ec-40de-a9c1-547f2a3b32d3",
debug: 3,
});
let localStream;
let room;
peer.on('open', () => {
$('#my-id').text(peer.id);
// Get things started
step1();
});
peer.on('error', err => {
alert(err.message);
// Return to step 2 if error occurs
step2();
});
$('#make-call').on('submit', e => {
e.preventDefault();
// Initiate a call!
const roomName = $('#join-room').val();
if (!roomName) {
return;
}
room = peer.joinRoom('mesh_multi_' + roomName, {stream: localStream});
$('#room-id').text(roomName);
step3(room);
});
$('#end-call').on('click', () => {
$('#chatbox-'+room.name).hide() // 切断時にチャットボックスを隠す
room.close();
step2();
});
// Retry if getUserMedia fails
$('#step1-retry').on('click', () => {
$('#step1-error').hide();
step1();
});
// set up audio and video input selectors
const audioSelect = $('#audioSource');
const videoSelect = $('#videoSource');
const selectors = [audioSelect, videoSelect];
navigator.mediaDevices.enumerateDevices()
.then(deviceInfos => {
const values = selectors.map(select => select.val() || '');
selectors.forEach(select => {
const children = select.children(':first');
while (children.length) {
select.remove(children);
}
});
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
const option = $('<option>').val(deviceInfo.deviceId);
if (deviceInfo.kind === 'audioinput') {
option.text(deviceInfo.label ||
'Microphone ' + (audioSelect.children().length + 1));
audioSelect.append(option);
} else if (deviceInfo.kind === 'videoinput') {
option.text(deviceInfo.label ||
'Camera ' + (videoSelect.children().length + 1));
videoSelect.append(option);
}
}
selectors.forEach((select, selectorIndex) => {
if (Array.prototype.slice.call(select.children()).some(n => {
return n.value === values[selectorIndex];
})) {
select.val(values[selectorIndex]);
}
});
videoSelect.on('change', step1);
audioSelect.on('change', step1);
});
function step1() {
// Get audio/video stream
const audioSource = $('#audioSource').val();
const videoSource = $('#videoSource').val();
const constraints = {
audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
video: {deviceId: videoSource ? {exact: videoSource} : undefined},
};
navigator.mediaDevices.getUserMedia(constraints).then(stream => {
$('#my-video').get(0).srcObject = stream;
localStream = stream;
if (room) {
room.replaceStream(stream);
return;
}
step2();
}).catch(err => {
$('#step1-error').show();
console.error(err);
});
}
function step2() {
$('#their-videos').empty();
$('#step1, #step3').hide();
$('#step2').show();
$('#join-room').focus();
}
function step3(room) {
// chatboxを追加する
const chatbox = $('<div></div>').addClass('chatbox').attr('id', 'chatbox-'+room.name);
const header = $('<h4></h4>').html('Room: <strong>' + room.name + '</strong>');
const messages = $('<div><em>Peer connected.</em></div>').addClass('messages');
chatbox.append(header);
chatbox.append(messages);
$('#chatframe').append(chatbox);
// メッセージ送信部分
$('#sendtextform').on('submit', e => {
e.preventDefault(); // form送信を抑制
const msg = $('#mymessage').val();
// ルームに送って自分のところにも反映
room.send(msg);
messages.prepend('<div><span class="you">You: </span>' + msg + '</div>');
$('#mymessage').val('');
});
// チャットとかファイルが飛んできたらdataでonになる
// ここではファイルは使わないのでもとのサンプルのif文はけしておく
room.on('data', message => {
messages.prepend('<div><span class="peer">' + message.src.substr(0,8) + '</span>: ' + message.data + '</div>');
});
room.on('peerJoin', peerId => {
messages.prepend('<div><span class="peer">' + peerId.substr(0,8) + '</span>: has joined the room </div>');
});
room.on('peerLeave', peerId => {
messages.prepend('<div><span class="peer">' + peerId.substr(0,8) + '</span>: has left the room </div>');
});
// streamが飛んできたら相手の画面を追加する
room.on('stream', stream => {
const peerId = stream.peerId;
const id = 'video_' + peerId + '_' + stream.id.replace('{', '').replace('}', '');
$('#their-videos').append($(
'<div class="video_' + peerId +'" id="' + id + '">' +
'<label>' + stream.peerId + ':' + stream.id + '</label>' +
'<video class="remoteVideos" autoplay playsinline>' +
'</div>'));
const el = $('#' + id).find('video').get(0);
el.srcObject = stream;
el.play();
});
room.on('removeStream', function(stream) {
const peerId = stream.peerId;
$('#video_' + peerId + '_' + stream.id.replace('{', '').replace('}', '')).remove();
});
// UI stuff
room.on('close', step2);
room.on('peerLeave', peerId => {
$('.video_' + peerId).remove();
});
$('#step1, #step2').hide();
$('#step3').show();
}
});