Skip to content

Instantly share code, notes, and snippets.

@Sean-Der
Created September 30, 2022 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sean-Der/1cf72cec548d070a19d3f846e14221f6 to your computer and use it in GitHub Desktop.
Save Sean-Der/1cf72cec548d070a19d3f846e14221f6 to your computer and use it in GitHub Desktop.
Pion rtpdump to disk
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{})
if err != nil {
panic(err)
}
// Allow us to receive 1 audio track, and 1 video track
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}); err != nil {
panic(err)
} else if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}); err != nil {
panic(err)
}
f, err := os.OpenFile(rtpdumpFilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
panic(err)
}
writer, err := rtpdump.NewWriter(f, rtpdump.Header{
Start: time.Unix(9, 0),
Source: net.IPv4(2, 2, 2, 2),
Port: 2222,
})
if err != nil {
panic(err)
}
startTime := time.Now()
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
for {
rtpPacket, _, err := track.ReadRTP()
if err != nil {
panic(err)
}
rtpPacket.Header.PayloadType = 96
rtpPacket.Header.SSRC = 96
buff, err := rtpPacket.Marshal()
if err != nil {
panic(err)
}
if err := writer.WritePacket(rtpdump.Packet{
Offset: time.Since(startTime),
IsRTCP: false,
Payload: buff,
}); err != nil {
panic(err)
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment