Skip to content

Commit a8c02b0

Browse files
stephanrotolanteSean-Der
authored andcommitted
Add examples/whip-whep
Create WHIP/WHEP example works with OBS or browser Resolves #2499
1 parent 836184c commit a8c02b0

File tree

3 files changed

+326
-0
lines changed

3 files changed

+326
-0
lines changed

examples/whip-whep/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# whip-whep
2+
whip-whep demonstrates using WHIP and WHEP with Pion. Since WHIP+WHEP is standardized signaling you can publish via tools like OBS and GStreamer.
3+
You can then watch it in sub-second time from your browser, or pull the video back into OBS and GStreamer via WHEP.
4+
5+
Further details about the why and how of WHIP+WHEP are below the instructions.
6+
7+
## Instructions
8+
9+
### Download whip-whep
10+
11+
This example requires you to clone the repo since it is serving static HTML.
12+
13+
```
14+
git clone https://github.com/pion/webrtc.git
15+
cd webrtc/examples/whip-whep
16+
```
17+
18+
### Run whip-whep
19+
Execute `go run *.go`
20+
21+
### Publish
22+
23+
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.
24+
25+
To publish via OBS set `Service` to `WHIP` and `Server` to `http://localhost:8080/whip`. The `Bearer Token` can be whatever value you like.
26+
27+
28+
### Subscribe
29+
30+
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
31+
OBS or your browser.
32+
33+
Congrats, you have used Pion WebRTC! Now start building something cool
34+
35+
## Why WHIP/WHEP?
36+
37+
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.
38+
39+
For more info on WHIP/WHEP specification, feel free to read some of these great resources:
40+
- https://webrtchacks.com/webrtc-cracks-the-whip-on-obs/
41+
- https://datatracker.ietf.org/doc/draft-ietf-wish-whip/
42+
- https://datatracker.ietf.org/doc/draft-ietf-wish-whep/
43+
- https://bloggeek.me/whip-whep-webrtc-live-streaming

examples/whip-whep/index.html

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<html>
2+
3+
<!--
4+
SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
5+
SPDX-License-Identifier: MIT
6+
-->
7+
<head>
8+
<title>whip-whep</title>
9+
</head>
10+
11+
<body>
12+
<button onclick="window.doWHIP()">Publish</button>
13+
<button onclick="window.doWHEP()">Subscribe</button>
14+
<h3> Video </h3>
15+
<video id="videoPlayer" autoplay muted controls style="width: 500"> </video>
16+
17+
18+
<h3> ICE Connection States </h3>
19+
<div id="iceConnectionStates"></div> <br />
20+
</body>
21+
22+
<script>
23+
let peerConnection = new RTCPeerConnection()
24+
25+
peerConnection.oniceconnectionstatechange = () => {
26+
let el = document.createElement('p')
27+
el.appendChild(document.createTextNode(peerConnection.iceConnectionState))
28+
29+
document.getElementById('iceConnectionStates').appendChild(el);
30+
}
31+
32+
window.doWHEP = () => {
33+
peerConnection.addTransceiver('video', { direction: 'recvonly' })
34+
35+
peerConnection.ontrack = function (event) {
36+
document.getElementById('videoPlayer').srcObject = event.streams[0]
37+
}
38+
39+
peerConnection.createOffer().then(offer => {
40+
peerConnection.setLocalDescription(offer)
41+
42+
fetch(`/whep`, {
43+
method: 'POST',
44+
body: offer.sdp,
45+
headers: {
46+
Authorization: `Bearer none`,
47+
'Content-Type': 'application/sdp'
48+
}
49+
}).then(r => r.text())
50+
.then(answer => {
51+
peerConnection.setRemoteDescription({
52+
sdp: answer,
53+
type: 'answer'
54+
})
55+
})
56+
})
57+
}
58+
59+
window.doWHIP = () => {
60+
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
61+
.then(stream => {
62+
document.getElementById('videoPlayer').srcObject = stream
63+
stream.getTracks().forEach(track => peerConnection.addTrack(track, stream))
64+
65+
peerConnection.createOffer().then(offer => {
66+
peerConnection.setLocalDescription(offer)
67+
68+
fetch(`/whip`, {
69+
method: 'POST',
70+
body: offer.sdp,
71+
headers: {
72+
Authorization: `Bearer none`,
73+
'Content-Type': 'application/sdp'
74+
}
75+
}).then(r => r.text())
76+
.then(answer => {
77+
peerConnection.setRemoteDescription({
78+
sdp: answer,
79+
type: 'answer'
80+
})
81+
})
82+
})
83+
})
84+
}
85+
</script>
86+
</html>

