-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
124 lines (114 loc) · 3.48 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>synced-playback</title>
</head>
<body id="body">
<video id="video1" autoplay playsinline></video>
<div>
<input type="number" id="seekTime" value="30">
<button type="button" onClick="seekClick()">Seek</button>
<button type="button" onClick="playClick()">Play</button>
<button type="button" onClick="pauseClick()">Pause</button>
</div>
<script>
let conn = new WebSocket('ws://' + window.location.host + '/ws')
let pc = new RTCPeerConnection()
window.seekClick = () => {
conn.send(JSON.stringify({event: 'seek', data: document.getElementById('seekTime').value}))
}
window.playClick = () => {
conn.send(JSON.stringify({event: 'play', data: ''}))
}
window.pauseClick = () => {
conn.send(JSON.stringify({event: 'pause', data: ''}))
}
pc.ontrack = function (event) {
console.log('track received', event.track.kind, event.track);
if (event.track.kind === 'video') {
var el = document.getElementById('video1')
el.srcObject = event.streams[0]
el.autoplay = true
el.controls = true
console.log("get element by id video1", el)
el.onloadedmetadata = ()=>{
console.log('video metadata loaded');
el.play().then(() => console.log('video playback started')).catch(e=> console.log('video playback failed',e))
}
}
el.onloadedmetadata = () => {
console.log('Video metadata loaded');
}
el.onplaying = () => {
console.log('Video started playing');
}
el.onwaiting = () => {
console.log('Video is waiting for data');
}
el.onerror = (e) => {
console.error('Video error:', e);
}
}
pc.onconnectionstatechange = event => {
console.log("Connection State Change:", pc.connectionState);
if (pc.connectionState === "failed") {
console.error("Connection Failed. Possible SDP Mismatch or ICE Failure.");
}
}
pc.onicecandidate = event =>{
if (event.candidate){
console.log('sending ice candidate to server')
conn.send(JSON.stringify({
event: 'candidate',
data: event.candidate
}))
}else{
console.log('ice candidate gathering complete')
}
}
pc.oniceconnectionstatechange = () => {
if (pc.iceConnectionState == "connected") {
console.log("ICE gathering complete");
}
console.log('Connection state:', pc.connectionState);
}
pc.onicegatheringstatechange = () => {
console.log('ICE gathering state:', pc.iceGatheringState);
}
conn.onopen = () => {
console.log('websocket connection opened, waiting for offer')
}
conn.onclose = evt => {
console.log('Connection closed')
}
conn.onmessage = async evt => {
let msg = JSON.parse(evt.data)
if (!msg) {
return console.log('failed to parse msg')
}
switch (msg.event) {
case 'offer':
console.log('received offer from server')
await pc.setRemoteDescription(new RTCSessionDescription(msg.data))
const answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
conn.send(JSON.stringify({
event:'answer',
data:answer
}))
console.log('sent sdp answer')
break;
case 'candidate':
console.log('received ice candidates from server', msg.data)
try {
await pc.addIceCandidate(new RTCIceCandidate(msg.data));
} catch (error) {
console.error('Error adding ICE candidate:', error);
}
break;
}
}
window.conn = conn
</script>
</body>
</html>