Skip to content

Commit 131e878

Browse files
committed
Rewrite send_from_file example
1 parent e72d499 commit 131e878

14 files changed

+416
-321
lines changed
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

examples/send_from_file/.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
send_from_file-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/
27+
28+
# Media files
29+
video.ivf
30+
audio.ogg

examples/send_from_file/README.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
# Send From File
1+
# Send from File
22

33
Send video and audio from files to a browser.
44

5-
1. Start `ex_ice/signalling_server` with `mix run --no-halt`
6-
2. Run `elixir example.exs`
7-
3. Visit `example.html` in your browser e.g. `file:///home/Repos/elixir-webrtc/ex_webrtc/examples/send_from_file/example.html`
8-
4. Press the play button.
5+
While in `examples/send_from_file` directory
96

10-
You can replace `video.ivf` or `audio.ogg` and use your own files instead.
7+
1. Generate media files
8+
9+
```shell
10+
ffmpeg -f lavfi -i testsrc=duration=5:size=640x480:rate=30 video.ivf
11+
ffmpeg -f lavfi -i "sine=frequency=420:duration=5" -c:a libopus audio.ogg
12+
```
13+
14+
You may use your own files, if they meet the requirements:
15+
* for video, it must be IVP in 30 FPS
16+
* for audio, it must be Ogg with a single Opus stream
17+
18+
2. Run `mix deps.get`
19+
3. Run `mix run --no-halt`
20+
4. Visit `http://127.0.0.1:8829` in your browser and press the `play` button.
21+
22+
The video and audio will loop infinitely.

examples/send_from_file/example.html examples/send_from_file/assets/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<main>
1111
<h1>Elixir WebRTC Send From File Example</h1>
1212
</main>
13-
<video id="videoPlayer" autoplay controls> </video>
14-
<script src="example.js"></script>
13+
<video id="videoPlayer" controls> </video>
14+
<script src="script.js"></script>
1515
</body>
1616
</html>
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const pcConfig = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' },] };
2+
const address = "ws://127.0.0.1:8829/ws"
3+
4+
const ws = new WebSocket(address);
5+
ws.onopen = _ => start_connection(ws);
6+
ws.onclose = event => console.log("WebSocket connection was terminated:", event);
7+
8+
const start_connection = async (ws) => {
9+
const videoPlayer = document.getElementById("videoPlayer");
10+
videoPlayer.srcObject = new MediaStream();
11+
12+
const pc = new RTCPeerConnection(pcConfig);
13+
pc.ontrack = event => videoPlayer.srcObject.addTrack(event.track);
14+
pc.onicecandidate = event => {
15+
if (event.candidate === null) return;
16+
17+
console.log("Sent ICE candidate:", event.candidate);
18+
ws.send(JSON.stringify({ type: "ice", data: event.candidate }));
19+
};
20+
21+
ws.onmessage = async event => {
22+
const {type, data} = JSON.parse(event.data);
23+
24+
switch (type) {
25+
case "offer":
26+
console.log("Received SDP offer:", data);
27+
await pc.setRemoteDescription(data)
28+
29+
const answer = await pc.createAnswer();
30+
await pc.setLocalDescription(answer);
31+
32+
console.log("Sent SDP answer:", answer);
33+
ws.send(JSON.stringify({type: "answer", data: answer}))
34+
break;
35+
case "ice":
36+
console.log("Recieved ICE candidate:", data);
37+
await pc.addIceCandidate(data);
38+
}
39+
};
40+
};

examples/send_from_file/audio.ogg

-26.7 KB
Binary file not shown.

examples/send_from_file/example.exs

-266
This file was deleted.

0 commit comments

Comments
 (0)