examples/whip-whep/main.go

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
2+
// SPDX-License-Identifier: MIT
3+
4+
//go:build !js
5+
// +build !js
6+
7+
// whip-whep demonstrates how to use the WHIP/WHEP specifications to exhange SPD descriptions and stream media to a WebRTC client in the browser or OBS
8+
package main
9+
10+
import (
11+
"fmt"
12+
"io"
13+
"net/http"
14+
15+
"github.com/pion/interceptor"
16+
"github.com/pion/interceptor/pkg/intervalpli"
17+
"github.com/pion/webrtc/v4"
18+
)
19+
20+
// nolint: gochecknoglobals
21+
var (
22+
videoTrack *webrtc.TrackLocalStaticRTP
23+
24+
peerConnectionConfiguration = webrtc.Configuration{
25+
ICEServers: []webrtc.ICEServer{
26+
{
27+
URLs: []string{"stun:stun.l.google.com:19302"},
28+
},
29+
},
30+
}
31+
)
32+
33+
// nolint:gocognit
34+
func main() {
35+
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
36+
var err error
37+
if videoTrack, err = webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264}, "video", "pion"); err != nil {
38+
panic(err)
39+
}
40+
41+
http.Handle("/", http.FileServer(http.Dir(".")))
42+
http.HandleFunc("/whep", whepHandler)
43+
http.HandleFunc("/whip", whipHandler)
44+
45+
fmt.Println("Open http://localhost:8080 to access this demo")
46+
panic(http.ListenAndServe(":8080", nil)) // nolint: gosec
47+
}
48+
49+
func whipHandler(w http.ResponseWriter, r *http.Request) {
50+
// Read the offer from HTTP Request
51+
offer, err := io.ReadAll(r.Body)
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
// Create a MediaEngine object to configure the supported codec
57+
m := &webrtc.MediaEngine{}
58+
59+
// Setup the codecs you want to use.
60+
// We'll only use H264 but you can also define your own
61+
if err = m.RegisterCodec(webrtc.RTPCodecParameters{
62+
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
63+
PayloadType: 96,
64+
}, webrtc.RTPCodecTypeVideo); err != nil {
65+
panic(err)
66+
}
67+
68+
// Create a InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
69+
// This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
70+
// this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
71+
// for each PeerConnection.
72+
i := &interceptor.Registry{}
73+
74+
// Register a intervalpli factory
75+
// This interceptor sends a PLI every 3 seconds. A PLI causes a video keyframe to be generated by the sender.
76+
// This makes our video seekable and more error resilent, but at a cost of lower picture quality and higher bitrates
77+
// A real world application should process incoming RTCP packets from viewers and forward them to senders
78+
intervalPliFactory, err := intervalpli.NewReceiverInterceptor()
79+
if err != nil {
80+
panic(err)
81+
}
82+
i.Add(intervalPliFactory)
83+
84+
// Use the default set of Interceptors
85+
if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil {
86+
panic(err)
87+
}
88+
89+
// Create the API object with the MediaEngine
90+
api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i))
91+
92+
// Prepare the configuration
93+
94+
// Create a new RTCPeerConnection
95+
peerConnection, err := api.NewPeerConnection(peerConnectionConfiguration)
96+
if err != nil {
97+
panic(err)
98+
}
99+
100+
// Allow us to receive 1 video trac
101+
if _, err = peerConnection.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo); err != nil {
102+
panic(err)
103+
}
104+
105+
// Set a handler for when a new remote track starts, this handler saves buffers to disk as
106+
// an ivf file, since we could have multiple video tracks we provide a counter.
107+
// In your application this is where you would handle/process video
108+
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { //nolint: revive
109+
for {
110+
pkt, _, err := track.ReadRTP()
111+
if err != nil {
112+
panic(err)
113+
}
114+
115+
if err = videoTrack.WriteRTP(pkt); err != nil {
116+
panic(err)
117+
}
118+
}
119+
})
120+
121+
// Send answer via HTTP Response
122+
writeAnswer(w, peerConnection, offer, "/whip")
123+
}
124+
125+
func whepHandler(w http.ResponseWriter, r *http.Request) {
126+
// Read the offer from HTTP Request
127+
offer, err := io.ReadAll(r.Body)
128+
if err != nil {
129+
panic(err)
130+
}
131+
132+
// Create a new RTCPeerConnection
133+
peerConnection, err := webrtc.NewPeerConnection(peerConnectionConfiguration)
134+
if err != nil {
135+
panic(err)
136+
}
137+
138+
// Add Video Track that is being written to from WHIP Session
139+
rtpSender, err := peerConnection.AddTrack(videoTrack)
140+
if err != nil {
141+
panic(err)
142+
}
143+
144+
// Read incoming RTCP packets
145+
// Before these packets are returned they are processed by interceptors. For things
146+
// like NACK this needs to be called.
147+
go func() {
148+
rtcpBuf := make([]byte, 1500)
149+
for {
150+
if _, _, rtcpErr := rtpSender.Read(rtcpBuf); rtcpErr != nil {
151+
return
152+
}
153+
}
154+
}()
155+
156+
// Send answer via HTTP Response
157+
writeAnswer(w, peerConnection, offer, "/whep")
158+
}
159+
160+
func writeAnswer(w http.ResponseWriter, peerConnection *webrtc.PeerConnection, offer []byte, path string) {
161+
// Set the handler for ICE connection state
162+
// This will notify you when the peer has connected/disconnected
163+
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
164+
fmt.Printf("ICE Connection State has changed: %s\n", connectionState.String())
165+
166+
if connectionState == webrtc.ICEConnectionStateFailed {
167+
_ = peerConnection.Close()
168+
}
169+
})
170+
171+
if err := peerConnection.SetRemoteDescription(webrtc.SessionDescription{Type: webrtc.SDPTypeOffer, SDP: string(offer)}); err != nil {
172+
panic(err)
173+
}
174+
175+
// Create channel that is blocked until ICE Gathering is complete
176+
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
177+
178+
// Create answer
179+
answer, err := peerConnection.CreateAnswer(nil)
180+
if err != nil {
181+
panic(err)
182+
} else if err = peerConnection.SetLocalDescription(answer); err != nil {
183+
panic(err)
184+
}
185+
186+
// Block until ICE Gathering is complete, disabling trickle ICE
187+
// we do this because we only can exchange one signaling message
188+
// in a production application you should exchange ICE Candidates via OnICECandidate
189+
<-gatherComplete
190+
191+
// WHIP+WHEP expects a Location header and a HTTP Status Code of 201
192+
w.Header().Add("Location", path)
193+
w.WriteHeader(http.StatusCreated)
194+
195+
// Write Answer with Candidates as HTTP Response
196+
fmt.Fprint(w, peerConnection.LocalDescription().SDP)
197+
}

0 commit comments

Comments
 (0)