Skip to content

Commit 50f6f5e

Browse files
authored
feat: datachannel forwarding (#312)
* feat: datachannel forwarding * simplify * check ready state * handle collisions + reserved labels * lint fix * fix examples
1 parent a405fa7 commit 50f6f5e

File tree

9 files changed

+153
-290
lines changed

9 files changed

+153
-290
lines changed

examples/echotest/index.html

+61-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ <h3>Pion</h3>
6363
</div>
6464
</div>
6565

66+
<div class="row">
67+
<div class="col-12 pt-4">Media</div>
68+
</div>
6669
<div class="row">
6770
<div class="col-6 pt-2">
6871
<span
@@ -263,11 +266,50 @@ <h3>Pion</h3>
263266
<pre
264267
id="api"
265268
class="d-block border"
266-
style="background-color: #f8f9fa; height: 117px"
269+
style="
270+
background-color: #f8f9fa;
271+
height: 117px;
272+
width: 320px;
273+
margin: 5px 0;
274+
"
267275
></pre>
268276
</div>
269277
</div>
270278
</div>
279+
<div class="row">
280+
<div class="col-12 pt-4">Data</div>
281+
</div>
282+
<div class="row">
283+
<div class="col-6 pt-2">
284+
<textarea
285+
id="local-data"
286+
class="d-block border"
287+
style="
288+
background-color: #f8f9fa;
289+
height: 117px;
290+
width: 320px;
291+
margin: 5px 0;
292+
padding: 5px;
293+
"
294+
placeholder="Send a message"
295+
></textarea>
296+
<button type="button" class="btn btn-primary" onclick="send()">
297+
send
298+
</button>
299+
</div>
300+
<div class="col-6 pt-2">
301+
<pre
302+
id="remote-data"
303+
class="d-block border"
304+
style="
305+
background-color: #f8f9fa;
306+
height: 117px;
307+
width: 320px;
308+
margin: 5px 0;
309+
"
310+
></pre>
311+
</div>
312+
</div>
271313
</div>
272314
<!-- Optional JavaScript -->
273315
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
@@ -286,13 +328,16 @@ <h3>Pion</h3>
286328
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
287329
crossorigin="anonymous"
288330
></script>
289-
<script src="https://unpkg.com/ion-sdk-js@1.4.1/dist/ion-sdk.min.js"></script>
331+
<script src="https://unpkg.com/ion-sdk-js@1.5.0/dist/ion-sdk.min.js"></script>
290332
<script>
291333
const localVideo = document.getElementById("local-video");
292334
const remoteVideo = document.getElementById("remote-video");
335+
const localData = document.getElementById("local-data");
336+
const remoteData = document.getElementById("remote-data");
293337
const sizeTag = document.getElementById("size-tag");
294338
const brTag = document.getElementById("br-tag");
295339
let simulcast = false;
340+
let localDataChannel;
296341

297342
remoteVideo.addEventListener("loadedmetadata", function () {
298343
sizeTag.innerHTML = `${remoteVideo.videoWidth}x${remoteVideo.videoHeight}`;
@@ -325,14 +370,21 @@ <h3>Pion</h3>
325370
})
326371
.then((media) => {
327372
localStream = media;
328-
localVideo.srcObject = media.stream;
373+
localVideo.srcObject = media;
329374
localVideo.autoplay = true;
330375
localVideo.controls = true;
331376
localVideo.muted = true;
332377
joinBtns.style.display = "none";
333378
clientLocal.publish(media);
334379
})
335380
.catch(console.error);
381+
localDataChannel = clientLocal.createDataChannel("data");
382+
};
383+
384+
const send = () => {
385+
if (localDataChannel.readyState === "open") {
386+
localDataChannel.send(localData.value);
387+
}
336388
};
337389

338390
let remoteStream;
@@ -356,6 +408,12 @@ <h3>Pion</h3>
356408
}
357409
};
358410

411+
clientRemote.ondatachannel = ({ channel }) => {
412+
channel.onmessage = ({ data }) => {
413+
remoteData.innerHTML = data;
414+
};
415+
};
416+
359417
const api = {
360418
streamId: "",
361419
video: "high",

examples/pubsubtest/index.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ <h3>Pion</h3>
134134
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
135135
crossorigin="anonymous"
136136
></script>
137-
<script src="https://unpkg.com/ion-sdk-js@1.1.1/dist/ion-sdk.min.js"></script>
137+
<script src="https://unpkg.com/ion-sdk-js@1.5.0/dist/ion-sdk.min.js"></script>
138138
<script>
139139
const localVideo = document.getElementById("local-video");
140140
const remotesDiv = document.getElementById("remotes");
@@ -158,18 +158,18 @@ <h3>Pion</h3>
158158

159159
let localStream;
160160
const start = () => {
161-
clientLocal.getUserMedia({
161+
IonSDK.LocalStream.getUserMedia({
162162
resolution: "vga",
163163
audio: true,
164164
})
165165
.then((media) => {
166166
localStream = media;
167-
localVideo.srcObject = media.stream;
167+
localVideo.srcObject = media;
168168
localVideo.autoplay = true;
169169
localVideo.controls = true;
170170
localVideo.muted = true;
171171
joinBtns.style.display = "none";
172-
media.publish(media);
172+
clientLocal.publish(media);
173173
})
174174
.catch(console.error);
175175
};

examples/racetest/sdk.html renamed to examples/racetest/index.html

+1-9
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ <h3>Pion</h3>
7373
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
7474
crossorigin="anonymous"
7575
></script>
76-
<script src="https://unpkg.com/ion-sdk-js@1.0.20/dist/ion-sdk.min.js"></script>
76+
<script src="https://unpkg.com/ion-sdk-js@1.5.0/dist/ion-sdk.min.js"></script>
7777
<script>
7878
const localVideo = document.getElementById("local-video");
7979
const remotesDiv = document.getElementById("remotes");
@@ -82,14 +82,6 @@ <h3>Pion</h3>
8282
const joinBtn = document.getElementById("join-btn");
8383
const publishBtn = document.getElementById("publish-btn");
8484

85-
const config = {
86-
iceServers: [
87-
{
88-
urls: "stun:stun.l.google.com:19302",
89-
},
90-
],
91-
};
92-
9385
class Peer {
9486
constructor() {
9587
this.signal = new IonSDK.IonSFUJSONRPCSignal(

0 commit comments

Comments
 (0)