-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrtmp_publisher.go
224 lines (190 loc) · 5.79 KB
/
rtmp_publisher.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
package rtsp2rtmp
import (
// "flag"
"fmt"
"os"
"time"
// "github.com/zhangpeihao/goflv"
rtmp "github.com/falconray0704/gortmp"
"github.com/zhangpeihao/log"
)
/*
const (
programName = "RtmpPublisher"
version = "0.0.1"
)
var (
url *string = flag.String("URL", "rtmp://video-center.alivecdn.com/AppName/StreamName?vhost=live.gz-app.com", "The rtmp url to connect.")
streamName *string = flag.String("Stream", "camstream", "Stream name to play.")
flvFileName *string = flag.String("FLV", "./v_4097.flv", "FLV file to publishs.")
)
*/
const (
AUDIO_TAG = byte(0x08)
VIDEO_TAG = byte(0x09)
SCRIPT_DATA_TAG = byte(0x12)
DURATION_OFFSET = 53
HEADER_LEN = 13
)
type FlvChunk struct {
TagType byte
DataSize uint32
Timestamp uint32
NaluData []byte
}
type RtmpPublisherContext struct {
rtmpUrl string
streamName string
obConn rtmp.OutboundConn
createStreamChan chan rtmp.OutboundStream
logger log.Logger
stopStreamChan chan uint32
flvChan chan FlvChunk
}
type RtmpOutboundConnHandler struct {
RtmpPublisherCtx RtmpPublisherContext
videoDataSize int64
audioDataSize int64
}
//var flvFile *flv.File
var status uint
func (handler *RtmpOutboundConnHandler) OnStatus(conn rtmp.OutboundConn) {
var err error
if handler.RtmpPublisherCtx.obConn == nil {
return
}
status, err = handler.RtmpPublisherCtx.obConn.Status()
fmt.Printf("@@@@@@@@@@@@@status: %d, err: %v\n", status, err)
}
func (handler *RtmpOutboundConnHandler) OnClosed(conn rtmp.Conn) {
fmt.Printf("@@@@@@@@@@@@@Closed\n")
}
func (handler *RtmpOutboundConnHandler) OnReceived(conn rtmp.Conn, message *rtmp.Message) {
}
func (handler *RtmpOutboundConnHandler) OnReceivedRtmpCommand(conn rtmp.Conn, command *rtmp.Command) {
fmt.Printf("ReceviedRtmpCommand: %+v\n", command)
}
func (handler *RtmpOutboundConnHandler) OnStreamCreated(conn rtmp.OutboundConn, stream rtmp.OutboundStream) {
fmt.Printf("Stream created: %d\n", stream.ID())
handler.RtmpPublisherCtx.createStreamChan <- stream
}
func (handler *RtmpOutboundConnHandler) OnPlayStart(stream rtmp.OutboundStream) {
}
func (handler *RtmpOutboundConnHandler) OnPublishStart(stream rtmp.OutboundStream) {
// Set chunk buffer size
go publish(stream, &handler.RtmpPublisherCtx)
}
func publish(stream rtmp.OutboundStream, ctx *RtmpPublisherContext) {
fmt.Println("1")
var err error
fmt.Println("2")
//defer flvFile.Close()
startTs := uint32(0)
startAt := time.Now().UnixNano()
preTs := uint32(0)
fmt.Println("3")
for status == rtmp.OUTBOUND_CONN_STATUS_CREATE_STREAM_OK {
/*
if flvFile.IsFinished() {
fmt.Println("@@@@@@@@@@@@@@File finished")
flvFile.LoopBack()
startAt = time.Now().UnixNano()
startTs = uint32(0)
preTs = uint32(0)
}
header, data, err := flvFile.ReadTag()
if err != nil {
fmt.Println("flvFile.ReadTag() error:", err)
break
}
switch header.TagType {
case flv.VIDEO_TAG:
videoDataSize += int64(len(data))
case flv.AUDIO_TAG:
audioDataSize += int64(len(data))
}
*/
var flvChunk FlvChunk
select {
case flvChunk = <-ctx.flvChan:
}
if startTs == uint32(0) {
startTs = flvChunk.Timestamp
}
diff1 := uint32(0)
// deltaTs := uint32(0)
if flvChunk.Timestamp > startTs {
diff1 = flvChunk.Timestamp - startTs
} else {
//fmt.Printf("@@@@@@@@@@@@@@diff1 header(%+v), startTs: %d\n", flvChunk, startTs)
fmt.Printf("@@@@@@@@@@@@@@diff1 startTs: %d\n", startTs)
}
if diff1 > preTs {
// deltaTs = diff1 - preTs
preTs = diff1
}
//fmt.Printf("@@@@@@@@@@@@@@diff1 header(%+v), startTs: %d\n", flvChunk, startTs)
fmt.Printf("@@@@@@@@@@@@@@diff1 startTs: %d\n", startTs)
if err = stream.PublishData(flvChunk.TagType, flvChunk.NaluData, diff1); err != nil {
fmt.Println("PublishData() error:", err)
break
}
diff2 := uint32((time.Now().UnixNano() - startAt) / 1000000)
// fmt.Printf("diff1: %d, diff2: %d\n", diff1, diff2)
if diff1 > diff2+100 {
// fmt.Printf("header.Timestamp: %d, now: %d\n", header.Timestamp, time.Now().UnixNano())
time.Sleep(time.Millisecond * time.Duration(diff1-diff2))
}
}
}
func InitRtmpHandler(handler *RtmpOutboundConnHandler, streamName string, rtmpUrl string, inFlvChan chan FlvChunk) {
handler.RtmpPublisherCtx.streamName = streamName
handler.RtmpPublisherCtx.rtmpUrl = rtmpUrl
handler.RtmpPublisherCtx.flvChan = inFlvChan
handler.RtmpPublisherCtx.logger = *(log.NewLogger(".", "publisher", nil, 60, 3600*24, true))
rtmp.InitLogger(&handler.RtmpPublisherCtx.logger)
//defer ctx.logger.Close()
handler.RtmpPublisherCtx.createStreamChan = make(chan rtmp.OutboundStream)
handler.RtmpPublisherCtx.stopStreamChan = make(chan uint32)
}
func runPublish(handler *RtmpOutboundConnHandler) {
for {
select {
case stream := <-handler.RtmpPublisherCtx.createStreamChan:
// Publish
stream.Attach(handler)
err := stream.Publish(handler.RtmpPublisherCtx.streamName, "live")
if err != nil {
fmt.Printf("Publish error: %s", err.Error())
os.Exit(-1)
}
case stop := <-handler.RtmpPublisherCtx.stopStreamChan:
if stop == uint32(0x01) {
break
}
case <-time.After(1 * time.Second):
fmt.Printf("Audio size: %d bytes; Vedio size: %d bytes\n", handler.audioDataSize, handler.videoDataSize)
}
}
}
func StartPublish(handler *RtmpOutboundConnHandler) {
//rtmpHandler := &RtmpOutboundConnHandler{ RtmpPublisherCtx : ctx}
fmt.Println("to dial")
fmt.Println("a")
var err error
handler.RtmpPublisherCtx.obConn, err = rtmp.Dial(handler.RtmpPublisherCtx.rtmpUrl, handler, 100)
if err != nil {
fmt.Println("Dial error", err)
os.Exit(-1)
}
fmt.Println("b")
//defer obConn.Close()
fmt.Println("to connect")
err = handler.RtmpPublisherCtx.obConn.Connect()
if err != nil {
fmt.Printf("Connect error: %s", err.Error())
os.Exit(-1)
}
fmt.Println("c")
go runPublish(handler)
}