forked from xiangxud/rtmp-to-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtmp.go
216 lines (184 loc) · 5.62 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
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"log"
"net"
"time"
"github.com/Glimesh/go-fdkaac/fdkaac"
"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"
// opus "gopkg.in/hraban/opus.v2"
opus "github.com/sean-der/rtmp-to-webrtc/opus"
)
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
audioDecoder *fdkaac.AacDecoder
audioEncoder *opus.Encoder
audioBuffer []byte
audioClockRate uint32
}
func (h *Handler) OnServe(conn *rtmp.Conn) {
}
func (h *Handler) OnConnect(timestamp uint32, cmd *rtmpmsg.NetConnectionConnect) error {
log.Printf("OnConnect: %#v", cmd)
h.audioClockRate = 48000
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) SetOpusCtl() {
h.audioEncoder.SetMaxBandwidth(opus.Bandwidth(2))
h.audioEncoder.SetComplexity(9)
h.audioEncoder.SetBitrateToAuto()
h.audioEncoder.SetInBandFEC(true)
}
func (h *Handler) initAudio(clockRate uint32) error {
encoder, err := opus.NewEncoder(48000, 2, opus.AppAudio)
if err != nil {
println(err.Error())
return err
}
h.audioEncoder = encoder
h.SetOpusCtl()
h.audioDecoder = fdkaac.NewAacDecoder()
return nil
}
func (h *Handler) OnAudio(timestamp uint32, payload io.Reader) error {
// Convert AAC to opus
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
}
if data.Len() <= 0 {
log.Println("no audio datas", timestamp, payload)
return fmt.Errorf("no audio datas")
}
// log.Println("\r\ntimestamp->", timestamp, "\r\npayload->", payload, "\r\naudio data->", data.Bytes())
datas := data.Bytes()
// log.Println("\r\naudio data len:", len(datas), "->") // hex.EncodeToString(datas))
if audio.AACPacketType == flvtag.AACPacketTypeSequenceHeader {
log.Println("Created new codec ", hex.EncodeToString(datas))
err := h.initAudio(h.audioClockRate)
if err != nil {
log.Println(err, "error initializing Audio")
return fmt.Errorf("can't initialize codec with %s", err.Error())
}
err = h.audioDecoder.InitRaw(datas)
if err != nil {
log.Println(err, "error initializing stream")
return fmt.Errorf("can't initialize codec with %s", hex.EncodeToString(datas))
}
return nil
}
pcm, err := h.audioDecoder.Decode(datas)
if err != nil {
log.Println("decode error: ", hex.EncodeToString(datas), err)
return fmt.Errorf("decode error")
}
// log.Println("\r\npcm len ", len(pcm), " ->") //, pcm)
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)
pcm16len := len(pcm16)
for i := 0; i < pcm16len; i++ {
pcm16[i] = int16(binary.LittleEndian.Uint16(h.audioBuffer[i*2:]))
}
bufferSize := 1024
opusData := make([]byte, bufferSize)
n, err := h.audioEncoder.Encode(pcm16, opusData)
// n, err := h.audioEncoder.ReadEncode(pcm16, opusData)
if err != nil {
return err
}
opusOutput := opusData[:n]
// log.Println(" pcm len:", pcm16len, " data->", " opusData len", n, " data->")
if audioErr := h.audioTrack.WriteSample(media.Sample{
Data: opusOutput,
Duration: 20 * time.Millisecond,
}); audioErr != nil {
log.Println("WriteSample err", audioErr)
}
}
return nil
}
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")
}