Skip to content

Commit 7ca94de

Browse files
author
Rain Liu
committed
add play-from-disk-renegotation example
1 parent 02d20dd commit 7ca94de

File tree

4 files changed

+486
-2
lines changed

4 files changed

+486
-2
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ env_logger = "0.8"
5252
clap = "2"
5353
hyper = { version = "0.14", features = ["full"] }
5454
signal = {path = "examples/signal" }
55+
tokio-util = "0.6.8"
5556

5657
[profile.dev]
5758
opt-level = 0
@@ -96,6 +97,11 @@ name = "play-from-disk"
9697
path = "examples/play-from-disk/play-from-disk.rs"
9798
bench = false
9899

100+
[[example]]
101+
name = "play-from-disk-renegotation"
102+
path = "examples/play-from-disk-renegotation/play-from-disk-renegotation.rs"
103+
bench = false
104+
99105
[[example]]
100106
name = "reflect"
101107
path = "examples/reflect/reflect.rs"

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ All examples are ported from [Pion](https://github.com/pion/webrtc/tree/master/e
77
#### Media API
88
- [x] [Reflect](reflect): The reflect example demonstrates how to have webrtc-rs send back to the user exactly what it receives using the same PeerConnection.
99
- [x] [Play from Disk](play-from-disk): The play-from-disk example demonstrates how to send video to your browser from a file saved to disk.
10-
- [ ] [Play from Disk Renegotation](play-from-disk-renegotation): The play-from-disk-renegotation example is an extension of the play-from-disk example, but demonstrates how you can add/remove video tracks from an already negotiated PeerConnection.
10+
- [x] [Play from Disk Renegotation](play-from-disk-renegotation): The play-from-disk-renegotation example is an extension of the play-from-disk example, but demonstrates how you can add/remove video tracks from an already negotiated PeerConnection.
1111
- [x] [Insertable Streams](insertable-streams): The insertable-streams example demonstrates how webrtc-rs can be used to send E2E encrypted video and decrypt via insertable streams in the browser.
1212
- [x] [Save to Disk](save-to-disk): The save-to-disk example shows how to record your webcam and save the footage to disk on the server side.
1313
- [x] [Broadcast](broadcast): The broadcast example demonstrates how to broadcast a video to multiple peers. A broadcaster uploads the video once and the server forwards it to all other peers.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<html>
2+
<head>
3+
<title>play-from-disk-renegotation</title>
4+
</head>
5+
6+
<body>
7+
<button onClick="window.addVideo()"> Add Video</button>
8+
<br/>
9+
<button onClick="window.removeVideo()"> Remove Video</button>
10+
<br/>
11+
12+
13+
<h3> Video </h3>
14+
<div id="remoteVideos"></div>
15+
<br/>
16+
17+
<h3> Logs </h3>
18+
<div id="logs"></div>
19+
</body>
20+
21+
<script>
22+
let activeVideos = 0
23+
let pc = new RTCPeerConnection({
24+
iceServers: [
25+
{
26+
urls: 'stun:stun.l.google.com:19302'
27+
}
28+
]
29+
})
30+
pc.ontrack = function (event) {
31+
var el = document.createElement(event.track.kind)
32+
el.srcObject = event.streams[0]
33+
el.autoplay = true
34+
el.controls = true
35+
36+
event.track.onmute = function (event) {
37+
el.parentNode.removeChild(el);
38+
}
39+
40+
document.getElementById('remoteVideos').appendChild(el)
41+
}
42+
43+
let doSignaling = method => {
44+
pc.createOffer()
45+
.then(offer => {
46+
pc.setLocalDescription(offer)
47+
48+
return fetch(`/${method}`, {
49+
method: 'post',
50+
headers: {
51+
'Accept': 'application/json, text/plain, */*',
52+
'Content-Type': 'application/json'
53+
},
54+
body: JSON.stringify(offer)
55+
})
56+
})
57+
.then(res => res.json())
58+
.then(res => pc.setRemoteDescription(res))
59+
.catch(alert)
60+
}
61+
62+
// Create a noop DataChannel. By default PeerConnections do not connect
63+
// if they have no media tracks or DataChannels
64+
pc.createDataChannel('noop')
65+
doSignaling('createPeerConnection')
66+
67+
window.addVideo = () => {
68+
if (pc.getTransceivers().length <= activeVideos) {
69+
pc.addTransceiver('video')
70+
activeVideos++
71+
}
72+
73+
doSignaling('addVideo')
74+
};
75+
76+
window.removeVideo = () => {
77+
doSignaling('removeVideo')
78+
};
79+
</script>
80+
</html>

0 commit comments

Comments
 (0)