-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrtmp_client.go
More file actions
143 lines (124 loc) · 3.59 KB
/
Copy pathrtmp_client.go
File metadata and controls
143 lines (124 loc) · 3.59 KB
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
package main
import (
"net"
// "bufio"
"bytes"
"crypto/tls"
"encoding/gob"
"flag"
"fmt"
"os"
"time"
// "strconv"
quicconn "github.com/marten-seemann/quic-conn"
rtmp "github.com/zhangpeihao/gortmp"
)
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
var conn net.Conn
var network bytes.Buffer
var enc *gob.Encoder
var (
url *string = flag.String("URL", "rtmp://84.20.77.50/live", "The rtmp url to connect.")
streamName *string = flag.String("Stream", "livestream1", "Stream name to play.")
)
func (handler *TestOutboundConnHandler) OnStatus(conn rtmp.OutboundConn) {
var err error
status, err = conn.Status()
fmt.Printf("@@@@@@@@@@@@@status: %d, err: %v\n", status, err)
}
func (handler *TestOutboundConnHandler) OnClosed(conn rtmp.Conn) {
fmt.Printf("@@@@@@@@@@@@@Closed\n")
}
type P struct {
Buf []byte
Type uint8
Timestamp uint32
AbsoluteTimestamp uint32
}
func (handler *TestOutboundConnHandler) OnReceived(rconn rtmp.Conn, message *rtmp.Message) {
switch message.Type {
case rtmp.VIDEO_TYPE:
// if flvFile != nil {
// flvFile.WriteVideoTag(message.Buf.Bytes(), message.AbsoluteTimestamp)
// }
videoDataSize += int64(message.Buf.Len())
// fmt.Println("**",message.Buf.Len())
if err := enc.Encode(P{message.Buf.Bytes(), message.Type, message.Timestamp, message.AbsoluteTimestamp}); err != nil {
fmt.Println("error")
panic(err)
}
case rtmp.AUDIO_TYPE:
// if flvFile != nil {
// flvFile.WriteAudioTag(message.Buf.Bytes(), message.AbsoluteTimestamp)
// }
audioDataSize += int64(message.Buf.Len())
// fmt.Println("**",message.Buf.Len())
if err := enc.Encode(P{message.Buf.Bytes(), message.Type, message.Timestamp, message.AbsoluteTimestamp}); err != nil {
fmt.Println(err)
panic(err)
}
}
}
func (handler *TestOutboundConnHandler) OnReceivedRtmpCommand(conn rtmp.Conn, command *rtmp.Command) {
fmt.Printf("ReceviedCommand: %+v\n", command)
}
func (handler *TestOutboundConnHandler) OnStreamCreated(conn rtmp.OutboundConn, stream rtmp.OutboundStream) {
fmt.Printf("Stream created: %d\n", stream.ID())
createStreamChan <- stream
}
func main() {
// utils.SetLogLevel(utils.LogLevelDebug)
startClient := flag.Bool("c", false, "client")
flag.Parse()
if *startClient {
// run the client
go func() {
var err error
tlsConf := &tls.Config{} //InsecureSkipVerify: true}
conn, err = quicconn.Dial("streemtechnology.com:8081", tlsConf)
if err != nil {
panic(err)
}
enc = gob.NewEncoder(conn)
// fmt.Fprintf(conn, message+strconv.Itoa(i)+"\n")
createStreamChan = make(chan rtmp.OutboundStream)
testHandler := &TestOutboundConnHandler{}
fmt.Println("to dial")
obConn, err = rtmp.Dial(*url, testHandler, 100)
if err != nil {
fmt.Println("Dial error", err)
os.Exit(-1)
}
defer obConn.Close()
fmt.Printf("obConn: %+v\n", obConn)
fmt.Printf("obConn.URL(): %s\n", obConn.URL())
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:
// Play
err = stream.Play(*streamName, nil, nil, nil)
if err != nil {
fmt.Printf("Play 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)
}
}
}()
}
time.Sleep(time.Hour)
}