snapshot of https://github.com/pion/webrtc.git (main, 2026-09-15; blobs > 131072B omitted)
Some checks failed
REUSE Compliance Check / lint (push) Failing after 0s
Browser E2E / Test (push) Failing after 3s
Test / test (1.24) (push) Failing after 0s
Test / test-i386 (1.25) (push) Failing after 0s
Test / test-i386 (1.24) (push) Failing after 0s
Test / test-windows (1.25) (push) Failing after 0s
Test / test-windows (1.24) (push) Failing after 0s
Test / test (1.25) (push) Failing after 0s
Examples Tests / Test (push) Failing after 3s
Test / test-macos (1.25) (push) Failing after 0s
Test / test-macos (1.24) (push) Failing after 0s
Go mod tidy / tidy (push) Failing after 0s
Test / test-wasm (push) Failing after 0s
CodeQL / analyze (push) Failing after 1s
Fuzz / fuzz (push) Failing after 0s

This commit is contained in:
upstream-mirror
2026-09-15 20:06:07 +05:00
commit 8acb1a3a29
394 changed files with 78746 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
# whip-whep
whip-whep demonstrates using WHIP and WHEP with Pion. Since WHIP+WHEP is standardized signaling you can publish via tools like OBS and GStreamer.
You can then watch it in sub-second time from your browser, or pull the video back into OBS and GStreamer via WHEP.
Further details about the why and how of WHIP+WHEP are below the instructions.
## Instructions
### Download whip-whep
This example requires you to clone the repo since it is serving static HTML.
```
git clone https://github.com/pion/webrtc.git
cd webrtc/examples/whip-whep
```
### Run whip-whep
Execute `go run *.go`
### Publish
You can publish via an tool that supports WHIP or via your browser. To publish via your browser open [http://localhost:8080](http://localhost:8080), and press publish.
To publish via OBS set `Service` to `WHIP` and `Server` to `http://localhost:8080/whip`. The `Bearer Token` can be whatever value you like.
### Subscribe
Once you have started publishing open [http://localhost:8080](http://localhost:8080) and press the subscribe button. You can now view your video you published via
OBS or your browser.
Congrats, you have used Pion WebRTC! Now start building something cool
## Why WHIP/WHEP?
WHIP/WHEP mandates that a Offer is uploaded via HTTP. The server responds with a Answer. With this strong API contract WebRTC support can be added to tools like OBS.
For more info on WHIP/WHEP specification, feel free to read some of these great resources:
- https://webrtchacks.com/webrtc-cracks-the-whip-on-obs/
- https://datatracker.ietf.org/doc/draft-ietf-wish-whip/
- https://datatracker.ietf.org/doc/draft-ietf-wish-whep/
- https://bloggeek.me/whip-whep-webrtc-live-streaming

View File

@@ -0,0 +1,87 @@
<html>
<!--
SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
SPDX-License-Identifier: MIT
-->
<head>
<title>whip-whep</title>
</head>
<body>
<button onclick="window.doWHIP()">Publish</button>
<button onclick="window.doWHEP()">Subscribe</button>
<h3> Video </h3>
<video id="videoPlayer" autoplay muted controls style="width: 500"> </video>
<h3> ICE Connection States </h3>
<div id="iceConnectionStates"></div> <br />
</body>
<script>
let peerConnection = new RTCPeerConnection()
peerConnection.oniceconnectionstatechange = () => {
let el = document.createElement('p')
el.appendChild(document.createTextNode(peerConnection.iceConnectionState))
document.getElementById('iceConnectionStates').appendChild(el);
}
window.doWHEP = () => {
peerConnection.addTransceiver('video', { direction: 'recvonly' })
peerConnection.addTransceiver('audio', { direction: 'recvonly' })
peerConnection.ontrack = function (event) {
document.getElementById('videoPlayer').srcObject = event.streams[0]
}
peerConnection.createOffer().then(offer => {
peerConnection.setLocalDescription(offer)
fetch(`/whep`, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer none`,
'Content-Type': 'application/sdp'
}
}).then(r => r.text())
.then(answer => {
peerConnection.setRemoteDescription({
sdp: answer,
type: 'answer'
})
})
})
}
window.doWHIP = () => {
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(stream => {
document.getElementById('videoPlayer').srcObject = stream
stream.getTracks().forEach(track => peerConnection.addTrack(track, stream))
peerConnection.createOffer().then(offer => {
peerConnection.setLocalDescription(offer)
fetch(`/whip`, {
method: 'POST',
body: offer.sdp,
headers: {
Authorization: `Bearer none`,
'Content-Type': 'application/sdp'
}
}).then(r => r.text())
.then(answer => {
peerConnection.setRemoteDescription({
sdp: answer,
type: 'answer'
})
})
})
})
}
</script>
</html>

330
examples/whip-whep/main.go Normal file
View File

@@ -0,0 +1,330 @@
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// whip-whep demonstrates how to use the WHIP/WHEP specifications to exchange SPD descriptions
// and stream media to a WebRTC client in the browser or OBS.
package main
import (
"errors"
"fmt"
"io"
"net/http"
"github.com/pion/interceptor"
"github.com/pion/interceptor/pkg/intervalpli"
"github.com/pion/webrtc/v4"
)
// nolint: gochecknoglobals
var (
videoTrack *webrtc.TrackLocalStaticRTP
audioTrack *webrtc.TrackLocalStaticRTP
peerConnectionConfiguration = webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
)
// nolint:gocognit
func main() {
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
var err error
if videoTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeH264,
}, "video", "pion"); err != nil {
panic(err)
}
if audioTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeOpus,
}, "audio", "pion"); err != nil {
panic(err)
}
http.Handle("/", http.FileServer(http.Dir(".")))
http.HandleFunc("/whep", whepHandler)
http.HandleFunc("/whip", whipHandler)
fmt.Println("Open http://localhost:8080 to access this demo")
panic(http.ListenAndServe(":8080", nil)) // nolint: gosec
}
func whipHandler(res http.ResponseWriter, req *http.Request) { // nolint: cyclop
fmt.Printf("Request to %s, method = %s\n", req.URL, req.Method)
res.Header().Add("Access-Control-Allow-Origin", "*")
res.Header().Add("Access-Control-Allow-Methods", "POST")
res.Header().Add("Access-Control-Allow-Headers", "*")
res.Header().Add("Access-Control-Allow-Headers", "Authorization")
if req.Method == http.MethodOptions {
return
}
// Read the offer from HTTP Request
offer, err := io.ReadAll(req.Body)
if err != nil {
panic(err)
}
// Create a MediaEngine object to configure the supported codec
mediaEngine := &webrtc.MediaEngine{}
// Set up the codecs you want to use.
// We'll only use H264 and Opus but you can also define your own
if err = mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeH264, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil,
},
PayloadType: 96,
}, webrtc.RTPCodecTypeVideo); err != nil {
panic(err)
}
if err = mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 2, SDPFmtpLine: "", RTCPFeedback: nil,
},
PayloadType: 97,
}, webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
// Create an InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
// This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
// this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
// for each PeerConnection.
interceptorRegistry := &interceptor.Registry{}
// Register a intervalpli factory
// This interceptor sends a PLI every 3 seconds. A PLI causes a video keyframe to be generated by the sender.
// This makes our video seekable and more error resilent, but at a cost of lower picture quality and higher bitrates
// A real world application should process incoming RTCP packets from viewers and forward them to senders
intervalPliFactory, err := intervalpli.NewReceiverInterceptor()
if err != nil {
panic(err)
}
interceptorRegistry.Add(intervalPliFactory)
// Use the default set of Interceptors
if err = webrtc.RegisterDefaultInterceptors(mediaEngine, interceptorRegistry); err != nil {
panic(err)
}
// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine), webrtc.WithInterceptorRegistry(interceptorRegistry))
// Create a new RTCPeerConnection
peerConnection, err := api.NewPeerConnection(peerConnectionConfiguration)
if err != nil {
panic(err)
}
// Allow us to receive 1 video track and 1 audio track
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {
panic(err)
}
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
// Set a handler for when a new remote track starts, this handler saves buffers to disk as
// an ivf file, since we could have multiple video tracks we provide a counter.
// In your application this is where you would handle/process video
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
go func() {
for {
_, _, err := receiver.ReadRTCP()
if err != nil {
if errors.Is(err, io.EOF) {
fmt.Printf("***** EOF reading RTCP from publish peer connection\n")
break
}
panic(err)
}
}
}()
go func() {
for {
pkt, _, err := track.ReadRTP()
if err != nil {
if errors.Is(err, io.EOF) {
fmt.Printf("***** EOF reading RTP from publish peer connection\n")
break
}
panic(err)
}
// Strip any WHIP extensions before forwarding to WHEP
pkt.Header.Extensions = nil
pkt.Header.Extension = false
if track.Kind() == webrtc.RTPCodecTypeVideo {
if err = videoTrack.WriteRTP(pkt); err != nil {
panic(err)
}
} else if track.Kind() == webrtc.RTPCodecTypeAudio {
if err = audioTrack.WriteRTP(pkt); err != nil {
panic(err)
}
}
}
}()
})
// Send answer via HTTP Response
writeAnswer(res, peerConnection, offer, "/whip")
}
func whepHandler(res http.ResponseWriter, req *http.Request) { //nolint:cyclop
fmt.Printf("Request to %s, method = %s\n", req.URL, req.Method)
res.Header().Add("Access-Control-Allow-Origin", "*")
res.Header().Add("Access-Control-Allow-Methods", "POST")
res.Header().Add("Access-Control-Allow-Headers", "*")
res.Header().Add("Access-Control-Allow-Headers", "Authorization")
if req.Method == http.MethodOptions {
return
}
// Read the offer from HTTP Request
offer, err := io.ReadAll(req.Body)
if err != nil {
panic(err)
}
// Create a MediaEngine object to configure the supported codec
media := &webrtc.MediaEngine{}
// Set up the codecs you want to use.
if err = media.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeH264,
ClockRate: 90000,
Channels: 0,
SDPFmtpLine: "",
RTCPFeedback: nil,
},
PayloadType: 96,
}, webrtc.RTPCodecTypeVideo); err != nil {
panic(err)
}
if err = media.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypeOpus,
ClockRate: 48000,
Channels: 2,
SDPFmtpLine: "",
RTCPFeedback: nil,
},
PayloadType: 97,
}, webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
// Create an InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
ir := &interceptor.Registry{}
// Use the default set of Interceptors
if err = webrtc.RegisterDefaultInterceptors(media, ir); err != nil {
panic(err)
}
// We want TWCC in case the subscriber supports it
if err = webrtc.ConfigureTWCCHeaderExtensionSender(media, ir); err != nil {
panic(err)
}
// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(media), webrtc.WithInterceptorRegistry(ir))
// Create a new RTCPeerConnection
peerConnection, err := api.NewPeerConnection(peerConnectionConfiguration)
if err != nil {
panic(err)
}
// Add Video Track that is being written to from WHIP Session
rtpSenderVideo, err := peerConnection.AddTrack(videoTrack)
if err != nil {
panic(err)
}
// Add Audio Track that is being written to from WHIP Session
rtpSenderAudio, err := peerConnection.AddTrack(audioTrack)
if err != nil {
panic(err)
}
// Read incoming RTCP packets for video
// Before these packets are returned they are processed by interceptors. For things
// like NACK this needs to be called.
go func() {
rtcpBuf := make([]byte, 1500)
for {
if _, _, rtcpErr := rtpSenderVideo.Read(rtcpBuf); rtcpErr != nil {
return
}
}
}()
// Read incoming RTCP packets for audio
go func() {
rtcpBuf := make([]byte, 1500)
for {
if _, _, rtcpErr := rtpSenderAudio.Read(rtcpBuf); rtcpErr != nil {
return
}
}
}()
// Send answer via HTTP Response
writeAnswer(res, peerConnection, offer, "/whep")
}
func writeAnswer(res http.ResponseWriter, peerConnection *webrtc.PeerConnection, offer []byte, path string) {
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
if connectionState == webrtc.ICEConnectionStateFailed {
_ = peerConnection.Close()
}
})
if err := peerConnection.SetRemoteDescription(webrtc.SessionDescription{
Type: webrtc.SDPTypeOffer, SDP: string(offer),
}); err != nil {
panic(err)
}
// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
// Create answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
} else if err = peerConnection.SetLocalDescription(answer); err != nil {
panic(err)
}
// Block until ICE Gathering is complete, disabling trickle ICE
// we do this because we only can exchange one signaling message
// in a production application you should exchange ICE Candidates via OnICECandidate
<-gatherComplete
// WHIP+WHEP expects a Location header and a HTTP Status Code of 201
res.Header().Add("Location", path)
res.WriteHeader(http.StatusCreated)
// Write Answer with Candidates as HTTP Response
fmt.Fprint(res, peerConnection.LocalDescription().SDP) //nolint: errcheck
}