-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
211 lines (187 loc) · 5.85 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
<!DOCTYPE html>
<html>
<head>
<title>rtcfire Simple Multi-peer Demo</title>
<meta name="viewport" content="width=device-width, minimum-scale=1" />
<style>
body {
font-family: sans-serif;
margin: 20px;
}
#topic-header code {
font-size: 16px;
color: coral;
}
#peers-header,
#zero-state {
display: none;
}
#peers {
list-style: none;
padding: 0;
display: flex;
flex-flow: row wrap;
}
video {
background-color: #eee;
object-fit: cover;
width: 300px;
aspect-ratio: 4 / 3;
max-width: 100%;
}
#peers video {
margin: 0 8px 8px 0;
}
#my-video {
transform: scaleX(-1);
}
</style>
</head>
<body>
<h1>
<a href="https://github.com/romannurik/rtcfire">rtcfire</a> Simple
Multi-Peer Demo
</h1>
<p id="topic-header"></p>
<h3>Your local video stream</h3>
<video id="my-video" muted autoplay playsinline></video>
<h3 id="peers-header">Other participants</h3>
<ul id="peers"></ul>
<div id="zero-state">
Share this page's URL with a friend, or open it in another tab!
</div>
<script src="https://www.gstatic.com/firebasejs/8.2.10/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.10/firebase-database.js"></script>
<script src="https://unpkg.com/rtcfire/dist/index.umd.js"></script>
<script>
const FRUIT = [
'apple',
'apricot',
'avocado',
'banana',
'cacao',
'cashew',
'cherry',
'coconut',
'currant',
'date',
'fig',
'grape',
'guava',
'kiwi',
'kumquat',
'lemon',
'lime',
'lychee',
'melon',
'nutmeg',
'orange',
'papaya',
'peach',
'pear',
'plum',
'pumpkin',
'tomato',
];
const randomTopic = () =>
Array(3)
.fill()
.map(() => FRUIT[Math.floor(Math.random() * FRUIT.length)])
.join('-');
const TAB_ID = String(Math.floor(Math.random() * 9999));
class Demo {
constructor() {
this.topic = (window.location.search.match(/topic=(.+)/) || [])[1];
this.myId = String(Math.floor(Math.random() * 9999));
console.log(this.myId);
if (this.topic) {
document.querySelector(
'#topic-header',
).innerHTML += `Room/topic: <code><b>${this.topic}</b></code>`;
this.setupFirebase();
// this.signIn();
this.setupVideo();
} else {
window.location.search = `topic=${randomTopic()}`;
}
// fix for mobile browser freezes
window.addEventListener('beforeunload', () => {
this.rtcSession && this.rtcSession.close();
});
}
setupFirebase() {
firebase.initializeApp({
apiKey: 'AIzaSyDUeSX9EYJzc-0DoxRZbK7HSz-9nehq3Lo',
authDomain: 'hang-out-together.firebaseapp.com',
projectId: 'hang-out-together',
storageBucket: 'hang-out-together.appspot.com',
appId: '1:48209665356:web:ed662fdf16d2ee1f9a03e9',
databaseURL:
'https://hang-out-together-default-rtdb.asia-southeast1.firebasedatabase.app',
});
}
setupVideo() {
let participantsRef = firebase
.database()
.ref(`${this.topic}/participants`);
let videoStreams = {};
let meRef = participantsRef.child(this.myId);
meRef.update({ joined: true });
meRef.onDisconnect().set(null);
this.rtcSession = rtcfire.rtcFireSession({
myId: this.myId,
negotiationRef: firebase
.database()
.ref(`${this.topic}/participants`),
onMyStream: (stream) =>
(document.querySelector('#my-video').srcObject = stream),
onParticipantStream: (pid, stream) => {
videoStreams[pid] = stream;
this.updatePeers(this.rtcSession.participants, videoStreams);
},
});
participantsRef.on('value', (snap) => {
console.log('on value =====', snap);
let participants = Object.keys(snap.val() || {});
this.rtcSession.participants = participants;
this.updatePeers(participants, videoStreams);
});
}
updatePeers(participants, videoStreams) {
participants = new Set(
participants.filter((pid) => pid !== this.myId),
);
console.log(participants);
let parent = document.querySelector('#peers');
// new and existing peers
for (let pid of participants) {
let node = parent.querySelector(`li[data-pid="${pid}"]`);
if (!node) {
node = document.createElement('li');
node.setAttribute('data-pid', pid);
node.innerHTML = '<video autoplay playsinline>';
parent.appendChild(node);
}
let video = node.querySelector('video');
video.srcObject = videoStreams[pid];
}
// removed peers
for (let existing of parent.querySelectorAll('li')) {
if (!participants.has(existing.getAttribute('data-pid'))) {
parent.removeChild(existing);
}
}
// zero state
let zero = participants.size === 0;
document.querySelector('#peers-header').style.display = zero
? 'none'
: 'block';
document.querySelector('#zero-state').style.display = zero
? 'block'
: 'none';
}
}
window.demo = new Demo();
</script>
</body>
</html>