-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient2.html
More file actions
135 lines (108 loc) · 4.4 KB
/
client2.html
File metadata and controls
135 lines (108 loc) · 4.4 KB
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
<html>
<head>
<title>Test</title>
</head>
<body>
<video autoplay></video>
<script>
// const from = 1;
// const to = 2;
const from = 2;
const to = 1;
const remoteView = document.querySelector('video');
class SignalingChannel {
constructor(url) {
this.url = url;
this.socket = new WebSocket(url);
}
send(data) {
var socket = this.socket;
socket.send(JSON.stringify(data))
// socket.addEventListener('open', function (msg) {
// console.log("sending")
// socket.send(msg);
// });
}
addListener(handler) {
this.socket.addEventListener('message', handler);
}
};
const signaling = new SignalingChannel(`ws://192.168.1.205:8080/?from=${from}&to=${to}`);
const constraints = { audio: false, video: true };
const configuration = { iceServers: [{ urls: 'stun:stun.iptel.org' }] };
const pc = new RTCPeerConnection(configuration);
pc.onicecandidate = ({ candidate }) => {
console.log(`ice candidate ${JSON.stringify(candidate)}`)
let msg = { "id": to, "body": JSON.stringify({ "type": "candidate", "body": candidate }) }
signaling.send(msg)
};
pc.onnegotiationneeded = async () => {
console.log('Negotiation is needed')
try {
await pc.setLocalDescription(await pc.createOffer());
// send the offer to the other peer
let msg = { "id": to, "body": JSON.stringify({ "type": "desc", "body": pc.localDescription }) }
signaling.send(msg);
// signaling.send({ desc: pc.localDescription });
} catch (err) {
console.error(err);
}
};
pc.ontrack = (event) => {
// don't set srcObject again if it is already set.
if (remoteView.srcObject) return;
remoteView.srcObject = event.streams[0];
console.log("New track received")
};
async function start() {
try {
// get local stream, show it in self-view and add it to be sent
const stream =
await navigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach((track) =>
pc.addTrack(track, stream));
// selfView.srcObject = stream;
} catch (err) {
console.error(err);
}
}
const listener = async (data) => {
try {
console.log(`Signaling message received ${JSON.stringify(data)}`)
if (data.body != null) {
var obj = JSON.parse(data.body)
if (obj.type == "desc") {
desc = obj;
// if we get an offer, we need to reply with an answer
if (desc.type === 'offer') {
await pc.setRemoteDescription(desc);
const stream =
await navigator.mediaDevices.getUserMedia(constraints);
stream.getTracks().forEach((track) =>
pc.addTrack(track, stream));
await pc.setLocalDescription(await pc.createAnswer());
let msg = { "id": to, "body": JSON.stringify({ "type": "desc", "body": pc.localDescription }) }
signaling.send(msg);
} else if (desc.type === 'answer') {
await pc.setRemoteDescription(desc);
} else {
console.log('Unsupported SDP type.');
}
} else if (obj.type == "candidate") {
await pc.addIceCandidate(obj);
}
}
} catch (err) {
console.error(err);
}
};
signaling.addListener(data => {
console.log("adding listener again")
listener(data).then(res => {
console.log("Finished")
});
});
start().then(result => console.log("started"));
</script>
</body>
</html>