Skip to content

Commit da3e0c5

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

File tree

3 files changed

+298
-0
lines changed

3 files changed

+298
-0
lines changed

examples/whip-whep/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# whip-whep
2+
whip-whep demonstrates how to use WHIP and WHEP with Pion.
3+
4+
5+
## Instructions
6+
### Download whip-whep
7+
```
8+
go install github.com/pion/webrtc/v4/examples/whip-whep@latest
9+
```
10+
11+
### Start http server
12+
```
13+
go run main.go
14+
```
15+
16+
### Send RTP to listening socket
17+
Now that you initialzed a client, you can use any software to send H264 encoded rtp packets to port 5004. We also have the pre made examples below
18+
19+
#### ffmpeg
20+
```
21+
ffmpeg -stream_loop -1 -i <your_input_source> -an -c:v libx264 -bsf:v h264_mp4toannexb -r 30 -f rtp udp://127.0.0.1:5004
22+
```
23+
24+
If you wish to send VP8 instead of H264 replace all occurrences of `h264` with VP8 in `main.go` then run
25+
26+
```
27+
ffmpeg -stream_loop -1 -i <your_input_source> -an -c:v libvpx -bsf:v h264_mp4toannexb -r 30 -f rtp udp://127.0.0.1:5004
28+
```
29+
30+
### OBS or Browser
31+
Here are two ways to view data from your local http server
32+
33+
34+
#### Initializing connection with OBS
35+
OBS supports publishing and injestion of data with WebRTC via the WHIP/WHEP exchange. Here is a quick demo on how to establish a WebRTC connection with you local http server and injest RTP data
36+
37+
https://github.com/stephanrotolante/webrtc/assets/28844768/42fdf64c-c3ab-436d-ad3c-951765020fcd
38+
39+
You simply have to choose WHIP as a source, point to your http server, fill out a beaerer token, and hit ok!
40+
41+
#### Initializing connection the browser
42+
Since the http server is started, open `http://localhost:8080` in your browser and a new WebRTC connection will be established
43+
44+
45+
For more info on WHIP/WHEP specification, feel free to read some of these great resources:
46+
- https://bloggeek.me/whip-whep-webrtc-live-streaming
47+
- https://www.ietf.org/archive/id/draft-murillo-whep-03.html

examples/whip-whep/index.html

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
<h3> Video </h3>
13+
<video id="videoPlayer" autoplay muted controls style="width: 500"> </video>
14+
15+
16+
<h3> ICE Connection States </h3>
17+
<div id="iceConnectionStates"></div> <br />
18+
</body>
19+
20+
<script>
21+
let peerConnection = new RTCPeerConnection()
22+
peerConnection.addTransceiver('video', { direction: 'recvonly' })
23+
24+
peerConnection.ontrack = function (event) {
25+
document.getElementById('videoPlayer').srcObject = event.streams[0]
26+
}
27+
28+
peerConnection.oniceconnectionstatechange = () => {
29+
let el = document.createElement('p')
30+
el.appendChild(document.createTextNode(peerConnection.iceConnectionState))
31+
32+
document.getElementById('iceConnectionStates').appendChild(el);
33+
}
34+
35+
peerConnection.createOffer().then(offer => {
36+
peerConnection.setLocalDescription(offer)
37+
38+
fetch(`/whep`, {
39+
method: 'POST',
40+
body: offer.sdp,
41+
headers: {
42+
Authorization: `Bearer none`,
43+
'Content-Type': 'application/sdp'
44+
}
45+
}).then(r => r.text())
46+
.then(answer => {
47+
peerConnection.setRemoteDescription({
48+
sdp: answer,
49+
type: 'answer'
50+
})
51+
})
52+
})
53+
</script>
54+
</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)