-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallee.html
More file actions
141 lines (115 loc) · 4.14 KB
/
callee.html
File metadata and controls
141 lines (115 loc) · 4.14 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
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<head>
<title>Basic camera</title>
<script type="text/javascript">
var peerConnection2;
var sdpConstraints = { 'mandatory': { 'OfferToReceiveAudio': true, 'OfferToReceiveVideo': true } };
let con = new WebSocket("ws://localhost:8080");
con.onopen = function (e) {
//some sort of registration
sendMessage(
JSON.stringify(
{
sender: "callee"
}
));
start();
}
function sendMessage(msg) {
con.send(msg);
}
con.onmessage = function (event) {
event.data.text().then(
txt => {
console.log(txt);
//alert(`[meData received from server: ${txt}`);
//gotDescription2(JSON.parse(txt));
routeMessage(JSON.parse(txt));
});
}
con.onclose = function (event) {
if (event.wasClean) {
alert(`[close] Connection closed cleanly, code=${event.reason}`);
} else {
// e.g. server proces
// event.code is usually 1006 in this case
alert('[close] Connection died');
}
};
con.onerror = function (error) {
alert(`[error] ${error.message}`);
};
function routeMessage(msg) {
if (msg.type == "ice-candidate") {
handleIceCandidate(msg.payload);
}
else if (msg.type == "offer-answer") {
}
else if (msg.type == "offer") {
gotRemoteDescription(msg.payload);
}
}
function gotRemoteStream(e) {
calleeVideo = document.getElementById("calleeVideo");
calleeVideo.srcObject = e.stream;
alert(e.stream.getVideoTracks().length);
//calleeVideo.play();
}
function start() {
servers = null; //{ iceServers: [ urls: "1123@localhost:8080"] };
peerConnection2 = new RTCPeerConnection(servers);
peerConnection2.onicecandidate = iceCallback;
peerConnection2.onaddstream = gotRemoteStream;
peerConnection2.ontrack = handleTrackEvent;
}
function handleTrackEvent(event) {
calleeVideo = document.getElementById("calleeVideo");
calleeVideo.srcObject = event.streams[0];
calleeVideo.play();
}
function gotRemoteDescription(desc) {
peerConnection2.setRemoteDescription(desc);
peerConnection2.createAnswer(gotLocalDescription, function () { alert('an error !'); }, sdpConstraints);
}
function gotLocalDescription(desc) {
peerConnection2.setLocalDescription(desc);
//SEND THE ANSWER HERE
sendMessage(JSON.stringify(
{
sender: "callee",
recipient: "caller",
type: "offer-answer",
payload: desc
}));
//peerConnection1.setRemoteDescription(desc);
}
function hangup() {
peerConnection2.close();
}
function handleIceCandidate(candidate) {
if (candidate) {
peerConnection2.addIceCandidate(new RTCIceCandidate(candidate));
}
}
function iceCallback(event) {
if (event.candidate) {
sendMessage(
JSON.stringify(
{
sender: "callee",
recipient: "caller",
type: "ice-candidate",
payload: event.candidate
}
)
)
//peerConnection1.addIceCandidate(new RTCIceCandidate(event.candidate));
}
}
</script>
</head>
<body>
<button id="hangup" onclick="hangup()">Hangup</button>
<video id="calleeVideo" autoplay="true" />
</body>