Skip to content

Commit 98a0025

Browse files
committed
Add support for AV1 and VP9 to play from disk
1 parent 8f8c232 commit 98a0025

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

Diff for: examples/play-from-disk-renegotiation/README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@ git clone https://github.com/pion/webrtc.git
1313
cd webrtc/examples/play-from-disk-renegotiation
1414
```
1515

16-
### Create IVF named `output.ivf` that contains a VP8 track
16+
### Create IVF named `output.ivf` that contains a VP8, VP9 or AV1 track
1717

18+
To encode video to VP8:
1819
```
19-
ffmpeg -i $INPUT_FILE -g 30 -b:v 2M output.ivf
20+
ffmpeg -i $INPUT_FILE -c:v libvpx -g 30 -b:v 2M output.ivf
21+
```
22+
23+
alternatively, to encode video to AV1 (Note: AV1 is CPU intensive, you may need to adjust `-cpu-used`):
24+
```
25+
ffmpeg -i $INPUT_FILE -c:v libaom-av1 -cpu-used 8 -g 30 -b:v 2M output.ivf
26+
```
27+
28+
Or to encode video to VP9:
29+
```
30+
ffmpeg -i $INPUT_FILE -c:v libvpx-vp9 -cpu-used 4 -g 30 -b:v 2M output.ivf
31+
```
32+
33+
If you have a VP8, VP9 or AV1 file in a different container you can use `ffmpeg` to mux it into IVF:
34+
```
35+
ffmpeg -i $INPUT_FILE -c:v copy -an output.ivf
2036
```
2137

2238
**Note**: In the `ffmpeg` command, 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.

Diff for: examples/play-from-disk-renegotiation/main.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,32 @@ func createPeerConnection(res http.ResponseWriter, req *http.Request) {
7171
}
7272

7373
// Add a single video track.
74-
func addVideo(res http.ResponseWriter, req *http.Request) {
74+
func addVideo(res http.ResponseWriter, req *http.Request) { //nolint:cyclop
75+
// Open a IVF file and start reading using our IVFReader
76+
file, err := os.Open("output.ivf")
77+
if err != nil {
78+
panic(err)
79+
}
80+
81+
ivf, header, err := ivfreader.NewWith(file)
82+
if err != nil {
83+
panic(err)
84+
}
85+
86+
var mimeType string
87+
switch header.FourCC {
88+
case "VP80":
89+
mimeType = webrtc.MimeTypeVP8
90+
case "VP90":
91+
mimeType = webrtc.MimeTypeVP9
92+
case "AV01":
93+
mimeType = webrtc.MimeTypeAV1
94+
default:
95+
panic(fmt.Sprintf("unsupported codec: %s", header.FourCC))
96+
}
97+
7598
videoTrack, err := webrtc.NewTrackLocalStaticSample(
76-
webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8},
99+
webrtc.RTPCodecCapability{MimeType: mimeType},
77100
fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()),
78101
fmt.Sprintf("video-%d", randutil.NewMathRandomGenerator().Uint32()),
79102
)
@@ -97,9 +120,9 @@ func addVideo(res http.ResponseWriter, req *http.Request) {
97120
}
98121
}()
99122

100-
go writeVideoToTrack(videoTrack)
101123
doSignaling(res, req)
102124
fmt.Println("Video track has been added")
125+
go writeVideoToTrack(ivf, header, videoTrack)
103126
}
104127

105128
// Remove a single sender.
@@ -163,18 +186,9 @@ func main() {
163186

164187
// Read a video file from disk and write it to a webrtc.Track
165188
// When the video has been completely read this exits without error.
166-
func writeVideoToTrack(track *webrtc.TrackLocalStaticSample) {
167-
// Open a IVF file and start reading using our IVFReader
168-
file, err := os.Open("output.ivf")
169-
if err != nil {
170-
panic(err)
171-
}
172-
173-
ivf, header, err := ivfreader.NewWith(file)
174-
if err != nil {
175-
panic(err)
176-
}
177-
189+
func writeVideoToTrack(
190+
ivf *ivfreader.IVFReader, header *ivfreader.IVFFileHeader, track *webrtc.TrackLocalStaticSample,
191+
) {
178192
// Send our video file frame at a time. Pace our sending so we send it at the same speed it should be played back as.
179193
// This isn't required since the video is timestamped, but we will such much higher loss if we send all at once.
180194
//

0 commit comments

Comments
 (0)