-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathmain.go
394 lines (339 loc) · 9.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
//go:build !js
// +build !js
// save-to-webm is a simple application that shows how to receive audio and video using Pion and then save to WebM container.
package main
import (
"bufio"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/signal"
"strings"
"time"
"github.com/at-wat/ebml-go/webm"
"github.com/pion/interceptor/pkg/jitterbuffer"
"github.com/pion/rtcp"
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/pkg/media/samplebuilder"
)
const (
naluTypeBitmask = 0b11111
naluTypeSPS = 7
)
func main() {
saver := newWebmSaver()
peerConnection := createWebRTCConn(saver)
closed := make(chan os.Signal, 1)
signal.Notify(closed, os.Interrupt)
<-closed
if err := peerConnection.Close(); err != nil {
panic(err)
}
saver.Close()
}
type webmSaver struct {
audioWriter, videoWriter webm.BlockWriteCloser
audioBuilder, vp8Builder *samplebuilder.SampleBuilder
audioTimestamp, videoTimestamp time.Duration
h264JitterBuffer *jitterbuffer.JitterBuffer
lastVideoTimestamp uint32
}
func newWebmSaver() *webmSaver {
return &webmSaver{
audioBuilder: samplebuilder.New(10, &codecs.OpusPacket{}, 48000),
vp8Builder: samplebuilder.New(10, &codecs.VP8Packet{}, 90000),
h264JitterBuffer: jitterbuffer.New(),
}
}
func (s *webmSaver) Close() {
fmt.Printf("Finalizing webm...\n")
if s.audioWriter != nil {
if err := s.audioWriter.Close(); err != nil {
panic(err)
}
}
if s.videoWriter != nil {
if err := s.videoWriter.Close(); err != nil {
panic(err)
}
}
}
func (s *webmSaver) PushOpus(rtpPacket *rtp.Packet) {
s.audioBuilder.Push(rtpPacket)
for {
sample := s.audioBuilder.Pop()
if sample == nil {
return
}
if s.audioWriter != nil {
s.audioTimestamp += sample.Duration
if _, err := s.audioWriter.Write(true, int64(s.audioTimestamp/time.Millisecond), sample.Data); err != nil {
panic(err)
}
}
}
}
func (s *webmSaver) PushH264(rtpPacket *rtp.Packet) {
s.h264JitterBuffer.Push(rtpPacket)
pkt, err := s.h264JitterBuffer.Peek(true)
if err != nil {
return
}
pkts := []*rtp.Packet{pkt}
for {
pkt, err = s.h264JitterBuffer.PeekAtSequence(pkts[len(pkts)-1].SequenceNumber + 1)
if err != nil {
return
}
// We have popped a whole frame, lets write it
if pkts[0].Timestamp != pkt.Timestamp {
break
}
pkts = append(pkts, pkt)
}
h264Packet := &codecs.H264Packet{}
data := []byte{}
for i := range pkts {
if _, err = s.h264JitterBuffer.PopAtSequence(pkts[i].SequenceNumber); err != nil {
panic(err)
}
out, err := h264Packet.Unmarshal(pkts[i].Payload)
if err != nil {
panic(err)
}
data = append(data, out...)
}
videoKeyframe := (data[4] & naluTypeBitmask) == naluTypeSPS
if s.videoWriter == nil && videoKeyframe {
if s.videoWriter == nil || s.audioWriter == nil {
s.InitWriter(true, 1280, 720)
}
}
samples := uint32(0)
if s.lastVideoTimestamp != 0 {
samples = pkts[0].Timestamp - s.lastVideoTimestamp
}
s.lastVideoTimestamp = pkts[0].Timestamp
if s.videoWriter != nil {
s.videoTimestamp += time.Duration(float64(samples) / float64(90000) * float64(time.Second))
if _, err := s.videoWriter.Write(videoKeyframe, int64(s.videoTimestamp/time.Millisecond), data); err != nil {
panic(err)
}
}
}
func (s *webmSaver) PushVP8(rtpPacket *rtp.Packet) {
s.vp8Builder.Push(rtpPacket)
for {
sample := s.vp8Builder.Pop()
if sample == nil {
return
}
// Read VP8 header.
videoKeyframe := (sample.Data[0]&0x1 == 0)
if videoKeyframe {
// Keyframe has frame information.
raw := uint(sample.Data[6]) | uint(sample.Data[7])<<8 | uint(sample.Data[8])<<16 | uint(sample.Data[9])<<24
width := int(raw & 0x3FFF)
height := int((raw >> 16) & 0x3FFF)
if s.videoWriter == nil || s.audioWriter == nil {
s.InitWriter(false, width, height)
}
}
if s.videoWriter != nil {
s.videoTimestamp += sample.Duration
if _, err := s.videoWriter.Write(videoKeyframe, int64(s.videoTimestamp/time.Millisecond), sample.Data); err != nil {
panic(err)
}
}
}
}
func (s *webmSaver) InitWriter(isH264 bool, width, height int) {
w, err := os.OpenFile("test.webm", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
panic(err)
}
videoMimeType := "V_VP8"
if isH264 {
videoMimeType = "V_MPEG4/ISO/AVC"
}
ws, err := webm.NewSimpleBlockWriter(w,
[]webm.TrackEntry{
{
Name: "Audio",
TrackNumber: 1,
TrackUID: 12345,
CodecID: "A_OPUS",
TrackType: 2,
DefaultDuration: 20000000,
Audio: &webm.Audio{
SamplingFrequency: 48000.0,
Channels: 2,
},
}, {
Name: "Video",
TrackNumber: 2,
TrackUID: 67890,
CodecID: videoMimeType,
TrackType: 1,
DefaultDuration: 33333333,
Video: &webm.Video{
PixelWidth: uint64(width),
PixelHeight: uint64(height),
},
},
})
if err != nil {
panic(err)
}
fmt.Printf("WebM saver has started with video width=%d, height=%d\n", width, height)
s.audioWriter = ws[0]
s.videoWriter = ws[1]
}
func createWebRTCConn(saver *webmSaver) *webrtc.PeerConnection {
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
// Prepare the configuration
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
// Create a MediaEngine object to configure the supported codec
m := &webrtc.MediaEngine{}
// Setup the codecs you want to use.
// This example supports VP8 or H264. Some browsers may only support one (or the other)
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 96,
}, webrtc.RTPCodecTypeVideo); err != nil {
panic(err)
}
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeH264, ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 98,
}, webrtc.RTPCodecTypeVideo); err != nil {
panic(err)
}
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
PayloadType: 111,
}, webrtc.RTPCodecTypeAudio); err != nil {
panic(err)
}
// Create the API object with the MediaEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
// Create a new RTCPeerConnection
peerConnection, err := api.NewPeerConnection(config)
if err != nil {
panic(err)
}
// Set a handler for when a new remote track starts, this handler copies inbound RTP packets,
// replaces the SSRC and sends them back
peerConnection.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
if track.Kind() == webrtc.RTPCodecTypeVideo {
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
go func() {
ticker := time.NewTicker(time.Second * 3)
for range ticker.C {
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
if errSend != nil {
fmt.Println(errSend)
}
}
}()
}
fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), track.Codec().RTPCodecCapability.MimeType)
for {
// Read RTP packets being sent to Pion
rtp, _, readErr := track.ReadRTP()
if readErr != nil {
if errors.Is(readErr, io.EOF) {
return
}
panic(readErr)
}
switch track.Codec().MimeType {
case webrtc.MimeTypeOpus:
saver.PushOpus(rtp)
case webrtc.MimeTypeVP8:
saver.PushVP8(rtp)
case webrtc.MimeTypeH264:
saver.PushH264(rtp)
}
}
})
// 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("Connection State has changed %s \n", connectionState.String())
})
// Wait for the offer to be pasted
offer := webrtc.SessionDescription{}
decode(readUntilNewline(), &offer)
// Set the remote SessionDescription
err = peerConnection.SetRemoteDescription(offer)
if err != nil {
panic(err)
}
// Create an answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
}
// Create channel that is blocked until ICE Gathering is complete
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
// Sets the LocalDescription, and starts our UDP listeners
err = peerConnection.SetLocalDescription(answer)
if 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
// Output the answer in base64 so we can paste it in browser
fmt.Println(encode(peerConnection.LocalDescription()))
return peerConnection
}
// Read from stdin until we get a newline
func readUntilNewline() (in string) {
var err error
r := bufio.NewReader(os.Stdin)
for {
in, err = r.ReadString('\n')
if err != nil && !errors.Is(err, io.EOF) {
panic(err)
}
if in = strings.TrimSpace(in); len(in) > 0 {
break
}
}
fmt.Println("")
return
}
// JSON encode + base64 a SessionDescription
func encode(obj *webrtc.SessionDescription) string {
b, err := json.Marshal(obj)
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(b)
}
// Decode a base64 and unmarshal JSON into a SessionDescription
func decode(in string, obj *webrtc.SessionDescription) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}
if err = json.Unmarshal(b, obj); err != nil {
panic(err)
}
}