-
Notifications
You must be signed in to change notification settings - Fork 64
/
sample.html
168 lines (148 loc) · 4.73 KB
/
sample.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
<!DOCTYPE html>
<html>
<head>
<title>WebRTC C++ sample</title>
</head>
<body>
<div>
<textarea id="stdout" cols="100" rows="32" readonly>output</textarea>
</div>
<div>
<button type="button" onclick="sdp1();">sdp1</button>
<button type="button" onclick="sdp2();">sdp2</button>
<button type="button" onclick="sdp3();">sdp3</button>
<button type="button" onclick="ice1();">ice1</button>
<button type="button" onclick="ice2();">ice2</button>
<button type="button" onclick="send();">send</button>
<button type="button" onclick="quit();">quit</button>
</div>
<div>
<textarea id="stdin" cols="100" rows="8"></textarea>
</div>
</body>
<script type="text/javascript">
var dataChannel = null;
var peerConnection = null;
var pcConfig = {
iceServers:[
{url:'stun:stun.l.google.com:19302'}
]
};
var iceArray = [];
function output(log) {
var stdout = document.getElementById('stdout');
stdout.value = stdout.value + log + '\n';
}
function input() {
var stdin = document.getElementById('stdin');
var input = stdin.value;
stdin.value = '';
return input;
}
function onDataChannel(evt) {
output('onDataChannel');
dataChannel = evt.channel;
setDataChannelEvents(dataChannel);
}
function onIceCandidate(evt) {
if (evt.candidate) {
iceArray.push(evt.candidate);
} else {
output("end of ice candidate" + evt.eventPhase);
}
}
function onIceConnectionStateChange(evt) {
output("onIceConnectionStateChange:" + peerConnection.iceConnectionState);
}
function setDataChannelEvents(dataChannel) {
dataChannel.onerror = function (error) {
ouptput('Data Channel onerror:' + error);
};
dataChannel.onmessage = function (event) {
output('Data Channel onmessage:' + event.data);
output(String.fromCharCode.apply("", new Uint8Array(event.data)));
};
dataChannel.onopen = function () {
output('Data Channel onopen');
};
dataChannel.onclose = function () {
output('Data Channel onclose');
};
}
function sdp1() {
function onOfferSuccess(sessionDescription) {
peerConnection.setLocalDescription(sessionDescription);
output('createOffer -> onOfferSuccess');
output('Offer SDP:begin');
output(sessionDescription.sdp);
output('Offer SDP:end');
}
function onOfferFailure() {
output('createOffer -> onOfferFailure');
}
peerConnection = new RTCPeerConnection(pcConfig);
peerConnection.onicecandidate = onIceCandidate;
peerConnection.ondatachannel = onDataChannel;
peerConnection.oniceconnectionstatechange = onIceConnectionStateChange;
var dataChannelOptions = {
ordered: true, // 順序を保証する
maxRetransmitTime: 3000 // ミリ秒
};
dataChannel = peerConnection.createDataChannel('data_channel', dataChannelOptions);
setDataChannelEvents(dataChannel);
peerConnection.createOffer(onOfferSuccess, onOfferFailure, null);
}
function sdp2() {
function onAnswerSuccess(sessionDescription) {
peerConnection.setLocalDescription(sessionDescription);
output('createAnswer -> onAnswerSuccess');
output('Answer SDP:begin');
output(sessionDescription.sdp);
output('Answer SDP:end');
}
function onAnswerFailure() {
output('createAnswer -> onAnswerFailure');
}
var sdp = new RTCSessionDescription({
type: 'offer',
sdp: input()
});
peerConnection = new RTCPeerConnection(pcConfig);
peerConnection.onicecandidate = onIceCandidate;
peerConnection.ondatachannel = onDataChannel;
peerConnection.oniceconnectionstatechange = onIceConnectionStateChange;
peerConnection.setRemoteDescription(sdp);
peerConnection.createAnswer(onAnswerSuccess, onAnswerFailure, null);
}
function sdp3() {
var sdp = new RTCSessionDescription({
type: 'answer',
sdp: input()
});
peerConnection.setRemoteDescription(sdp);
}
function ice1() {
output('ICE:begin');
output(JSON.stringify(iceArray));
iceArray = [];
output('ICE:end');
}
function ice2() {
var ices = JSON.parse(input());
for (var ice of ices) {
var iceObj = new RTCIceCandidate(ice);
peerConnection.addIceCandidate(iceObj);
}
}
function send() {
var data = input();
dataChannel.send(data);
}
function quit() {
dataChannel.close();
dataChannel = null;
peerConnection.close();
peerConnection = null;
}
</script>
</html>