-
Notifications
You must be signed in to change notification settings - Fork 16
/
rtmp.go
133 lines (109 loc) · 3.09 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
package main
import (
"bytes"
"encoding/binary"
"io"
"log"
"net"
"time"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/pkg/errors"
flvtag "github.com/yutopp/go-flv/tag"
"github.com/yutopp/go-rtmp"
rtmpmsg "github.com/yutopp/go-rtmp/message"
)
func startRTMPServer(peerConnection *webrtc.PeerConnection, videoTrack, audioTrack *webrtc.TrackLocalStaticSample) {
log.Println("Starting RTMP Server")
tcpAddr, err := net.ResolveTCPAddr("tcp", ":1935")
if err != nil {
log.Panicf("Failed: %+v", err)
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Panicf("Failed: %+v", err)
}
srv := rtmp.NewServer(&rtmp.ServerConfig{
OnConnect: func(conn net.Conn) (io.ReadWriteCloser, *rtmp.ConnConfig) {
return conn, &rtmp.ConnConfig{
Handler: &Handler{
peerConnection: peerConnection,
videoTrack: videoTrack,
audioTrack: audioTrack,
},
ControlState: rtmp.StreamControlStateConfig{
DefaultBandwidthWindowSize: 6 * 1024 * 1024 / 8,
},
}
},
})
if err := srv.Serve(listener); err != nil {
log.Panicf("Failed: %+v", err)
}
}
type Handler struct {
rtmp.DefaultHandler
peerConnection *webrtc.PeerConnection
videoTrack, audioTrack *webrtc.TrackLocalStaticSample
}
func (h *Handler) OnServe(conn *rtmp.Conn) {
}
func (h *Handler) OnConnect(timestamp uint32, cmd *rtmpmsg.NetConnectionConnect) error {
log.Printf("OnConnect: %#v", cmd)
return nil
}
func (h *Handler) OnCreateStream(timestamp uint32, cmd *rtmpmsg.NetConnectionCreateStream) error {
log.Printf("OnCreateStream: %#v", cmd)
return nil
}
func (h *Handler) OnPublish(timestamp uint32, cmd *rtmpmsg.NetStreamPublish) error {
log.Printf("OnPublish: %#v", cmd)
if cmd.PublishingName == "" {
return errors.New("PublishingName is empty")
}
return nil
}
func (h *Handler) OnAudio(timestamp uint32, payload io.Reader) error {
var audio flvtag.AudioData
if err := flvtag.DecodeAudioData(payload, &audio); err != nil {
return err
}
data := new(bytes.Buffer)
if _, err := io.Copy(data, audio.Data); err != nil {
return err
}
return h.audioTrack.WriteSample(media.Sample{
Data: data.Bytes(),
Duration: 128 * time.Millisecond,
})
}
const headerLengthField = 4
func (h *Handler) OnVideo(timestamp uint32, payload io.Reader) error {
var video flvtag.VideoData
if err := flvtag.DecodeVideoData(payload, &video); err != nil {
return err
}
data := new(bytes.Buffer)
if _, err := io.Copy(data, video.Data); err != nil {
return err
}
outBuf := []byte{}
videoBuffer := data.Bytes()
for offset := 0; offset < len(videoBuffer); {
bufferLength := int(binary.BigEndian.Uint32(videoBuffer[offset : offset+headerLengthField]))
if offset+bufferLength >= len(videoBuffer) {
break
}
offset += headerLengthField
outBuf = append(outBuf, []byte{0x00, 0x00, 0x00, 0x01}...)
outBuf = append(outBuf, videoBuffer[offset:offset+bufferLength]...)
offset += int(bufferLength)
}
return h.videoTrack.WriteSample(media.Sample{
Data: outBuf,
Duration: time.Second / 30,
})
}
func (h *Handler) OnClose() {
log.Printf("OnClose")
}