-
Notifications
You must be signed in to change notification settings - Fork 161
/
inboundstream.go
281 lines (257 loc) · 7.36 KB
/
inboundstream.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"bytes"
"fmt"
"github.com/zhangpeihao/goamf"
"github.com/zhangpeihao/log"
)
type InboundStreamHandler interface {
OnPlayStart(stream InboundStream)
OnPublishStart(stream InboundStream)
OnReceiveAudio(stream InboundStream, on bool)
OnReceiveVideo(stream InboundStream, on bool)
}
// Message stream:
//
// A logical channel of communication that allows the flow of
// messages.
type inboundStream struct {
id uint32
streamName string
conn *inboundConn
chunkStreamID uint32
handler InboundStreamHandler
bufferLength uint32
}
// A RTMP logical stream on connection.
type InboundStream interface {
Conn() InboundConn
// ID
ID() uint32
// StreamName
StreamName() string
// Close
Close()
// Received messages
Received(message *Message) (handlered bool)
// Attach handler
Attach(handler InboundStreamHandler)
// Send audio data
SendAudioData(data []byte, deltaTimestamp uint32) error
// Send video data
SendVideoData(data []byte, deltaTimestamp uint32) error
// Send data
SendData(dataType uint8, data []byte, deltaTimestamp uint32) error
}
func (stream *inboundStream) Conn() InboundConn {
return stream.conn
}
// ID
func (stream *inboundStream) ID() uint32 {
return stream.id
}
// StreamName
func (stream *inboundStream) StreamName() string {
return stream.streamName
}
// Close
func (stream *inboundStream) Close() {
var err error
cmd := &Command{
IsFlex: true,
Name: "closeStream",
TransactionID: 0,
Objects: make([]interface{}, 1),
}
cmd.Objects[0] = nil
message := NewMessage(stream.chunkStreamID, COMMAND_AMF3, stream.id, AUTO_TIMESTAMP, nil)
if err = cmd.Write(message.Buf); err != nil {
return
}
message.Dump("closeStream")
conn := stream.conn.Conn()
conn.Send(message)
}
func (stream *inboundStream) Received(message *Message) bool {
if message.Type == VIDEO_TYPE || message.Type == AUDIO_TYPE {
return false
}
var err error
if message.Type == COMMAND_AMF0 || message.Type == COMMAND_AMF3 {
cmd := &Command{}
if message.Type == COMMAND_AMF3 {
cmd.IsFlex = true
_, err = message.Buf.ReadByte()
if err != nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundStream::Received() Read first in flex commad err:", err)
return true
}
}
cmd.Name, err = amf.ReadString(message.Buf)
if err != nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundStream::Received() AMF0 Read name err:", err)
return true
}
var transactionID float64
transactionID, err = amf.ReadDouble(message.Buf)
if err != nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundStream::Received() AMF0 Read transactionID err:", err)
return true
}
cmd.TransactionID = uint32(transactionID)
var object interface{}
for message.Buf.Len() > 0 {
object, err = amf.ReadValue(message.Buf)
if err != nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundStream::Received() AMF0 Read object err:", err)
return true
}
cmd.Objects = append(cmd.Objects, object)
}
switch cmd.Name {
case "play":
return stream.onPlay(cmd)
case "publish":
return stream.onPublish(cmd)
case "recevieAudio":
return stream.onRecevieAudio(cmd)
case "recevieVideo":
return stream.onRecevieVideo(cmd)
case "closeStream":
return stream.onCloseStream(cmd)
default:
logger.ModulePrintf(logHandler, log.LOG_LEVEL_TRACE, "inboundStream::Received: %+v\n", cmd)
}
}
return false
}
func (stream *inboundStream) Attach(handler InboundStreamHandler) {
stream.handler = handler
}
// Send audio data
func (stream *inboundStream) SendAudioData(data []byte, deltaTimestamp uint32) (err error) {
message := NewMessage(stream.chunkStreamID-4, AUDIO_TYPE, stream.id, AUTO_TIMESTAMP, data)
message.Timestamp = deltaTimestamp
return stream.conn.Send(message)
}
// Send video data
func (stream *inboundStream) SendVideoData(data []byte, deltaTimestamp uint32) (err error) {
message := NewMessage(stream.chunkStreamID-4, VIDEO_TYPE, stream.id, AUTO_TIMESTAMP, data)
message.Timestamp = deltaTimestamp
return stream.conn.Send(message)
}
// Send data
func (stream *inboundStream) SendData(dataType uint8, data []byte, deltaTimestamp uint32) (err error) {
var csid uint32
switch dataType {
case VIDEO_TYPE:
csid = stream.chunkStreamID - 4
case AUDIO_TYPE:
csid = stream.chunkStreamID - 4
default:
csid = stream.chunkStreamID
}
message := NewMessage(csid, dataType, stream.id, AUTO_TIMESTAMP, data)
message.Timestamp = deltaTimestamp
return stream.conn.Send(message)
}
func (stream *inboundStream) onPlay(cmd *Command) bool {
// Get stream name
if cmd.Objects == nil || len(cmd.Objects) < 2 || cmd.Objects[1] == nil {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundStream::onPlay: command error 1! %+v\n", cmd)
return true
}
if streamName, ok := cmd.Objects[1].(string); !ok {
logger.ModulePrintf(logHandler, log.LOG_LEVEL_WARNING,
"inboundStream::onPlay: command error 2! %+v\n", cmd)
return true
} else {
stream.streamName = streamName
}
// Response
stream.conn.conn.SetChunkSize(4096)
stream.conn.conn.SendUserControlMessage(EVENT_STREAM_BEGIN)
stream.streamReset()
stream.streamStart()
stream.rtmpSampleAccess()
stream.handler.OnPlayStart(stream)
return true
}
func (stream *inboundStream) onPublish(cmd *Command) bool {
return true
}
func (stream *inboundStream) onRecevieAudio(cmd *Command) bool {
return true
}
func (stream *inboundStream) onRecevieVideo(cmd *Command) bool {
return true
}
func (stream *inboundStream) onCloseStream(cmd *Command) bool {
return true
}
func (stream *inboundStream) streamReset() {
cmd := &Command{
IsFlex: false,
Name: "onStatus",
TransactionID: 0,
Objects: make([]interface{}, 2),
}
cmd.Objects[0] = nil
cmd.Objects[1] = amf.Object{
"level": "status",
"code": NETSTREAM_PLAY_RESET,
"description": fmt.Sprintf("playing and resetting %s", stream.streamName),
"details": stream.streamName,
}
buf := new(bytes.Buffer)
err := cmd.Write(buf)
CheckError(err, "inboundStream::streamReset() Create command")
message := &Message{
ChunkStreamID: CS_ID_USER_CONTROL,
Type: COMMAND_AMF0,
Size: uint32(buf.Len()),
Buf: buf,
}
message.Dump("streamReset")
stream.conn.conn.Send(message)
}
func (stream *inboundStream) streamStart() {
cmd := &Command{
IsFlex: false,
Name: "onStatus",
TransactionID: 0,
Objects: make([]interface{}, 2),
}
cmd.Objects[0] = nil
cmd.Objects[1] = amf.Object{
"level": "status",
"code": NETSTREAM_PLAY_START,
"description": fmt.Sprintf("Started playing %s", stream.streamName),
"details": stream.streamName,
}
buf := new(bytes.Buffer)
err := cmd.Write(buf)
CheckError(err, "inboundStream::streamStart() Create command")
message := &Message{
ChunkStreamID: CS_ID_USER_CONTROL,
Type: COMMAND_AMF0,
Size: uint32(buf.Len()),
Buf: buf,
}
message.Dump("streamStart")
stream.conn.conn.Send(message)
}
func (stream *inboundStream) rtmpSampleAccess() {
message := NewMessage(CS_ID_USER_CONTROL, DATA_AMF0, 0, 0, nil)
amf.WriteString(message.Buf, "|RtmpSampleAccess")
amf.WriteBoolean(message.Buf, false)
amf.WriteBoolean(message.Buf, false)
message.Dump("rtmpSampleAccess")
stream.conn.conn.Send(message)
}