@@ -13,15 +13,11 @@ import (
13
13
"io"
14
14
"net/http"
15
15
16
- "github.com/pion/interceptor"
17
- "github.com/pion/interceptor/pkg/intervalpli"
18
16
"github.com/pion/webrtc/v4"
19
17
)
20
18
21
19
// nolint: gochecknoglobals
22
20
var (
23
- videoTrack * webrtc.TrackLocalStaticRTP
24
-
25
21
peerConnectionConfiguration = webrtc.Configuration {
26
22
ICEServers : []webrtc.ICEServer {
27
23
{
34
30
// nolint:gocognit
35
31
func main () {
36
32
// 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
-
44
33
http .Handle ("/" , http .FileServer (http .Dir ("." )))
45
34
http .HandleFunc ("/whep" , whepHandler )
46
35
http .HandleFunc ("/whip" , whipHandler )
@@ -56,73 +45,12 @@ func whipHandler(res http.ResponseWriter, req *http.Request) {
56
45
panic (err )
57
46
}
58
47
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
-
99
48
// Create a new RTCPeerConnection
100
- peerConnection , err := api .NewPeerConnection (peerConnectionConfiguration )
49
+ peerConnection , err := webrtc .NewPeerConnection (peerConnectionConfiguration )
101
50
if err != nil {
102
51
panic (err )
103
52
}
104
53
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
-
126
54
// Send answer via HTTP Response
127
55
writeAnswer (res , peerConnection , offer , "/whip" )
128
56
}
@@ -140,24 +68,6 @@ func whepHandler(res http.ResponseWriter, req *http.Request) {
140
68
panic (err )
141
69
}
142
70
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
-
161
71
// Send answer via HTTP Response
162
72
writeAnswer (res , peerConnection , offer , "/whep" )
163
73
}
0 commit comments