-
Notifications
You must be signed in to change notification settings - Fork 3
/
rtmp.go
530 lines (440 loc) · 12.9 KB
/
rtmp.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"image"
"image/jpeg"
"io"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/Glimesh/go-fdkaac/fdkaac"
"github.com/Glimesh/rtmp-ingest/pkg/h264"
"github.com/Glimesh/rtmp-ingest/pkg/protocols/ftl"
"github.com/Glimesh/rtmp-ingest/pkg/services"
h264joy "github.com/nareix/joy5/codec/h264"
"github.com/pion/rtp/v2"
"github.com/pion/rtp/v2/codecs"
"github.com/sirupsen/logrus"
flvtag "github.com/yutopp/go-flv/tag"
"github.com/yutopp/go-rtmp"
rtmpmsg "github.com/yutopp/go-rtmp/message"
opus "gopkg.in/hraban/opus.v2"
)
const (
FTL_MTU = 1392
FTL_VIDEO_PT = 96
FTL_AUDIO_PT = 97
// Realistic 8000Kbps
BANDWIDTH_LIMIT = 8000 * 1000
)
func NewRTMPServer(streamManager StreamManager, log logrus.FieldLogger, debugVideo bool) {
log.Info("Starting RTMP Server on :1935")
tcpAddr, err := net.ResolveTCPAddr("tcp", ":1935")
if err != nil {
log.Fatal(err)
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatal(err)
}
srv := rtmp.NewServer(&rtmp.ServerConfig{
OnConnect: func(conn net.Conn) (io.ReadWriteCloser, *rtmp.ConnConfig) {
return conn, &rtmp.ConnConfig{
Handler: &ConnHandler{
manager: streamManager,
log: log,
stopMetadataCollection: make(chan bool, 1),
debugSaveVideo: debugVideo,
},
ControlState: rtmp.StreamControlStateConfig{
DefaultBandwidthWindowSize: 6 * 1024 * 1024 / 8,
DefaultBandwidthLimitType: rtmpmsg.LimitTypeSoft,
},
Logger: log.WithField("app", "yutopp/go-rtmp"),
}
},
})
if err := srv.Serve(listener); err != nil {
log.Fatal(err)
}
}
type ConnHandler struct {
rtmp.DefaultHandler
manager StreamManager
log logrus.FieldLogger
channelID ftl.ChannelID
streamID ftl.StreamID
streamKey []byte
started bool
authenticated bool
errored bool
metadataFailures int
stream *Stream
videoSequencer rtp.Sequencer
videoPacketizer rtp.Packetizer
videoClockRate uint32
audioSequencer rtp.Sequencer
audioPacketizer rtp.Packetizer
audioClockRate uint32
audioDecoder *fdkaac.AacDecoder
audioBuffer []byte
audioEncoder *opus.Encoder
keyframes int
lastKeyFrames int
lastInterFrames int
sps []byte
pps []byte
stopMetadataCollection chan bool
// Metadata
startTime int64
lastTime int64 // Last time the metadata collector ran
audioBps int
videoBps int
audioPackets int
videoPackets int
lastAudioPackets int
lastVideoPackets int
clientVendorName string
clientVendorVersion string
videoCodec string
audioCodec string
videoHeight int
videoWidth int
outputBytes int
debugSaveVideo bool
debugVideoFile *os.File
lastFullFrame []byte
videoJoyCodec *h264joy.Codec
}
func (h *ConnHandler) OnServe(conn *rtmp.Conn) {
h.log.Info("OnServe: %#v", conn)
}
func (h *ConnHandler) OnConnect(timestamp uint32, cmd *rtmpmsg.NetConnectionConnect) (err error) {
h.log.Info("OnConnect: %#v", cmd)
h.metadataFailures = 0
h.errored = false
h.videoClockRate = 90000
// TODO: This can be customized by the user, we should figure out how to infer it from the client
h.audioClockRate = 48000
h.startTime = time.Now().Unix()
h.audioCodec = "opus"
h.videoCodec = "H264"
h.videoHeight = 0
h.videoWidth = 0
return nil
}
func (h *ConnHandler) OnCreateStream(timestamp uint32, cmd *rtmpmsg.NetConnectionCreateStream) error {
h.log.Info("OnCreateStream: %#v", cmd)
return nil
}
func (h *ConnHandler) OnPublish(ctx *rtmp.StreamContext, timestamp uint32, cmd *rtmpmsg.NetStreamPublish) (err error) {
h.log.Info("OnPublish: %#v", cmd)
if cmd.PublishingName == "" {
return errors.New("PublishingName is empty")
}
// Authenticate
auth := strings.SplitN(cmd.PublishingName, "-", 2)
u64, err := strconv.ParseUint(auth[0], 10, 32)
if err != nil {
h.log.Error(err)
return err
}
h.channelID = ftl.ChannelID(u64)
h.streamKey = []byte(auth[1])
if err := h.manager.NewStream(h.channelID); err != nil {
h.log.Error(err)
return err
}
h.started = true
if err := h.manager.Authenticate(h.channelID, h.streamKey); err != nil {
h.log.Error(err)
return err
}
stream, err := h.manager.StartStream(h.channelID)
if err != nil {
h.log.Error(err)
return err
}
h.authenticated = true
h.stream = stream
h.streamID = stream.streamID
// Add some meta info to the logger
h.log = h.log.WithFields(logrus.Fields{
"channel_id": h.channelID,
"stream_id": h.streamID,
})
if err := h.initVideo(h.videoClockRate); err != nil {
return err
}
if err := h.initAudio(h.audioClockRate); err != nil {
return err
}
go h.setupMetadataCollector()
return nil
}
func (h *ConnHandler) OnClose() {
h.log.Info("OnClose")
h.stopMetadataCollection <- true
// We only want to publish the stop if it's ours
if h.authenticated {
// StopStream mainly calls external services, there's a chance this call can hang for a bit while the other services are processing
// However it's not safe to call RemoveStream until this is finished or the pointer wont... exist?
if err := h.manager.StopStream(h.channelID); err != nil {
h.log.Error(err)
// panic(err)
}
}
h.authenticated = false
if h.started {
if err := h.manager.RemoveStream(h.channelID); err != nil {
h.log.Error(err)
// panic(err)
}
}
h.started = false
if h.audioDecoder != nil {
h.audioDecoder.Close()
h.audioDecoder = nil
}
if h.debugSaveVideo {
h.debugVideoFile.Close()
}
}
func (h *ConnHandler) initAudio(clockRate uint32) (err error) {
h.audioSequencer = rtp.NewFixedSequencer(0) // ftl client says this should be changed to a random value
h.audioPacketizer = rtp.NewPacketizer(FTL_MTU, FTL_AUDIO_PT, uint32(h.channelID), &codecs.OpusPayloader{}, h.audioSequencer, clockRate)
h.audioEncoder, err = opus.NewEncoder(int(clockRate), 2, opus.AppAudio)
if err != nil {
return err
}
h.audioDecoder = fdkaac.NewAacDecoder()
return nil
}
func (h *ConnHandler) OnAudio(timestamp uint32, payload io.Reader) error {
if h.errored {
return errors.New("stream is not longer authenticated")
}
// Convert AAC to opus
var audio flvtag.AudioData
if err := flvtag.DecodeAudioData(payload, &audio); err != nil {
return err
}
data, err := io.ReadAll(audio.Data)
if err != nil {
return err
}
if audio.AACPacketType == flvtag.AACPacketTypeSequenceHeader {
h.log.Infof("Created new codec %s", hex.EncodeToString(data))
err := h.audioDecoder.InitRaw(data)
if err != nil {
h.log.WithError(err).Errorf("error initializing stream")
return fmt.Errorf("can't initialize codec with %s", hex.EncodeToString(data))
}
return nil
}
pcm, err := h.audioDecoder.Decode(data)
if err != nil {
h.log.Errorf("decode error: %s %s", hex.EncodeToString(data), err)
return fmt.Errorf("decode error")
}
blockSize := 960
for h.audioBuffer = append(h.audioBuffer, pcm...); len(h.audioBuffer) >= blockSize*4; h.audioBuffer = h.audioBuffer[blockSize*4:] {
pcm16 := make([]int16, blockSize*2)
for i := 0; i < len(pcm16); i++ {
pcm16[i] = int16(binary.LittleEndian.Uint16(h.audioBuffer[i*2:]))
}
bufferSize := 1024
opusData := make([]byte, bufferSize)
n, err := h.audioEncoder.Encode(pcm16, opusData)
if err != nil {
return err
}
opusOutput := opusData[:n]
packets := h.audioPacketizer.Packetize(opusOutput, uint32(blockSize))
for _, p := range packets {
h.audioPackets++
h.outputBytes += len(p.Payload)
if err := h.stream.WriteRTP(p); err != nil {
h.log.Error(err)
return err
}
}
}
return nil
}
func (h *ConnHandler) initVideo(clockRate uint32) (err error) {
h.videoSequencer = rtp.NewFixedSequencer(25000)
h.videoPacketizer = rtp.NewPacketizer(FTL_MTU, FTL_VIDEO_PT, uint32(h.channelID+1), &codecs.H264Payloader{}, h.videoSequencer, clockRate)
if h.debugSaveVideo {
h.debugVideoFile, err = os.Create(fmt.Sprintf("debug-video-%d.h264", h.streamID))
return err
}
return nil
}
func (h *ConnHandler) OnVideo(timestamp uint32, payload io.Reader) error {
if h.errored {
return errors.New("stream is not longer authenticated")
}
var video flvtag.VideoData
if err := flvtag.DecodeVideoData(payload, &video); err != nil {
return err
}
// video.CodecID == H264, I wonder if we should check this?
// video.FrameType does not seem to contain b-frames even if they exist
switch video.FrameType {
case flvtag.FrameTypeKeyFrame:
h.lastKeyFrames += 1
h.keyframes += 1
case flvtag.FrameTypeInterFrame:
h.lastInterFrames += 1
default:
h.log.Debug("Unknown FLV Video Frame: %+v\n", video)
}
data, err := io.ReadAll(video.Data)
if err != nil {
return err
}
// From: https://github.com/nareix/joy5/blob/2c912ca30590ee653145d93873b0952716d21093/cmd/avtool/seqhdr.go#L38-L65
// joy5 is an unlicensed project -- need to confirm usage.
// Look at video.AVCPacketType == flvtag.AVCPacketTypeSequenceHeader to figure out sps and pps
// Store those in the stream object, then use them later for the keyframes
if video.AVCPacketType == flvtag.AVCPacketTypeSequenceHeader {
h.videoJoyCodec, err = h264joy.FromDecoderConfig(data)
if err != nil {
return err
}
}
var outBuf []byte
if video.FrameType == flvtag.FrameTypeKeyFrame {
pktnalus, _ := h264joy.SplitNALUs(data)
nalus := [][]byte{}
nalus = append(nalus, h264joy.Map2arr(h.videoJoyCodec.SPS)...)
nalus = append(nalus, h264joy.Map2arr(h.videoJoyCodec.PPS)...)
nalus = append(nalus, pktnalus...)
data := h264joy.JoinNALUsAnnexb(nalus)
outBuf = data
} else {
pktnalus, _ := h264joy.SplitNALUs(data)
data := h264joy.JoinNALUsAnnexb(pktnalus)
outBuf = data
}
h.debugVideoFile.Write(outBuf)
if video.FrameType == flvtag.FrameTypeKeyFrame {
// Save the last full keyframe for anything we may need, eg thumbnails
h.lastFullFrame = outBuf
}
// Likely there's more than one set of RTP packets in this read
samples := uint32(len(outBuf)) + h.videoClockRate
packets := h.videoPacketizer.Packetize(outBuf, samples)
for _, p := range packets {
h.videoPackets++
h.outputBytes += len(p.Payload)
if err := h.stream.WriteRTP(p); err != nil {
h.log.Error(err)
return err
}
}
return nil
}
func (h *ConnHandler) sendThumbnail() {
var img image.Image
h264dec, err := h264.NewH264Decoder()
if err != nil {
h.log.Error(err)
return
}
defer h264dec.Close()
img, err = h264dec.Decode(h.lastFullFrame)
if err != nil {
h.log.Error(err)
return
}
if img != nil {
buff := new(bytes.Buffer)
err = jpeg.Encode(buff, img, &jpeg.Options{
Quality: 75,
})
if err != nil {
h.log.Error(err)
return
}
err = h.manager.service.SendJpegPreviewImage(h.streamID, buff.Bytes())
if err != nil {
h.log.Error(err)
}
buff.Reset()
// Also update our metadata
h.videoWidth = img.Bounds().Dx()
h.videoHeight = img.Bounds().Dy()
}
}
func (h *ConnHandler) sendMetadata() error {
return h.manager.service.UpdateStreamMetadata(h.streamID, services.StreamMetadata{
AudioCodec: h.audioCodec,
IngestServer: h.manager.orchestrator.ClientHostname,
IngestViewers: 0,
LostPackets: 0, // Don't exist
NackPackets: 0, // Don't exist
RecvPackets: h.videoPackets + h.audioPackets,
SourceBitrate: 0, // Likely just need to calculate the bytes between two 5s snapshots?
SourcePing: 0, // Not accessible unless we ping them manually
StreamTimeSeconds: int(h.lastTime - h.startTime),
VendorName: h.clientVendorName,
VendorVersion: h.clientVendorVersion,
VideoCodec: h.videoCodec,
VideoHeight: h.videoHeight,
VideoWidth: h.videoWidth,
})
}
func (h *ConnHandler) setupMetadataCollector() {
ticker := time.NewTicker(5 * time.Second)
go func() {
for {
select {
case <-ticker.C:
h.lastTime = time.Now().Unix()
h.log.WithFields(logrus.Fields{
"keyframes": h.lastKeyFrames,
"interframes": h.lastInterFrames,
"packets": h.videoPackets - h.lastVideoPackets,
"bytes": h.outputBytes,
}).Debug("Processed 5s of input frames from RTMP input")
// Check to ensure we're not over our bandwidth limit
if h.outputBytes >= BANDWIDTH_LIMIT {
h.log.Errorf("Sent %d bytes over the last 5 seconds, ending stream", h.outputBytes)
h.errored = true
}
h.outputBytes = 0
// Calculate some of our last fields
h.audioBps = 0
h.lastVideoPackets = h.videoPackets
h.lastKeyFrames = 0
h.lastInterFrames = 0
if len(h.lastFullFrame) > 0 {
// Todo: Handle thumbnail failures
h.sendThumbnail()
}
err := h.sendMetadata()
if err != nil {
// Unauthenticate us so the next Video / Audio packet can stop the stream
h.metadataFailures += 1
if h.metadataFailures > 5 {
h.errored = true
h.log.Error("Metadata failures exceed 5, terminating the stream")
}
h.log.Warn(err)
}
h.metadataFailures = 0
case <-h.stopMetadataCollection:
ticker.Stop()
return
}
}
}()
}