@@ -13,15 +13,11 @@ import (
1313 "io"
1414 "net/http"
1515
16- "github.com/pion/interceptor"
17- "github.com/pion/interceptor/pkg/intervalpli"
1816 "github.com/pion/webrtc/v4"
1917)
2018
2119// nolint: gochecknoglobals
2220var (
23- videoTrack * webrtc.TrackLocalStaticRTP
24-
2521 peerConnectionConfiguration = webrtc.Configuration {
2622 ICEServers : []webrtc.ICEServer {
2723 {
3430// nolint:gocognit
3531func main () {
3632 // Everything below is the Pion WebRTC API! Thanks for using it ❤️.
37- var err error
38- if videoTrack , err = webrtc .NewTrackLocalStaticRTP (webrtc.RTPCodecCapability {
39- MimeType : webrtc .MimeTypeH264 ,
40- }, "video" , "pion" ); err != nil {
41- panic (err )
42- }
43-
4433 http .Handle ("/" , http .FileServer (http .Dir ("." )))
4534 http .HandleFunc ("/whep" , whepHandler )
4635 http .HandleFunc ("/whip" , whipHandler )
@@ -56,73 +45,12 @@ func whipHandler(res http.ResponseWriter, req *http.Request) {
5645 panic (err )
5746 }
5847
59- // Create a MediaEngine object to configure the supported codec
60- mediaEngine := & webrtc.MediaEngine {}
61-
62- // Setup the codecs you want to use.
63- // We'll only use H264 but you can also define your own
64- if err = mediaEngine .RegisterCodec (webrtc.RTPCodecParameters {
65- RTPCodecCapability : webrtc.RTPCodecCapability {
66- MimeType : webrtc .MimeTypeH264 , ClockRate : 90000 , Channels : 0 , SDPFmtpLine : "" , RTCPFeedback : nil ,
67- },
68- PayloadType : 96 ,
69- }, webrtc .RTPCodecTypeVideo ); err != nil {
70- panic (err )
71- }
72-
73- // Create a InterceptorRegistry. This is the user configurable RTP/RTCP Pipeline.
74- // This provides NACKs, RTCP Reports and other features. If you use `webrtc.NewPeerConnection`
75- // this is enabled by default. If you are manually managing You MUST create a InterceptorRegistry
76- // for each PeerConnection.
77- interceptorRegistry := & interceptor.Registry {}
78-
79- // Register a intervalpli factory
80- // This interceptor sends a PLI every 3 seconds. A PLI causes a video keyframe to be generated by the sender.
81- // This makes our video seekable and more error resilent, but at a cost of lower picture quality and higher bitrates
82- // A real world application should process incoming RTCP packets from viewers and forward them to senders
83- intervalPliFactory , err := intervalpli .NewReceiverInterceptor ()
84- if err != nil {
85- panic (err )
86- }
87- interceptorRegistry .Add (intervalPliFactory )
88-
89- // Use the default set of Interceptors
90- if err = webrtc .RegisterDefaultInterceptors (mediaEngine , interceptorRegistry ); err != nil {
91- panic (err )
92- }
93-
94- // Create the API object with the MediaEngine
95- api := webrtc .NewAPI (webrtc .WithMediaEngine (mediaEngine ), webrtc .WithInterceptorRegistry (interceptorRegistry ))
96-
97- // Prepare the configuration
98-
9948 // Create a new RTCPeerConnection
100- peerConnection , err := api .NewPeerConnection (peerConnectionConfiguration )
49+ peerConnection , err := webrtc .NewPeerConnection (peerConnectionConfiguration )
10150 if err != nil {
10251 panic (err )
10352 }
10453
105- // Allow us to receive 1 video trac
106- if _ , err = peerConnection .AddTransceiverFromKind (webrtc .RTPCodecTypeVideo ); err != nil {
107- panic (err )
108- }
109-
110- // Set a handler for when a new remote track starts, this handler saves buffers to disk as
111- // an ivf file, since we could have multiple video tracks we provide a counter.
112- // In your application this is where you would handle/process video
113- peerConnection .OnTrack (func (track * webrtc.TrackRemote , receiver * webrtc.RTPReceiver ) { //nolint: revive
114- for {
115- pkt , _ , err := track .ReadRTP ()
116- if err != nil {
117- panic (err )
118- }
119-
120- if err = videoTrack .WriteRTP (pkt ); err != nil {
121- panic (err )
122- }
123- }
124- })
125-
12654 // Send answer via HTTP Response
12755 writeAnswer (res , peerConnection , offer , "/whip" )
12856}
@@ -140,24 +68,6 @@ func whepHandler(res http.ResponseWriter, req *http.Request) {
14068 panic (err )
14169 }
14270
143- // Add Video Track that is being written to from WHIP Session
144- rtpSender , err := peerConnection .AddTrack (videoTrack )
145- if err != nil {
146- panic (err )
147- }
148-
149- // Read incoming RTCP packets
150- // Before these packets are returned they are processed by interceptors. For things
151- // like NACK this needs to be called.
152- go func () {
153- rtcpBuf := make ([]byte , 1500 )
154- for {
155- if _ , _ , rtcpErr := rtpSender .Read (rtcpBuf ); rtcpErr != nil {
156- return
157- }
158- }
159- }()
160-
16171 // Send answer via HTTP Response
16272 writeAnswer (res , peerConnection , offer , "/whep" )
16373}
0 commit comments