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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WEBRTC DEMO</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<div id="local">
<video autoplay controls @click="start"></video>
<Controls @change="(it) => { localControls = it }" />
</div>
<div id="remote">
<video autoplay controls></video>
<Controls @change="(it) => { remoteControls = it }" />
</div>
</div>
</body>
</html>
<template id="Controls">
<div class="controls">
<div class="item">
<span>server:</span>
<input v-model="server" type="text" value="localhost" />
</div>
<div class="item">
<span>transport:</span>
<select v-model="transport">
<option value="tcp">tcp</option>
<option value="udp">udp</option>
</select>
</div>
<div class="item">
<span>auth:</span>
<select v-model="username">
<option value="user1">user1:test</option>
<option value="user2">user2:test</option>
</select>
</div>
</div>
</template>
<script>
const { createApp, ref, watch } = Vue;
const app = createApp({
setup() {
const localControls = ref({});
const remoteControls = ref({});
return {
localControls,
remoteControls,
async start() {
const localVideo = document.querySelector("#local video");
const remoteVideo = document.querySelector("#remote video");
console.log({
urls: `turn:${localControls.value.server}:3478?transport=${localControls.value.transport}`,
username: localControls.value.username,
credential: "test",
});
/**
* This is a local RTC connection that forces the use of forwarding.
* you can configure the ice server parameters yourself.
*/
let localRtc = new RTCPeerConnection({
iceTransportPolicy: "relay",
iceServers: [
{
urls: `turn:${localControls.value.server}:3478?transport=${localControls.value.transport}`,
username: localControls.value.username,
credential: "test",
},
],
});
/**
* This is a remote RTC connection that forces the use of forwarding.
* you can configure the parameters of the ice server yourself.
*/
let remoteRtc = new RTCPeerConnection({
iceTransportPolicy: "relay",
iceServers: [
{
urls: `turn:${remoteControls.value.server}:3478?transport=${remoteControls.value.transport}`,
username: remoteControls.value.username,
credential: "test",
},
],
});
/**
* The local ice candidate is added to the remote session.
*/
localRtc.addEventListener("icecandidate", async (event) => {
await remoteRtc.addIceCandidate(event.candidate);
});
/**
* The remote ice candidate is added to the local session.
*/
remoteRtc.addEventListener("icecandidate", async (event) => {
await localRtc.addIceCandidate(event.candidate);
});
/**
* Create a media stream and add all audio and video tracks received
* by the remote session to this media stream.
*/
{
const remoteStream = new MediaStream();
remoteVideo.srcObject = remoteStream;
remoteRtc.addEventListener("track", (event) => {
remoteStream.addTrack(event.track);
});
}
/**
* Get the capture stream of the desktop or tab, here it is
* capturing video not audio.
*/
const stream = await navigator.mediaDevices.getDisplayMedia({
video: true,
});
/**
* The local video player previews the captured stream.
*/
localVideo.srcObject = stream;
/**
* The local capture streams are all added to the local RTC
* session.
*/
for (const track of stream.getTracks()) {
localRtc.addTrack(track, stream);
}
/**
* The local connection creates the OFFER to submit to the
* remote connection.
*/
const offer = await localRtc.createOffer();
await localRtc.setLocalDescription(offer);
await remoteRtc.setRemoteDescription(offer);
/**
* Remote connections create ANSWER submissions to the local
* connection.
*/
const answer = await remoteRtc.createAnswer();
await remoteRtc.setLocalDescription(answer);
await localRtc.setRemoteDescription(answer);
},
};
},
});
app.component("Controls", {
template: document.getElementById("Controls").innerHTML,
emits: ["change"],
setup(props, context) {
const server = ref("127.0.0.1");
const transport = ref("udp");
const username = ref("user1");
context.emit("change", { server, transport, username });
watch([server, transport, username], () => {
context.emit("change", { server, transport, username });
});
return {
server,
transport,
username,
};
},
});
app.mount("#app");
</script>
<style>
* {
margin: 0;
padding: 0;
font-size: 12px;
}
#app {
display: flex;
}
#local,
#remote {
width: 50%;
height: 100vh;
display: flex;
flex-direction: column;
}
#local video,
#remote video {
flex: 1;
width: 100%;
}
.controls {
display: flex;
padding: 10px;
}
.controls .item {
flex: 1;
}
.controls .item span {
font-style: oblique;
}
.controls .item input,
.controls .item select,
.controls .item option {
border: 1px solid #ddd;
padding: 3px 5px;
border-radius: 3px;
}
</style>