forked from shethchintan7/gortmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtmp_publisher.go
165 lines (146 loc) · 4.13 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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/zhangpeihao/goflv"
rtmp "github.com/zhangpeihao/gortmp"
"github.com/zhangpeihao/log"
)
const (
programName = "RtmpPublisher"
version = "0.0.1"
)
var (
url *string = flag.String("URL", "rtmp://192.168.20.111/vid3", "The rtmp url to connect.")
streamName *string = flag.String("Stream", "camstream", "Stream name to play.")
flvFileName *string = flag.String("FLV", "", "FLV file to publishs.")
)
type TestOutboundConnHandler struct {
}
var obConn rtmp.OutboundConn
var createStreamChan chan rtmp.OutboundStream
var videoDataSize int64
var audioDataSize int64
var flvFile *flv.File
var status uint
func (handler *TestOutboundConnHandler) OnStatus(conn rtmp.OutboundConn) {
var err error
status, err = obConn.Status()
fmt.Printf("@@@@@@@@@@@@@status: %d, err: %v\n", status, err)
}
func (handler *TestOutboundConnHandler) OnClosed(conn rtmp.Conn) {
fmt.Printf("@@@@@@@@@@@@@Closed\n")
}
func (handler *TestOutboundConnHandler) OnReceived(conn rtmp.Conn, message *rtmp.Message) {
}
func (handler *TestOutboundConnHandler) OnReceivedRtmpCommand(conn rtmp.Conn, command *rtmp.Command) {
fmt.Printf("ReceviedRtmpCommand: %+v\n", command)
}
func (handler *TestOutboundConnHandler) OnStreamCreated(conn rtmp.OutboundConn, stream rtmp.OutboundStream) {
fmt.Printf("Stream created: %d\n", stream.ID())
createStreamChan <- stream
}
func (handler *TestOutboundConnHandler) OnPlayStart(stream rtmp.OutboundStream) {
}
func (handler *TestOutboundConnHandler) OnPublishStart(stream rtmp.OutboundStream) {
// Set chunk buffer size
go publish(stream)
}
func publish(stream rtmp.OutboundStream) {
var err error
flvFile, err = flv.OpenFile(*flvFileName)
if err != nil {
fmt.Println("Open FLV dump file error:", err)
return
}
defer flvFile.Close()
startTs := uint32(0)
startAt := time.Now().UnixNano()
preTs := uint32(0)
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))
}
if startTs == uint32(0) {
startTs = header.Timestamp
}
diff1 := uint32(0)
// deltaTs := uint32(0)
if header.Timestamp > startTs {
diff1 = header.Timestamp - startTs
} else {
fmt.Println("@@@@@@@@@@@@@@diff1")
}
if diff1 > preTs {
// deltaTs = diff1 - preTs
preTs = diff1
}
if err = stream.PublishData(header.TagType, data, 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 main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s version[%s]\r\nUsage: %s [OPTIONS]\r\n", programName, version, os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
l := log.NewLogger(".", "publisher", nil, 60, 3600*24, true)
rtmp.InitLogger(l)
defer l.Close()
createStreamChan = make(chan rtmp.OutboundStream)
testHandler := &TestOutboundConnHandler{}
fmt.Println("to dial")
var err error
obConn, err = rtmp.Dial(*url, testHandler, 100)
if err != nil {
fmt.Println("Dial error", err)
os.Exit(-1)
}
defer obConn.Close()
fmt.Println("to connect")
err = obConn.Connect()
if err != nil {
fmt.Printf("Connect error: %s", err.Error())
os.Exit(-1)
}
for {
select {
case stream := <-createStreamChan:
// Publish
stream.Attach(testHandler)
err = stream.Publish(*streamName, "live")
if err != nil {
fmt.Printf("Publish error: %s", err.Error())
os.Exit(-1)
}
case <-time.After(1 * time.Second):
fmt.Printf("Audio size: %d bytes; Vedio size: %d bytes\n", audioDataSize, videoDataSize)
}
}
}