Skip to content

Commit 08d015e

Browse files
committed
ConfigureFlexFEC03 helper and fec example
1 parent ca48a0d commit 08d015e

File tree

7 files changed

+533
-0
lines changed

7 files changed

+533
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# play-from-disk-fec
2+
play-from-disk-fec demonstrates how to use forward error correction (FlexFEC-03) while sending video to your Chrome-based browser from files saved to disk. The example is designed to drop 40% of the media packets, but browser will recover them using the FEC packets and the delivered packets.
3+
4+
## Instructions
5+
### Create IVF named `output.ivf` that contains a VP8/VP9/AV1 track
6+
```
7+
ffmpeg -i $INPUT_FILE -g 30 -b:v 2M output.ivf
8+
```
9+
10+
**Note**: In the `ffmpeg` command which produces the .ivf file, the argument `-b:v 2M` specifies the video bitrate to be 2 megabits per second. We provide this default value to produce decent video quality, but if you experience problems with this configuration (such as dropped frames etc.), you can decrease this. See the [ffmpeg documentation](https://ffmpeg.org/ffmpeg.html#Options) for more information on the format of the value.
11+
12+
### Download play-from-disk-fec
13+
14+
```
15+
go install github.com/pion/webrtc/v4/examples/play-from-disk-fec@latest
16+
```
17+
18+
### Open play-from-disk-fec example page
19+
Open [jsfiddle.net](https://jsfiddle.net/hgzwr9cm/) in your browser. You should see two text-areas and buttons for the offer-answer exchange.
20+
21+
### Run play-from-disk-fec to generate an offer
22+
The `output.ivf` you created should be in the same directory as `play-from-disk-fec`.
23+
24+
When you run play-from-disk-fec, it will generate an offer in base64 format and print it to stdout.
25+
26+
### Input play-from-disk-fec's offer into your browser
27+
Copy the base64 offer that `play-from-disk-fec` just emitted and paste it into the first text area in the jsfiddle (labeled "Remote Session Description")
28+
29+
### Hit 'Start Session' in jsfiddle to generate an answer
30+
Click the 'Start Session' button. This will process the offer and generate an answer, which will appear in the second text area.
31+
32+
### Save the browser's answer to a file
33+
Copy the base64-encoded answer from the second text area (labeled "Browser Session Description") and save it to a file named `answer.txt` in the same directory where you're running `play-from-disk-fec`.
34+
35+
### Press Enter to continue
36+
Once you've saved the answer to `answer.txt`, go back to the terminal where `play-from-disk-fec` is running and press Enter. The program will read the answer file and establish the connection.
37+
38+
### Enjoy your video!
39+
A video should start playing in your browser above the input boxes. `play-from-disk-fec` will exit when the file reaches the end
40+
41+
You can watch the stats about transmitted/dropped media & FEC packets in the stdout.
42+
43+
Congrats, you have used Pion WebRTC! Now start building something cool
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
3+
SPDX-License-Identifier: MIT
4+
*/
5+
textarea {
6+
width: 500px;
7+
min-height: 75px;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
3+
# SPDX-License-Identifier: MIT
4+
5+
name: play-from-disk-fec
6+
description: play-from-disk-fec demonstrates how to use forward error correction (FlexFEC-03) while sending video to your Chrome-based browser from files saved to disk.
7+
authors:
8+
- Aleksandr Alekseev
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
3+
SPDX-License-Identifier: MIT
4+
-->
5+
Remote Session Description (Paste offer from Go code here)
6+
<br/>
7+
<textarea id="remoteSessionDescription"></textarea>
8+
<br/>
9+
<button onclick="window.startSession()">Start Session</button>
10+
<br/>
11+
<br/>
12+
<br/>
13+
14+
Browser Session Description (Copy this to answer.txt file)
15+
<br/>
16+
<textarea id="localSessionDescription" readonly="true"></textarea>
17+
<br/>
18+
19+
<button onclick="window.copySessionDescription()">Copy browser Session Description to clipboard</button>
20+
21+
<br/>
22+
<br/>
23+
24+
Video
25+
<br/>
26+
<div id="remoteVideos"></div> <br />
27+
28+
Logs
29+
<br/>
30+
<div id="div"></div>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* eslint-env browser */
2+
3+
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
4+
// SPDX-License-Identifier: MIT
5+
6+
const pc = new RTCPeerConnection({
7+
iceServers: [
8+
{
9+
urls: 'stun:stun.l.google.com:19302'
10+
}
11+
]
12+
})
13+
const log = (msg) => {
14+
document.getElementById('div').innerHTML += msg + '<br>'
15+
}
16+
17+
pc.ontrack = function (event) {
18+
const el = document.createElement(event.track.kind)
19+
el.srcObject = event.streams[0]
20+
el.autoplay = true
21+
el.controls = true
22+
23+
document.getElementById('remoteVideos').appendChild(el)
24+
}
25+
26+
pc.oniceconnectionstatechange = (e) => log(pc.iceConnectionState)
27+
pc.onicecandidate = (event) => {
28+
if (event.candidate === null) {
29+
document.getElementById('localSessionDescription').value = btoa(
30+
JSON.stringify(pc.localDescription)
31+
)
32+
}
33+
}
34+
35+
window.startSession = () => {
36+
const sd = document.getElementById('remoteSessionDescription').value
37+
if (sd === '') {
38+
return alert('Session Description must not be empty')
39+
}
40+
41+
try {
42+
// Set the remote offer
43+
pc.setRemoteDescription(JSON.parse(atob(sd)))
44+
.then(() => {
45+
// Create answer
46+
return pc.createAnswer()
47+
})
48+
.then((answer) => {
49+
// Set local description with the answer
50+
return pc.setLocalDescription(answer)
51+
})
52+
.catch(log)
53+
} catch (e) {
54+
alert(e)
55+
}
56+
}
57+
58+
window.copySessionDescription = () => {
59+
const browserSessionDescription = document.getElementById(
60+
'localSessionDescription'
61+
)
62+
63+
browserSessionDescription.focus()
64+
browserSessionDescription.select()
65+
66+
try {
67+
const successful = document.execCommand('copy')
68+
const msg = successful ? 'successful' : 'unsuccessful'
69+
log('Copying SessionDescription was ' + msg)
70+
} catch (err) {
71+
log('Oops, unable to copy SessionDescription ' + err)
72+
}
73+
}

0 commit comments

Comments
 (0)